os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/fts2n.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/fts2n.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,196 @@
     1.4 +# 2007 April 26
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.
     1.7 +#
     1.8 +#*************************************************************************
     1.9 +# This file implements tests for prefix-searching in the fts2
    1.10 +# component of the SQLite library.
    1.11 +#
    1.12 +# $Id: fts2n.test,v 1.2 2007/12/13 21:54:11 drh Exp $
    1.13 +#
    1.14 +
    1.15 +set testdir [file dirname $argv0]
    1.16 +source $testdir/tester.tcl
    1.17 +
    1.18 +# If SQLITE_ENABLE_FTS2 is defined, omit this file.
    1.19 +ifcapable !fts2 {
    1.20 +  finish_test
    1.21 +  return
    1.22 +}
    1.23 +
    1.24 +# A large string to prime the pump with.
    1.25 +set text {
    1.26 +  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas
    1.27 +  iaculis mollis ipsum. Praesent rhoncus placerat justo. Duis non quam
    1.28 +  sed turpis posuere placerat. Curabitur et lorem in lorem porttitor
    1.29 +  aliquet. Pellentesque bibendum tincidunt diam. Vestibulum blandit
    1.30 +  ante nec elit. In sapien diam, facilisis eget, dictum sed, viverra
    1.31 +  at, felis. Vestibulum magna. Sed magna dolor, vestibulum rhoncus,
    1.32 +  ornare vel, vulputate sit amet, felis. Integer malesuada, tellus at
    1.33 +  luctus gravida, diam nunc porta nibh, nec imperdiet massa metus eu
    1.34 +  lectus. Aliquam nisi. Nunc fringilla nulla at lectus. Suspendisse
    1.35 +  potenti. Cum sociis natoque penatibus et magnis dis parturient
    1.36 +  montes, nascetur ridiculus mus. Pellentesque odio nulla, feugiat eu,
    1.37 +  suscipit nec, consequat quis, risus.
    1.38 +}
    1.39 +
    1.40 +db eval {
    1.41 +  CREATE VIRTUAL TABLE t1 USING fts2(c);
    1.42 +
    1.43 +  INSERT INTO t1(rowid, c) VALUES(1, $text);
    1.44 +  INSERT INTO t1(rowid, c) VALUES(2, 'Another lovely row');
    1.45 +}
    1.46 +
    1.47 +# Exact match
    1.48 +do_test fts2n-1.1 {
    1.49 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem'"
    1.50 +} {1}
    1.51 +
    1.52 +# And a prefix
    1.53 +do_test fts2n-1.2 {
    1.54 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore*'"
    1.55 +} {1}
    1.56 +
    1.57 +# Prefix includes exact match
    1.58 +do_test fts2n-1.3 {
    1.59 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lorem*'"
    1.60 +} {1}
    1.61 +
    1.62 +# Make certain everything isn't considered a prefix!
    1.63 +do_test fts2n-1.4 {
    1.64 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lore'"
    1.65 +} {}
    1.66 +
    1.67 +# Prefix across multiple rows.
    1.68 +do_test fts2n-1.5 {
    1.69 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo*'"
    1.70 +} {1 2}
    1.71 +
    1.72 +# Likewise, with multiple hits in one document.
    1.73 +do_test fts2n-1.6 {
    1.74 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'l*'"
    1.75 +} {1 2}
    1.76 +
    1.77 +# Prefix which should only hit one document.
    1.78 +do_test fts2n-1.7 {
    1.79 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lov*'"
    1.80 +} {2}
    1.81 +
    1.82 +# * not at end is dropped.
    1.83 +do_test fts2n-1.8 {
    1.84 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH 'lo *'"
    1.85 +} {}
    1.86 +
    1.87 +# Stand-alone * is dropped.
    1.88 +do_test fts2n-1.9 {
    1.89 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH '*'"
    1.90 +} {}
    1.91 +
    1.92 +# Phrase-query prefix.
    1.93 +do_test fts2n-1.10 {
    1.94 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r*\"'"
    1.95 +} {2}
    1.96 +do_test fts2n-1.11 {
    1.97 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"lovely r\"'"
    1.98 +} {}
    1.99 +
   1.100 +# Phrase query with multiple prefix matches.
   1.101 +do_test fts2n-1.12 {
   1.102 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l*\"'"
   1.103 +} {1 2}
   1.104 +
   1.105 +# Phrase query with multiple prefix matches.
   1.106 +do_test fts2n-1.13 {
   1.107 +  execsql "SELECT rowid FROM t1 WHERE t1 MATCH '\"a* l* row\"'"
   1.108 +} {2}
   1.109 +
   1.110 +
   1.111 +
   1.112 +
   1.113 +# Test across updates (and, by implication, deletes).
   1.114 +
   1.115 +# Version of text without "lorem".
   1.116 +regsub -all {[Ll]orem} $text '' ntext
   1.117 +
   1.118 +db eval {
   1.119 +  CREATE VIRTUAL TABLE t2 USING fts2(c);
   1.120 +
   1.121 +  INSERT INTO t2(rowid, c) VALUES(1, $text);
   1.122 +  INSERT INTO t2(rowid, c) VALUES(2, 'Another lovely row');
   1.123 +  UPDATE t2 SET c = $ntext WHERE rowid = 1;
   1.124 +}
   1.125 +
   1.126 +# Can't see lorem as an exact match.
   1.127 +do_test fts2n-2.1 {
   1.128 +  execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lorem'"
   1.129 +} {}
   1.130 +
   1.131 +# Can't see a prefix of lorem, either.
   1.132 +do_test fts2n-2.2 {
   1.133 +  execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lore*'"
   1.134 +} {}
   1.135 +
   1.136 +# Can see lovely in the other document.
   1.137 +do_test fts2n-2.3 {
   1.138 +  execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lo*'"
   1.139 +} {2}
   1.140 +
   1.141 +# Can still see other hits.
   1.142 +do_test fts2n-2.4 {
   1.143 +  execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'l*'"
   1.144 +} {1 2}
   1.145 +
   1.146 +# Prefix which should only hit one document.
   1.147 +do_test fts2n-2.5 {
   1.148 +  execsql "SELECT rowid FROM t2 WHERE t2 MATCH 'lov*'"
   1.149 +} {2}
   1.150 +
   1.151 +
   1.152 +
   1.153 +# Test with a segment which will have multiple levels in the tree.
   1.154 +
   1.155 +# Build a big document with lots of unique terms.
   1.156 +set bigtext $text
   1.157 +foreach c {a b c d e} {
   1.158 +  regsub -all {[A-Za-z]+} $bigtext "&$c" t
   1.159 +  append bigtext $t
   1.160 +}
   1.161 +
   1.162 +# Populate a table with many copies of the big document, so that we
   1.163 +# can test the number of hits found.  Populate $ret with the expected
   1.164 +# hit counts for each row.  offsets() returns 4 elements for every
   1.165 +# hit.  We'll have 6 hits for row 1, 1 for row 2, and 6*(2^5)==192 for
   1.166 +# $bigtext.
   1.167 +set ret {6 1}
   1.168 +db eval {
   1.169 +  BEGIN;
   1.170 +  CREATE VIRTUAL TABLE t3 USING fts2(c);
   1.171 +
   1.172 +  INSERT INTO t3(rowid, c) VALUES(1, $text);
   1.173 +  INSERT INTO t3(rowid, c) VALUES(2, 'Another lovely row');
   1.174 +}
   1.175 +for {set i 0} {$i<100} {incr i} {
   1.176 +  db eval {INSERT INTO t3(rowid, c) VALUES(3+$i, $bigtext)}
   1.177 +  lappend ret 192
   1.178 +}
   1.179 +db eval {COMMIT;}
   1.180 +
   1.181 +# Test that we get the expected number of hits.
   1.182 +do_test fts2n-3.1 {
   1.183 +  set t {}
   1.184 +  db eval {SELECT offsets(t3) as o FROM t3 WHERE t3 MATCH 'l*'} {
   1.185 +    set l [llength $o]
   1.186 +    lappend t [expr {$l/4}]
   1.187 +  }
   1.188 +  set t
   1.189 +} $ret
   1.190 +
   1.191 +# TODO(shess) It would be useful to test a couple edge cases, but I
   1.192 +# don't know if we have the precision to manage it from here at this
   1.193 +# time.  Prefix hits can cross leaves, which the code above _should_
   1.194 +# hit by virtue of size.  There are two variations on this.  If the
   1.195 +# tree is 2 levels high, the code will find the leaf-node extent
   1.196 +# directly, but if its higher, the code will have to follow two
   1.197 +# separate interior branches down the tree.  Both should be tested.
   1.198 +
   1.199 +finish_test