Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / sql / sqlite / test / fts3malloc.test
1 # 2009 October 22
2 #
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
5 #
6 #    May you do good and not evil.
7 #    May you find forgiveness for yourself and forgive others.
8 #    May you share freely, never taking more than you give.
9 #
10 #***********************************************************************
11 #
12 # This file contains tests to verify that malloc() errors that occur
13 # within the FTS3 module code are handled correctly. 
14 #
15
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 ifcapable !fts3 { finish_test ; return }
19 source $testdir/malloc_common.tcl
20 source $testdir/fts3_common.tcl
21
22 # Ensure the lookaside buffer is disabled for these tests.
23 #
24 sqlite3 db test.db
25 sqlite3_db_config_lookaside db 0 0 0
26
27 set sqlite_fts3_enable_parentheses 1
28 set DO_MALLOC_TEST 1
29
30 # Test organization:
31 #
32 # fts3_malloc-1.*: Test OOM during CREATE and DROP table statements.
33 # fts3_malloc-2.*: Test OOM during SELECT operations.
34 # fts3_malloc-3.*: Test OOM during SELECT operations with a larger database.
35 # fts3_malloc-4.*: Test OOM during database write operations.
36 # fts3_malloc-5.*: Test that a couple of memory leaks that could follow
37 #                  OOM in tokenizer code have been fixed.
38 #
39
40
41 proc normal_list {l} {
42   set ret [list]
43   foreach elem $l {lappend ret $elem}
44   set ret
45 }
46
47 do_write_test fts3_malloc-1.1 sqlite_master {
48   CREATE VIRTUAL TABLE ft1 USING fts3(a, b)
49 }
50 do_write_test fts3_malloc-1.2 sqlite_master {
51   CREATE VIRTUAL TABLE ft2 USING fts3([a], [b]);
52 }
53 do_write_test fts3_malloc-1.3 sqlite_master {
54   CREATE VIRTUAL TABLE ft3 USING fts3('a', "b");
55 }
56 do_write_test fts3_malloc-1.4 sqlite_master {
57   CREATE VIRTUAL TABLE ft4 USING fts3(`a`, 'fred''s column');
58 }
59 do_error_test fts3_malloc-1.5 {
60   CREATE VIRTUAL TABLE ft5 USING fts3(a, b, tokenize unknown)
61 } {unknown tokenizer: unknown}
62 do_write_test fts3_malloc-1.6 sqlite_master {
63   CREATE VIRTUAL TABLE ft6 USING fts3(a, b, tokenize porter)
64 }
65
66 # Test the xConnect/xDisconnect methods:
67 #db eval { ATTACH 'test2.db' AS aux }
68 #do_write_test fts3_malloc-1.6 aux.sqlite_master {
69 #  CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
70 #}
71 #do_write_test fts3_malloc-1.6 aux.sqlite_master {
72 #  CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
73 #}
74
75
76
77 do_test fts3_malloc-2.0 {
78   execsql { 
79     DROP TABLE ft1;
80     DROP TABLE ft2;
81     DROP TABLE ft3;
82     DROP TABLE ft4;
83     DROP TABLE ft6;
84   }
85   execsql { CREATE VIRTUAL TABLE ft USING fts3(a, b) }
86   for {set ii 1} {$ii < 32} {incr ii} {
87     set a [list]
88     set b [list]
89     if {$ii & 0x01} {lappend a one   ; lappend b neung}
90     if {$ii & 0x02} {lappend a two   ; lappend b song }
91     if {$ii & 0x04} {lappend a three ; lappend b sahm }
92     if {$ii & 0x08} {lappend a four  ; lappend b see  }
93     if {$ii & 0x10} {lappend a five  ; lappend b hah  }
94     execsql { INSERT INTO ft VALUES($a, $b) }
95   }
96 } {}
97
98 foreach {tn sql result} {
99   1 "SELECT count(*) FROM sqlite_master" {5}
100   2 "SELECT * FROM ft WHERE docid = 1"   {one neung}
101   3 "SELECT * FROM ft WHERE docid = 2"   {two song}
102   4 "SELECT * FROM ft WHERE docid = 3"   {{one two} {neung song}}
103
104   5 "SELECT a FROM ft" {
105     {one}                     {two}                 {one two}
106     {three}                   {one three}           {two three}     
107     {one two three}           {four}                {one four} 
108     {two four}                {one two four}        {three four}   
109     {one three four}          {two three four}      {one two three four}  
110     {five}                    {one five}            {two five}            
111     {one two five}            {three five}          {one three five} 
112     {two three five}          {one two three five}  {four five}
113     {one four five}           {two four five}       {one two four five}
114     {three four five}         {one three four five} {two three four five}
115     {one two three four five}
116   }
117
118   6 "SELECT a FROM ft WHERE a MATCH 'one'" {
119     {one} {one two} {one three} {one two three}
120     {one four} {one two four} {one three four} {one two three four}
121     {one five} {one two five} {one three five} {one two three five}
122     {one four five} {one two four five} 
123     {one three four five} {one two three four five}
124   }
125
126   7 "SELECT a FROM ft WHERE a MATCH 'o*'" {
127     {one} {one two} {one three} {one two three}
128     {one four} {one two four} {one three four} {one two three four}
129     {one five} {one two five} {one three five} {one two three five}
130     {one four five} {one two four five} 
131     {one three four five} {one two three four five}
132   }
133
134   8 "SELECT a FROM ft WHERE a MATCH 'o* t*'" {
135     {one two}             {one three}           {one two three} 
136     {one two four}        {one three four}      {one two three four} 
137     {one two five}        {one three five}      {one two three five} 
138     {one two four five}   {one three four five} {one two three four five}
139   }
140
141   9 "SELECT a FROM ft WHERE a MATCH '\"o* t*\"'" {
142     {one two}             {one three}           {one two three} 
143     {one two four}        {one three four}      {one two three four} 
144     {one two five}        {one three five}      {one two three five} 
145     {one two four five}   {one three four five} {one two three four five}
146   }
147
148   10 {SELECT a FROM ft WHERE a MATCH '"o* f*"'} {
149     {one four}            {one five}            {one four five}
150   }
151
152   11 {SELECT a FROM ft WHERE a MATCH '"one two three"'} {
153     {one two three}
154     {one two three four}  
155     {one two three five}
156     {one two three four five}
157   }
158
159   12 {SELECT a FROM ft WHERE a MATCH '"two three four"'} {
160     {two three four}
161     {one two three four}
162     {two three four five}
163     {one two three four five}
164   }
165
166   12 {SELECT a FROM ft WHERE a MATCH '"two three" five'} {
167     {two three five}         {one two three five}
168     {two three four five}    {one two three four five}
169   }
170
171   13 {SELECT a FROM ft WHERE ft MATCH '"song sahm" hah'} {
172     {two three five}         {one two three five}
173     {two three four five}    {one two three four five}
174   }
175
176   14 {SELECT a FROM ft WHERE b MATCH 'neung'} {
177     {one}                    {one two} 
178     {one three}              {one two three}
179     {one four}               {one two four} 
180     {one three four}         {one two three four}
181     {one five}               {one two five} 
182     {one three five}         {one two three five}
183     {one four five}          {one two four five} 
184     {one three four five}    {one two three four five}
185   }
186
187   15 {SELECT a FROM ft WHERE b MATCH '"neung song sahm"'} {
188     {one two three}          {one two three four}  
189     {one two three five}     {one two three four five}
190   }
191
192   16 {SELECT a FROM ft WHERE b MATCH 'hah "song sahm"'} {
193     {two three five}         {one two three five}
194     {two three four five}    {one two three four five}
195   }
196
197   17 {SELECT a FROM ft WHERE b MATCH 'song OR sahm'} {
198     {two}                     {one two}             {three}
199     {one three}               {two three}           {one two three}
200     {two four}                {one two four}        {three four}   
201     {one three four}          {two three four}      {one two three four}  
202     {two five}                {one two five}        {three five}
203     {one three five}          {two three five}      {one two three five}
204     {two four five}           {one two four five}   {three four five}
205     {one three four five}     {two three four five} {one two three four five}
206   }
207
208   18 {SELECT a FROM ft WHERE a MATCH 'three NOT two'} {
209     {three}                   {one three}           {three four}   
210     {one three four}          {three five}          {one three five}
211     {three four five}         {one three four five}
212   }
213
214   19 {SELECT a FROM ft WHERE b MATCH 'sahm NOT song'} {
215     {three}                   {one three}           {three four}   
216     {one three four}          {three five}          {one three five}
217     {three four five}         {one three four five}
218   }
219
220   20 {SELECT a FROM ft WHERE ft MATCH 'sahm NOT song'} {
221     {three}                   {one three}           {three four}   
222     {one three four}          {three five}          {one three five}
223     {three four five}         {one three four five}
224   }
225
226   21 {SELECT a FROM ft WHERE b MATCH 'neung NEAR song NEAR sahm'} {
227     {one two three}           {one two three four}  
228     {one two three five}      {one two three four five}
229   }
230
231 } {
232   set result [normal_list $result]
233   do_select_test fts3_malloc-2.$tn $sql $result
234 }
235
236 do_test fts3_malloc-3.0 {
237   execsql BEGIN
238   for {set ii 32} {$ii < 1024} {incr ii} {
239     set a [list]
240     set b [list]
241     if {$ii & 0x0001} {lappend a one   ; lappend b neung }
242     if {$ii & 0x0002} {lappend a two   ; lappend b song  }
243     if {$ii & 0x0004} {lappend a three ; lappend b sahm  }
244     if {$ii & 0x0008} {lappend a four  ; lappend b see   }
245     if {$ii & 0x0010} {lappend a five  ; lappend b hah   }
246     if {$ii & 0x0020} {lappend a six   ; lappend b hok   }
247     if {$ii & 0x0040} {lappend a seven ; lappend b jet   }
248     if {$ii & 0x0080} {lappend a eight ; lappend b bairt }
249     if {$ii & 0x0100} {lappend a nine  ; lappend b gow   }
250     if {$ii & 0x0200} {lappend a ten   ; lappend b sip   }
251     execsql { INSERT INTO ft VALUES($a, $b) }
252   }
253   execsql COMMIT
254 } {}
255 foreach {tn sql result} {
256   1 "SELECT count(*) FROM ft" {1023}
257
258   2 "SELECT a FROM ft WHERE a MATCH 'one two three four five six seven eight'" {
259      {one two three four five six seven eight}
260      {one two three four five six seven eight nine}
261      {one two three four five six seven eight ten}
262      {one two three four five six seven eight nine ten}
263   }
264
265   3 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH 'o*'} {
266     512 262144
267   }
268
269   4 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH '"two three four"'} {
270     128 66368
271   }
272 } {
273   set result [normal_list $result]
274   do_select_test fts3_malloc-3.$tn $sql $result
275 }
276
277 do_test fts3_malloc-4.0 {
278   execsql { DELETE FROM ft WHERE docid>=32 }
279 } {}
280 foreach {tn sql} {
281   1 "DELETE FROM ft WHERE ft MATCH 'one'"
282   2 "DELETE FROM ft WHERE ft MATCH 'three'"
283   3 "DELETE FROM ft WHERE ft MATCH 'five'"
284 } {
285   do_write_test fts3_malloc-4.1.$tn ft_content $sql
286 }
287 do_test fts3_malloc-4.2 {
288   execsql { SELECT a FROM ft }
289 } {two four {two four}}
290
291 do_write_test fts3_malloc-5.1 ft_content {
292   INSERT INTO ft VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken', 'cynics!')
293 }
294 do_test fts3_malloc-5.2 {
295   execsql { CREATE VIRTUAL TABLE ft8 USING fts3(x, tokenize porter) }
296 } {}
297 do_write_test fts3_malloc-5.3 ft_content {
298   INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken')
299 }
300
301
302 finish_test
303