OLD | NEW |
(Empty) | |
| 1 # 2014 June 17 |
| 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 # This file implements regression tests for SQLite library. The |
| 12 # focus of this script is testing the FTS5 module. |
| 13 # |
| 14 # |
| 15 |
| 16 source [file join [file dirname [info script]] fts5_common.tcl] |
| 17 set testprefix fts5ab |
| 18 |
| 19 # If SQLITE_ENABLE_FTS5 is defined, omit this file. |
| 20 ifcapable !fts5 { |
| 21 finish_test |
| 22 return |
| 23 } |
| 24 |
| 25 do_execsql_test 1.0 { |
| 26 CREATE VIRTUAL TABLE t1 USING fts5(a, b); |
| 27 INSERT INTO t1 VALUES('hello', 'world'); |
| 28 INSERT INTO t1 VALUES('one two', 'three four'); |
| 29 INSERT INTO t1(rowid, a, b) VALUES(45, 'forty', 'five'); |
| 30 } |
| 31 |
| 32 do_execsql_test 1.1 { |
| 33 SELECT * FROM t1 ORDER BY rowid DESC; |
| 34 } { forty five {one two} {three four} hello world } |
| 35 |
| 36 do_execsql_test 1.2 { |
| 37 SELECT rowid FROM t1 ORDER BY rowid DESC; |
| 38 } {45 2 1} |
| 39 |
| 40 do_execsql_test 1.3 { |
| 41 SELECT rowid FROM t1 ORDER BY rowid ASC; |
| 42 } {1 2 45} |
| 43 |
| 44 do_execsql_test 1.4 { |
| 45 SELECT * FROM t1 WHERE rowid=2; |
| 46 } {{one two} {three four}} |
| 47 |
| 48 do_execsql_test 1.5 { |
| 49 SELECT * FROM t1 WHERE rowid=2.01; |
| 50 } {} |
| 51 |
| 52 do_execsql_test 1.6 { |
| 53 SELECT * FROM t1 WHERE rowid=1.99; |
| 54 } {} |
| 55 |
| 56 #------------------------------------------------------------------------- |
| 57 |
| 58 reset_db |
| 59 do_execsql_test 2.1 { |
| 60 CREATE VIRTUAL TABLE t1 USING fts5(x); |
| 61 INSERT INTO t1(t1, rank) VALUES('pgsz', 32); |
| 62 INSERT INTO t1 VALUES('one'); |
| 63 INSERT INTO t1 VALUES('two'); |
| 64 INSERT INTO t1 VALUES('three'); |
| 65 } |
| 66 |
| 67 do_catchsql_test 2.2 { |
| 68 SELECT rowid, * FROM t1 WHERE t1 MATCH 'AND AND' |
| 69 } {1 {fts5: syntax error near "AND"}} |
| 70 |
| 71 do_execsql_test 2.3 { SELECT rowid, * FROM t1 WHERE t1 MATCH 'two' } {2 two} |
| 72 do_execsql_test 2.4 { SELECT rowid, * FROM t1 WHERE t1 MATCH 'three' } {3 three} |
| 73 do_execsql_test 2.5 { SELECT rowid, * FROM t1 WHERE t1 MATCH 'one' } {1 one} |
| 74 |
| 75 do_execsql_test 2.6 { |
| 76 INSERT INTO t1 VALUES('a b c d e f g'); |
| 77 INSERT INTO t1 VALUES('b d e a a a i'); |
| 78 INSERT INTO t1 VALUES('x y z b c c c'); |
| 79 } |
| 80 |
| 81 foreach {tn expr res} { |
| 82 1 a {5 4} |
| 83 2 b {6 5 4} |
| 84 3 c {6 4} |
| 85 4 d {5 4} |
| 86 5 e {5 4} |
| 87 6 f {4} |
| 88 7 g {4} |
| 89 8 x {6} |
| 90 9 y {6} |
| 91 10 z {6} |
| 92 } { |
| 93 do_execsql_test 2.7.$tn.1 { |
| 94 SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid DESC |
| 95 } $res |
| 96 do_execsql_test 2.7.$tn.2 { |
| 97 SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid ASC |
| 98 } [lsort -integer $res] |
| 99 } |
| 100 |
| 101 #------------------------------------------------------------------------- |
| 102 # |
| 103 reset_db |
| 104 do_execsql_test 3.0 { |
| 105 CREATE VIRTUAL TABLE t1 USING fts5(a,b); |
| 106 INSERT INTO t1(t1, rank) VALUES('pgsz', 32); |
| 107 } |
| 108 |
| 109 foreach {tn a b} { |
| 110 1 {abashed abandons abase abash abaft} {abases abased} |
| 111 2 {abasing abases abaft abated abandons} {abases abandoned} |
| 112 3 {abatement abash abash abated abase} {abasements abashing} |
| 113 4 {abaft abasements abase abasement abasing} {abasement abases} |
| 114 5 {abaft abashing abatement abash abasements} {abandons abandoning} |
| 115 6 {aback abate abasements abashes abandoned} {abasement abased} |
| 116 7 {abandons abated abased aback abandoning} {abases abandoned} |
| 117 8 {abashing abases abasement abaft abashing} {abashed abate} |
| 118 9 {abash abase abate abashing abashed} {abandon abandoned} |
| 119 10 {abate abandoning abandons abasement aback} {abandon abandoning} |
| 120 } { |
| 121 do_execsql_test 3.1.$tn.1 { INSERT INTO t1 VALUES($a, $b) } |
| 122 do_execsql_test 3.1.$tn.2 { INSERT INTO t1(t1) VALUES('integrity-check') } |
| 123 } |
| 124 |
| 125 foreach {tn expr res} { |
| 126 1 {abash} {9 5 3 1} |
| 127 2 {abase} {9 4 3 1} |
| 128 3 {abase + abash} {1} |
| 129 4 {abash + abase} {9} |
| 130 5 {abaft + abashing} {8 5} |
| 131 6 {abandon + abandoning} {10} |
| 132 7 {"abashing abases abasement abaft abashing"} {8} |
| 133 } { |
| 134 do_execsql_test 3.2.$tn { |
| 135 SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid DESC |
| 136 } $res |
| 137 } |
| 138 |
| 139 do_execsql_test 3.3 { |
| 140 SELECT rowid FROM t1 WHERE t1 MATCH 'NEAR(aback abate, 2)' |
| 141 } {6} |
| 142 |
| 143 foreach {tn expr res} { |
| 144 1 {abash} {1 3 5 9} |
| 145 2 {abase} {1 3 4 9} |
| 146 3 {abase + abash} {1} |
| 147 4 {abash + abase} {9} |
| 148 5 {abaft + abashing} {5 8} |
| 149 6 {abandon + abandoning} {10} |
| 150 7 {"abashing abases abasement abaft abashing"} {8} |
| 151 } { |
| 152 do_execsql_test 3.4.$tn { |
| 153 SELECT rowid FROM t1 WHERE t1 MATCH $expr |
| 154 } $res |
| 155 } |
| 156 |
| 157 #------------------------------------------------------------------------- |
| 158 # Documents with more than 2M tokens. |
| 159 # |
| 160 |
| 161 do_execsql_test 4.0 { |
| 162 CREATE VIRTUAL TABLE s1 USING fts5(x); |
| 163 } |
| 164 foreach {tn doc} [list \ |
| 165 1 [string repeat {a x } 1500000] \ |
| 166 2 "[string repeat {a a } 1500000] x" \ |
| 167 ] { |
| 168 do_execsql_test 4.$tn { INSERT INTO s1 VALUES($doc) } |
| 169 } |
| 170 |
| 171 do_execsql_test 4.3 { |
| 172 SELECT rowid FROM s1 WHERE s1 MATCH 'x' |
| 173 } {1 2} |
| 174 |
| 175 do_execsql_test 4.4 { |
| 176 SELECT rowid FROM s1 WHERE s1 MATCH '"a x"' |
| 177 } {1 2} |
| 178 |
| 179 #------------------------------------------------------------------------- |
| 180 # Check that a special case of segment promotion works. The case is where |
| 181 # a new segment is written to level L, but the oldest segment within level |
| 182 # (L-2) is larger than it. |
| 183 # |
| 184 do_execsql_test 5.0 { |
| 185 CREATE VIRTUAL TABLE s2 USING fts5(x); |
| 186 INSERT INTO s2(s2, rank) VALUES('pgsz', 32); |
| 187 INSERT INTO s2(s2, rank) VALUES('automerge', 0); |
| 188 } |
| 189 |
| 190 proc rnddoc {n} { |
| 191 set map [list 0 a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j] |
| 192 set doc [list] |
| 193 for {set i 0} {$i < $n} {incr i} { |
| 194 lappend doc [string map $map [format %.3d [expr int(rand()*1000)]]] |
| 195 } |
| 196 set doc |
| 197 } |
| 198 db func rnddoc rnddoc |
| 199 |
| 200 do_test 5.1 { |
| 201 for {set i 1} {$i <= 65} {incr i} { |
| 202 execsql { INSERT INTO s2 VALUES(rnddoc(10)) } |
| 203 } |
| 204 for {set i 1} {$i <= 63} {incr i} { |
| 205 execsql { DELETE FROM s2 WHERE rowid = $i } |
| 206 } |
| 207 fts5_level_segs s2 |
| 208 } {0 8} |
| 209 |
| 210 do_test 5.2 { |
| 211 execsql { |
| 212 INSERT INTO s2(s2, rank) VALUES('automerge', 8); |
| 213 } |
| 214 for {set i 0} {$i < 7} {incr i} { |
| 215 execsql { INSERT INTO s2 VALUES(rnddoc(50)) } |
| 216 } |
| 217 fts5_level_segs s2 |
| 218 } {8 0 0} |
| 219 |
| 220 # Test also the other type of segment promotion - when a new segment is written |
| 221 # that is larger than segments immediately following it. |
| 222 do_test 5.3 { |
| 223 execsql { |
| 224 DROP TABLE s2; |
| 225 CREATE VIRTUAL TABLE s2 USING fts5(x); |
| 226 INSERT INTO s2(s2, rank) VALUES('pgsz', 32); |
| 227 INSERT INTO s2(s2, rank) VALUES('automerge', 0); |
| 228 } |
| 229 |
| 230 for {set i 1} {$i <= 16} {incr i} { |
| 231 execsql { INSERT INTO s2 VALUES(rnddoc(5)) } |
| 232 } |
| 233 fts5_level_segs s2 |
| 234 } {0 1} |
| 235 |
| 236 do_test 5.4 { |
| 237 execsql { INSERT INTO s2 VALUES(rnddoc(160)) } |
| 238 fts5_level_segs s2 |
| 239 } {2 0} |
| 240 |
| 241 #------------------------------------------------------------------------- |
| 242 # |
| 243 do_execsql_test 6.0 { |
| 244 CREATE VIRTUAL TABLE s3 USING fts5(x); |
| 245 BEGIN; |
| 246 INSERT INTO s3 VALUES('a b c'); |
| 247 INSERT INTO s3 VALUES('A B C'); |
| 248 } |
| 249 |
| 250 do_execsql_test 6.1.1 { |
| 251 SELECT rowid FROM s3 WHERE s3 MATCH 'a' |
| 252 } {1 2} |
| 253 |
| 254 do_execsql_test 6.1.2 { |
| 255 SELECT rowid FROM s3 WHERE s3 MATCH 'a' ORDER BY rowid DESC |
| 256 } {2 1} |
| 257 |
| 258 do_execsql_test 6.2 { |
| 259 COMMIT; |
| 260 } |
| 261 |
| 262 do_execsql_test 6.3 { |
| 263 SELECT rowid FROM s3 WHERE s3 MATCH 'a' |
| 264 } {1 2} |
| 265 |
| 266 do_test 6.4 { |
| 267 db close |
| 268 sqlite3 db test.db |
| 269 execsql { |
| 270 BEGIN; |
| 271 INSERT INTO s3(s3) VALUES('optimize'); |
| 272 ROLLBACK; |
| 273 } |
| 274 } {} |
| 275 |
| 276 #------------------------------------------------------------------------- |
| 277 # |
| 278 set doc [string repeat "a b c " 500] |
| 279 breakpoint |
| 280 do_execsql_test 7.0 { |
| 281 CREATE VIRTUAL TABLE x1 USING fts5(x); |
| 282 INSERT INTO x1(x1, rank) VALUES('pgsz', 32); |
| 283 INSERT INTO x1 VALUES($doc); |
| 284 } |
| 285 |
| 286 |
| 287 |
| 288 finish_test |
| 289 |
OLD | NEW |