summaryrefslogtreecommitdiffstats
path: root/test/fts3malloc.test
blob: ed30fe24c8ab0928bae84471e7b8c9e2ad302d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# 2009 October 22
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# This file contains tests to verify that malloc() errors that occur
# within the FTS3 module code are handled correctly. 
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !fts3 { finish_test ; return }
source $testdir/malloc_common.tcl
source $testdir/fts3_common.tcl

# Ensure the lookaside buffer is disabled for these tests.
#
sqlite3 db test.db
sqlite3_db_config_lookaside db 0 0 0

set sqlite_fts3_enable_parentheses 1
set DO_MALLOC_TEST 1

# Test organization:
#
# fts3_malloc-1.*: Test OOM during CREATE and DROP table statements.
# fts3_malloc-2.*: Test OOM during SELECT operations.
# fts3_malloc-3.*: Test OOM during SELECT operations with a larger database.
# fts3_malloc-4.*: Test OOM during database write operations.
# fts3_malloc-5.*: Test that a couple of memory leaks that could follow
#                  OOM in tokenizer code have been fixed.
#


proc normal_list {l} {
  set ret [list]
  foreach elem $l {lappend ret $elem}
  set ret
}

do_write_test fts3_malloc-1.1 sqlite_master {
  CREATE VIRTUAL TABLE ft1 USING fts3(a, b)
}
do_write_test fts3_malloc-1.2 sqlite_master {
  CREATE VIRTUAL TABLE ft2 USING fts3([a], [b]);
}
do_write_test fts3_malloc-1.3 sqlite_master {
  CREATE VIRTUAL TABLE ft3 USING fts3('a', "b");
}
do_write_test fts3_malloc-1.4 sqlite_master {
  CREATE VIRTUAL TABLE ft4 USING fts3(`a`, 'fred''s column');
}
do_error_test fts3_malloc-1.5 {
  CREATE VIRTUAL TABLE ft5 USING fts3(a, b, tokenize unknown)
} {unknown tokenizer: unknown}
do_write_test fts3_malloc-1.6 sqlite_master {
  CREATE VIRTUAL TABLE ft6 USING fts3(a, b, tokenize porter)
}
do_write_test fts3_malloc-1.7 sqlite_master {
  CREATE VIRTUAL TABLE ft7 USING fts4(a, b, notindexed=b)
}

# Test the xConnect/xDisconnect methods:
#db eval { ATTACH 'test2.db' AS aux }
#do_write_test fts3_malloc-1.6 aux.sqlite_master {
#  CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
#}
#do_write_test fts3_malloc-1.6 aux.sqlite_master {
#  CREATE VIRTUAL TABLE aux.ft7 USING fts3(a, b, c);
#}



do_test fts3_malloc-2.0 {
  execsql { 
    DROP TABLE ft1;
    DROP TABLE ft2;
    DROP TABLE ft3;
    DROP TABLE ft4;
    DROP TABLE ft6;
    DROP TABLE ft7;
  }
  execsql { CREATE VIRTUAL TABLE ft USING fts3(a, b) }
  for {set ii 1} {$ii < 32} {incr ii} {
    set a [list]
    set b [list]
    if {$ii & 0x01} {lappend a one   ; lappend b neung}
    if {$ii & 0x02} {lappend a two   ; lappend b song }
    if {$ii & 0x04} {lappend a three ; lappend b sahm }
    if {$ii & 0x08} {lappend a four  ; lappend b see  }
    if {$ii & 0x10} {lappend a five  ; lappend b hah  }
    execsql { INSERT INTO ft VALUES($a, $b) }
  }
} {}

foreach {tn sql result} {
  1 "SELECT count(*) FROM sqlite_master" {5}
  2 "SELECT * FROM ft WHERE docid = 1"   {one neung}
  3 "SELECT * FROM ft WHERE docid = 2"   {two song}
  4 "SELECT * FROM ft WHERE docid = 3"   {{one two} {neung song}}

  5 "SELECT a FROM ft" {
    {one}                     {two}                 {one two}
    {three}                   {one three}           {two three}     
    {one two three}           {four}                {one four} 
    {two four}                {one two four}        {three four}   
    {one three four}          {two three four}      {one two three four}  
    {five}                    {one five}            {two five}            
    {one two five}            {three five}          {one three five} 
    {two three five}          {one two three five}  {four five}
    {one four five}           {two four five}       {one two four five}
    {three four five}         {one three four five} {two three four five}
    {one two three four five}
  }

  6 "SELECT a FROM ft WHERE a MATCH 'one'" {
    {one} {one two} {one three} {one two three}
    {one four} {one two four} {one three four} {one two three four}
    {one five} {one two five} {one three five} {one two three five}
    {one four five} {one two four five} 
    {one three four five} {one two three four five}
  }

  7 "SELECT a FROM ft WHERE a MATCH 'o*'" {
    {one} {one two} {one three} {one two three}
    {one four} {one two four} {one three four} {one two three four}
    {one five} {one two five} {one three five} {one two three five}
    {one four five} {one two four five} 
    {one three four five} {one two three four five}
  }

  8 "SELECT a FROM ft WHERE a MATCH 'o* t*'" {
    {one two}             {one three}           {one two three} 
    {one two four}        {one three four}      {one two three four} 
    {one two five}        {one three five}      {one two three five} 
    {one two four five}   {one three four five} {one two three four five}
  }

  9 "SELECT a FROM ft WHERE a MATCH '\"o* t*\"'" {
    {one two}             {one three}           {one two three} 
    {one two four}        {one three four}      {one two three four} 
    {one two five}        {one three five}      {one two three five} 
    {one two four five}   {one three four five} {one two three four five}
  }

  10 {SELECT a FROM ft WHERE a MATCH '"o* f*"'} {
    {one four}            {one five}            {one four five}
  }

  11 {SELECT a FROM ft WHERE a MATCH '"one two three"'} {
    {one two three}
    {one two three four}  
    {one two three five}
    {one two three four five}
  }

  12 {SELECT a FROM ft WHERE a MATCH '"two three four"'} {
    {two three four}
    {one two three four}
    {two three four five}
    {one two three four five}
  }

  12 {SELECT a FROM ft WHERE a MATCH '"two three" five'} {
    {two three five}         {one two three five}
    {two three four five}    {one two three four five}
  }

  13 {SELECT a FROM ft WHERE ft MATCH '"song sahm" hah'} {
    {two three five}         {one two three five}
    {two three four five}    {one two three four five}
  }

  14 {SELECT a FROM ft WHERE b MATCH 'neung'} {
    {one}                    {one two} 
    {one three}              {one two three}
    {one four}               {one two four} 
    {one three four}         {one two three four}
    {one five}               {one two five} 
    {one three five}         {one two three five}
    {one four five}          {one two four five} 
    {one three four five}    {one two three four five}
  }

  15 {SELECT a FROM ft WHERE b MATCH '"neung song sahm"'} {
    {one two three}          {one two three four}  
    {one two three five}     {one two three four five}
  }

  16 {SELECT a FROM ft WHERE b MATCH 'hah "song sahm"'} {
    {two three five}         {one two three five}
    {two three four five}    {one two three four five}
  }

  17 {SELECT a FROM ft WHERE b MATCH 'song OR sahm'} {
    {two}                     {one two}             {three}
    {one three}               {two three}           {one two three}
    {two four}                {one two four}        {three four}   
    {one three four}          {two three four}      {one two three four}  
    {two five}                {one two five}        {three five}
    {one three five}          {two three five}      {one two three five}
    {two four five}           {one two four five}   {three four five}
    {one three four five}     {two three four five} {one two three four five}
  }

  18 {SELECT a FROM ft WHERE a MATCH 'three NOT two'} {
    {three}                   {one three}           {three four}   
    {one three four}          {three five}          {one three five}
    {three four five}         {one three four five}
  }

  19 {SELECT a FROM ft WHERE b MATCH 'sahm NOT song'} {
    {three}                   {one three}           {three four}   
    {one three four}          {three five}          {one three five}
    {three four five}         {one three four five}
  }

  20 {SELECT a FROM ft WHERE ft MATCH 'sahm NOT song'} {
    {three}                   {one three}           {three four}   
    {one three four}          {three five}          {one three five}
    {three four five}         {one three four five}
  }

  21 {SELECT a FROM ft WHERE b MATCH 'neung NEAR song NEAR sahm'} {
    {one two three}           {one two three four}  
    {one two three five}      {one two three four five}
  }

} {
  set result [normal_list $result]
  do_select_test fts3_malloc-2.$tn $sql $result
}

do_test fts3_malloc-3.0 {
  execsql BEGIN
  for {set ii 32} {$ii < 1024} {incr ii} {
    set a [list]
    set b [list]
    if {$ii & 0x0001} {lappend a one   ; lappend b neung }
    if {$ii & 0x0002} {lappend a two   ; lappend b song  }
    if {$ii & 0x0004} {lappend a three ; lappend b sahm  }
    if {$ii & 0x0008} {lappend a four  ; lappend b see   }
    if {$ii & 0x0010} {lappend a five  ; lappend b hah   }
    if {$ii & 0x0020} {lappend a six   ; lappend b hok   }
    if {$ii & 0x0040} {lappend a seven ; lappend b jet   }
    if {$ii & 0x0080} {lappend a eight ; lappend b bairt }
    if {$ii & 0x0100} {lappend a nine  ; lappend b gow   }
    if {$ii & 0x0200} {lappend a ten   ; lappend b sip   }
    execsql { INSERT INTO ft VALUES($a, $b) }
  }
  execsql COMMIT
} {}
foreach {tn sql result} {
  1 "SELECT count(*) FROM ft" {1023}

  2 "SELECT a FROM ft WHERE a MATCH 'one two three four five six seven eight'" {
     {one two three four five six seven eight}
     {one two three four five six seven eight nine}
     {one two three four five six seven eight ten}
     {one two three four five six seven eight nine ten}
  }

  3 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH 'o*'} {
    512 262144
  }

  4 {SELECT count(*), sum(docid) FROM ft WHERE a MATCH '"two three four"'} {
    128 66368
  }
} {
  set result [normal_list $result]
  do_select_test fts3_malloc-3.$tn $sql $result
}

do_test fts3_malloc-4.0 {
  execsql { DELETE FROM ft WHERE docid>=32 }
} {}
foreach {tn sql} {
  1 "DELETE FROM ft WHERE ft MATCH 'one'"
  2 "DELETE FROM ft WHERE ft MATCH 'three'"
  3 "DELETE FROM ft WHERE ft MATCH 'five'"
} {
  do_write_test fts3_malloc-4.1.$tn ft_content $sql
}
do_test fts3_malloc-4.2 {
  execsql { SELECT a FROM ft }
} {two four {two four}}

do_write_test fts3_malloc-5.1 ft_content {
  INSERT INTO ft VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken', 'cynics!')
}
do_test fts3_malloc-5.2 {
  execsql { CREATE VIRTUAL TABLE ft8 USING fts3(x, tokenize porter) }
} {}

do_write_test fts3_malloc-5.3 ft_content {
  INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken')
}


finish_test