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