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