os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/fts2p.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/fts2p.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,357 @@
     1.4 +# 2008 June 26
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#*************************************************************************
    1.14 +# This file exercises some new testing functions in the FTS2 module,
    1.15 +# and then uses them to do some basic tests that FTS2 is internally
    1.16 +# working as expected.
    1.17 +#
    1.18 +# $Id: fts2p.test,v 1.1 2008/07/22 23:32:28 shess Exp $
    1.19 +#
    1.20 +
    1.21 +set testdir [file dirname $argv0]
    1.22 +source $testdir/tester.tcl
    1.23 +
    1.24 +# If SQLITE_ENABLE_FTS2 is not defined, omit this file.
    1.25 +ifcapable !fts2 {
    1.26 +  finish_test
    1.27 +  return
    1.28 +}
    1.29 +
    1.30 +#*************************************************************************
    1.31 +# Probe to see if support for these functions is compiled in.
    1.32 +# TODO(shess): Change main.mk to do the right thing and remove this test.
    1.33 +db eval {
    1.34 +  DROP TABLE IF EXISTS t1;
    1.35 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
    1.36 +  INSERT INTO t1 (rowid, c) VALUES (1, 'x');
    1.37 +}
    1.38 +
    1.39 +set s {SELECT dump_terms(t1, 1) FROM t1 LIMIT 1}
    1.40 +set r {1 {unable to use function dump_terms in the requested context}}
    1.41 +if {[catchsql $s]==$r} {
    1.42 +  finish_test
    1.43 +  return
    1.44 +}
    1.45 +
    1.46 +#*************************************************************************
    1.47 +# Test that the new functions give appropriate errors.
    1.48 +do_test fts2p-0.0 {
    1.49 +  catchsql {
    1.50 +    SELECT dump_terms(t1, 1) FROM t1 LIMIT 1;
    1.51 +  }
    1.52 +} {1 {dump_terms: incorrect arguments}}
    1.53 +
    1.54 +do_test fts2p-0.1 {
    1.55 +  catchsql {
    1.56 +    SELECT dump_terms(t1, 0, 0, 0) FROM t1 LIMIT 1;
    1.57 +  }
    1.58 +} {1 {dump_terms: incorrect arguments}}
    1.59 +
    1.60 +do_test fts2p-0.2 {
    1.61 +  catchsql {
    1.62 +    SELECT dump_terms(1, t1) FROM t1 LIMIT 1;
    1.63 +  }
    1.64 +} {1 {unable to use function dump_terms in the requested context}}
    1.65 +
    1.66 +do_test fts2p-0.3 {
    1.67 +  catchsql {
    1.68 +    SELECT dump_terms(t1, 16, 16) FROM t1 LIMIT 1;
    1.69 +  }
    1.70 +} {1 {dump_terms: segment not found}}
    1.71 +
    1.72 +do_test fts2p-0.4 {
    1.73 +  catchsql {
    1.74 +    SELECT dump_doclist(t1) FROM t1 LIMIT 1;
    1.75 +  }
    1.76 +} {1 {dump_doclist: incorrect arguments}}
    1.77 +
    1.78 +do_test fts2p-0.5 {
    1.79 +  catchsql {
    1.80 +    SELECT dump_doclist(t1, NULL) FROM t1 LIMIT 1;
    1.81 +  }
    1.82 +} {1 {dump_doclist: empty second argument}}
    1.83 +
    1.84 +do_test fts2p-0.6 {
    1.85 +  catchsql {
    1.86 +    SELECT dump_doclist(t1, '') FROM t1 LIMIT 1;
    1.87 +  }
    1.88 +} {1 {dump_doclist: empty second argument}}
    1.89 +
    1.90 +do_test fts2p-0.7 {
    1.91 +  catchsql {
    1.92 +    SELECT dump_doclist(t1, 'a', 0) FROM t1 LIMIT 1;
    1.93 +  }
    1.94 +} {1 {dump_doclist: incorrect arguments}}
    1.95 +
    1.96 +do_test fts2p-0.8 {
    1.97 +  catchsql {
    1.98 +    SELECT dump_doclist(t1, 'a', 0, 0, 0) FROM t1 LIMIT 1;
    1.99 +  }
   1.100 +} {1 {dump_doclist: incorrect arguments}}
   1.101 +
   1.102 +do_test fts2p-0.9 {
   1.103 +  catchsql {
   1.104 +    SELECT dump_doclist(t1, 'a', 16, 16) FROM t1 LIMIT 1;
   1.105 +  }
   1.106 +} {1 {dump_doclist: segment not found}}
   1.107 +
   1.108 +#*************************************************************************
   1.109 +# Utility function to check for the expected terms in the segment
   1.110 +# level/index.  _all version does same but for entire index.
   1.111 +proc check_terms {test level index terms} {
   1.112 +  # TODO(shess): Figure out why uplevel in do_test can't catch
   1.113 +  # $level and $index directly.
   1.114 +  set ::level $level
   1.115 +  set ::index $index
   1.116 +  do_test $test.terms {
   1.117 +    execsql {
   1.118 +      SELECT dump_terms(t1, $::level, $::index) FROM t1 LIMIT 1;
   1.119 +    }
   1.120 +  } [list $terms]
   1.121 +}
   1.122 +proc check_terms_all {test terms} {
   1.123 +  do_test $test.terms {
   1.124 +    execsql {
   1.125 +      SELECT dump_terms(t1) FROM t1 LIMIT 1;
   1.126 +    }
   1.127 +  } [list $terms]
   1.128 +}
   1.129 +
   1.130 +# Utility function to check for the expected doclist for the term in
   1.131 +# segment level/index.  _all version does same for entire index.
   1.132 +proc check_doclist {test level index term doclist} {
   1.133 +  # TODO(shess): Again, why can't the non-:: versions work?
   1.134 +  set ::term $term
   1.135 +  set ::level $level
   1.136 +  set ::index $index
   1.137 +  do_test $test {
   1.138 +    execsql {
   1.139 +      SELECT dump_doclist(t1, $::term, $::level, $::index) FROM t1 LIMIT 1;
   1.140 +    }
   1.141 +  } [list $doclist]
   1.142 +}
   1.143 +proc check_doclist_all {test term doclist} {
   1.144 +  set ::term $term
   1.145 +  do_test $test {
   1.146 +    execsql {
   1.147 +      SELECT dump_doclist(t1, $::term) FROM t1 LIMIT 1;
   1.148 +    }
   1.149 +  } [list $doclist]
   1.150 +}
   1.151 +
   1.152 +#*************************************************************************
   1.153 +# Test the segments resulting from straight-forward inserts.
   1.154 +db eval {
   1.155 +  DROP TABLE IF EXISTS t1;
   1.156 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
   1.157 +  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
   1.158 +  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
   1.159 +  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
   1.160 +}
   1.161 +
   1.162 +# Check for expected segments and expected matches.
   1.163 +do_test fts2p-1.0.segments {
   1.164 +  execsql {
   1.165 +    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
   1.166 +  }
   1.167 +} {0 0 0 1 0 2}
   1.168 +do_test fts2p-1.0.matches {
   1.169 +  execsql {
   1.170 +    SELECT OFFSETS(t1) FROM t1
   1.171 +     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
   1.172 +  }
   1.173 +} [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
   1.174 +        {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
   1.175 +        {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
   1.176 +
   1.177 +# Check the specifics of the segments constructed.
   1.178 +# Logical view of entire index.
   1.179 +check_terms_all   fts2p-1.0.1   {a is test that this was}
   1.180 +check_doclist_all fts2p-1.0.1.1 a {[1 0[2]] [2 0[2]] [3 0[2]]}
   1.181 +check_doclist_all fts2p-1.0.1.2 is {[1 0[1]] [3 0[1]]}
   1.182 +check_doclist_all fts2p-1.0.1.3 test {[1 0[3]] [2 0[3]] [3 0[3]]}
   1.183 +check_doclist_all fts2p-1.0.1.4 that {[2 0[0]]}
   1.184 +check_doclist_all fts2p-1.0.1.5 this {[1 0[0]] [3 0[0]]}
   1.185 +check_doclist_all fts2p-1.0.1.6 was {[2 0[1]]}
   1.186 +
   1.187 +# Segment 0,0
   1.188 +check_terms   fts2p-1.0.2   0 0 {a is test this}
   1.189 +check_doclist fts2p-1.0.2.1 0 0 a {[1 0[2]]}
   1.190 +check_doclist fts2p-1.0.2.2 0 0 is {[1 0[1]]}
   1.191 +check_doclist fts2p-1.0.2.3 0 0 test {[1 0[3]]}
   1.192 +check_doclist fts2p-1.0.2.4 0 0 this {[1 0[0]]}
   1.193 +
   1.194 +# Segment 0,1
   1.195 +check_terms   fts2p-1.0.3   0 1 {a test that was}
   1.196 +check_doclist fts2p-1.0.3.1 0 1 a {[2 0[2]]}
   1.197 +check_doclist fts2p-1.0.3.2 0 1 test {[2 0[3]]}
   1.198 +check_doclist fts2p-1.0.3.3 0 1 that {[2 0[0]]}
   1.199 +check_doclist fts2p-1.0.3.4 0 1 was {[2 0[1]]}
   1.200 +
   1.201 +# Segment 0,2
   1.202 +check_terms   fts2p-1.0.4   0 2 {a is test this}
   1.203 +check_doclist fts2p-1.0.4.1 0 2 a {[3 0[2]]}
   1.204 +check_doclist fts2p-1.0.4.2 0 2 is {[3 0[1]]}
   1.205 +check_doclist fts2p-1.0.4.3 0 2 test {[3 0[3]]}
   1.206 +check_doclist fts2p-1.0.4.4 0 2 this {[3 0[0]]}
   1.207 +
   1.208 +#*************************************************************************
   1.209 +# Test the segments resulting from inserts followed by a delete.
   1.210 +db eval {
   1.211 +  DROP TABLE IF EXISTS t1;
   1.212 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
   1.213 +  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
   1.214 +  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
   1.215 +  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
   1.216 +  DELETE FROM t1 WHERE rowid = 1;
   1.217 +}
   1.218 +
   1.219 +do_test fts2p-1.1.segments {
   1.220 +  execsql {
   1.221 +    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
   1.222 +  }
   1.223 +} {0 0 0 1 0 2 0 3}
   1.224 +do_test fts2p-1.1.matches {
   1.225 +  execsql {
   1.226 +    SELECT OFFSETS(t1) FROM t1
   1.227 +     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
   1.228 +  }
   1.229 +} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}}
   1.230 +
   1.231 +check_terms_all fts2p-1.1.1 {a is test that this was}
   1.232 +check_doclist_all fts2p-1.1.1.1 a {[2 0[2]] [3 0[2]]}
   1.233 +check_doclist_all fts2p-1.1.1.2 is {[3 0[1]]}
   1.234 +check_doclist_all fts2p-1.1.1.3 test {[2 0[3]] [3 0[3]]}
   1.235 +check_doclist_all fts2p-1.1.1.4 that {[2 0[0]]}
   1.236 +check_doclist_all fts2p-1.1.1.5 this {[3 0[0]]}
   1.237 +check_doclist_all fts2p-1.1.1.6 was {[2 0[1]]}
   1.238 +
   1.239 +check_terms fts2p-1.1.2 0 0 {a is test this}
   1.240 +check_doclist fts2p-1.1.2.1 0 0 a {[1 0[2]]}
   1.241 +check_doclist fts2p-1.1.2.2 0 0 is {[1 0[1]]}
   1.242 +check_doclist fts2p-1.1.2.3 0 0 test {[1 0[3]]}
   1.243 +check_doclist fts2p-1.1.2.4 0 0 this {[1 0[0]]}
   1.244 +
   1.245 +check_terms fts2p-1.1.3 0 1 {a test that was}
   1.246 +check_doclist fts2p-1.1.3.1 0 1 a {[2 0[2]]}
   1.247 +check_doclist fts2p-1.1.3.2 0 1 test {[2 0[3]]}
   1.248 +check_doclist fts2p-1.1.3.3 0 1 that {[2 0[0]]}
   1.249 +check_doclist fts2p-1.1.3.4 0 1 was {[2 0[1]]}
   1.250 +
   1.251 +check_terms fts2p-1.1.4 0 2 {a is test this}
   1.252 +check_doclist fts2p-1.1.4.1 0 2 a {[3 0[2]]}
   1.253 +check_doclist fts2p-1.1.4.2 0 2 is {[3 0[1]]}
   1.254 +check_doclist fts2p-1.1.4.3 0 2 test {[3 0[3]]}
   1.255 +check_doclist fts2p-1.1.4.4 0 2 this {[3 0[0]]}
   1.256 +
   1.257 +check_terms fts2p-1.1.5 0 3 {a is test this}
   1.258 +check_doclist fts2p-1.1.5.1 0 3 a {[1]}
   1.259 +check_doclist fts2p-1.1.5.2 0 3 is {[1]}
   1.260 +check_doclist fts2p-1.1.5.3 0 3 test {[1]}
   1.261 +check_doclist fts2p-1.1.5.4 0 3 this {[1]}
   1.262 +
   1.263 +#*************************************************************************
   1.264 +# Test results when all references to certain tokens are deleted.
   1.265 +db eval {
   1.266 +  DROP TABLE IF EXISTS t1;
   1.267 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
   1.268 +  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
   1.269 +  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
   1.270 +  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
   1.271 +  DELETE FROM t1 WHERE rowid IN (1,3);
   1.272 +}
   1.273 +
   1.274 +# Still 4 segments because 0,3 will contain deletes for rowid 1 and 3.
   1.275 +do_test fts2p-1.2.segments {
   1.276 +  execsql {
   1.277 +    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
   1.278 +  }
   1.279 +} {0 0 0 1 0 2 0 3}
   1.280 +do_test fts2p-1.2.matches {
   1.281 +  execsql {
   1.282 +    SELECT OFFSETS(t1) FROM t1
   1.283 +     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
   1.284 +  }
   1.285 +} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
   1.286 +
   1.287 +check_terms_all fts2p-1.2.1 {a is test that this was}
   1.288 +check_doclist_all fts2p-1.2.1.1 a {[2 0[2]]}
   1.289 +check_doclist_all fts2p-1.2.1.2 is {}
   1.290 +check_doclist_all fts2p-1.2.1.3 test {[2 0[3]]}
   1.291 +check_doclist_all fts2p-1.2.1.4 that {[2 0[0]]}
   1.292 +check_doclist_all fts2p-1.2.1.5 this {}
   1.293 +check_doclist_all fts2p-1.2.1.6 was {[2 0[1]]}
   1.294 +
   1.295 +check_terms fts2p-1.2.2 0 0 {a is test this}
   1.296 +check_doclist fts2p-1.2.2.1 0 0 a {[1 0[2]]}
   1.297 +check_doclist fts2p-1.2.2.2 0 0 is {[1 0[1]]}
   1.298 +check_doclist fts2p-1.2.2.3 0 0 test {[1 0[3]]}
   1.299 +check_doclist fts2p-1.2.2.4 0 0 this {[1 0[0]]}
   1.300 +
   1.301 +check_terms fts2p-1.2.3 0 1 {a test that was}
   1.302 +check_doclist fts2p-1.2.3.1 0 1 a {[2 0[2]]}
   1.303 +check_doclist fts2p-1.2.3.2 0 1 test {[2 0[3]]}
   1.304 +check_doclist fts2p-1.2.3.3 0 1 that {[2 0[0]]}
   1.305 +check_doclist fts2p-1.2.3.4 0 1 was {[2 0[1]]}
   1.306 +
   1.307 +check_terms fts2p-1.2.4 0 2 {a is test this}
   1.308 +check_doclist fts2p-1.2.4.1 0 2 a {[3 0[2]]}
   1.309 +check_doclist fts2p-1.2.4.2 0 2 is {[3 0[1]]}
   1.310 +check_doclist fts2p-1.2.4.3 0 2 test {[3 0[3]]}
   1.311 +check_doclist fts2p-1.2.4.4 0 2 this {[3 0[0]]}
   1.312 +
   1.313 +check_terms fts2p-1.2.5 0 3 {a is test this}
   1.314 +check_doclist fts2p-1.2.5.1 0 3 a {[1] [3]}
   1.315 +check_doclist fts2p-1.2.5.2 0 3 is {[1] [3]}
   1.316 +check_doclist fts2p-1.2.5.3 0 3 test {[1] [3]}
   1.317 +check_doclist fts2p-1.2.5.4 0 3 this {[1] [3]}
   1.318 +
   1.319 +#*************************************************************************
   1.320 +# Test results when everything is optimized manually.
   1.321 +db eval {
   1.322 +  DROP TABLE IF EXISTS t1;
   1.323 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
   1.324 +  INSERT INTO t1 (rowid, c) VALUES (1, 'This is a test');
   1.325 +  INSERT INTO t1 (rowid, c) VALUES (2, 'That was a test');
   1.326 +  INSERT INTO t1 (rowid, c) VALUES (3, 'This is a test');
   1.327 +  DELETE FROM t1 WHERE rowid IN (1,3);
   1.328 +  DROP TABLE IF EXISTS t1old;
   1.329 +  ALTER TABLE t1 RENAME TO t1old;
   1.330 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
   1.331 +  INSERT INTO t1 (rowid, c) SELECT rowid, c FROM t1old;
   1.332 +  DROP TABLE t1old;
   1.333 +}
   1.334 +
   1.335 +# Should be a single optimal segment with the same logical results.
   1.336 +do_test fts2p-1.3.segments {
   1.337 +  execsql {
   1.338 +    SELECT level, idx FROM t1_segdir ORDER BY level, idx;
   1.339 +  }
   1.340 +} {0 0}
   1.341 +do_test fts2p-1.3.matches {
   1.342 +  execsql {
   1.343 +    SELECT OFFSETS(t1) FROM t1
   1.344 +     WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY rowid;
   1.345 +  }
   1.346 +} {{0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4}}
   1.347 +
   1.348 +check_terms_all fts2p-1.3.1 {a test that was}
   1.349 +check_doclist_all fts2p-1.3.1.1 a {[2 0[2]]}
   1.350 +check_doclist_all fts2p-1.3.1.2 test {[2 0[3]]}
   1.351 +check_doclist_all fts2p-1.3.1.3 that {[2 0[0]]}
   1.352 +check_doclist_all fts2p-1.3.1.4 was {[2 0[1]]}
   1.353 +
   1.354 +check_terms fts2p-1.3.2 0 0 {a test that was}
   1.355 +check_doclist fts2p-1.3.2.1 0 0 a {[2 0[2]]}
   1.356 +check_doclist fts2p-1.3.2.2 0 0 test {[2 0[3]]}
   1.357 +check_doclist fts2p-1.3.2.3 0 0 that {[2 0[0]]}
   1.358 +check_doclist fts2p-1.3.2.4 0 0 was {[2 0[1]]}
   1.359 +
   1.360 +finish_test