xref: /sqlite-3.40.0/test/fts2b.test (revision 8e734e35)
1*8e734e35Sshess# 2006 September 13
2*8e734e35Sshess#
3*8e734e35Sshess# The author disclaims copyright to this source code.  In place of
4*8e734e35Sshess# a legal notice, here is a blessing:
5*8e734e35Sshess#
6*8e734e35Sshess#    May you do good and not evil.
7*8e734e35Sshess#    May you find forgiveness for yourself and forgive others.
8*8e734e35Sshess#    May you share freely, never taking more than you give.
9*8e734e35Sshess#
10*8e734e35Sshess#*************************************************************************
11*8e734e35Sshess# This file implements regression tests for SQLite library.  The
12*8e734e35Sshess# focus of this script is testing the FTS2 module.
13*8e734e35Sshess#
14*8e734e35Sshess# $Id: fts2b.test,v 1.1 2006/10/19 23:36:26 shess Exp $
15*8e734e35Sshess#
16*8e734e35Sshess
17*8e734e35Sshessset testdir [file dirname $argv0]
18*8e734e35Sshesssource $testdir/tester.tcl
19*8e734e35Sshess
20*8e734e35Sshess# If SQLITE_ENABLE_FTS2 is defined, omit this file.
21*8e734e35Sshessifcapable !fts2 {
22*8e734e35Sshess  finish_test
23*8e734e35Sshess  return
24*8e734e35Sshess}
25*8e734e35Sshess
26*8e734e35Sshess# Fill the full-text index "t1" with phrases in english, spanish,
27*8e734e35Sshess# and german.  For the i-th row, fill in the names for the bits
28*8e734e35Sshess# that are set in the value of i.  The least significant bit is
29*8e734e35Sshess# 1.  For example,  the value 5 is 101 in binary which will be
30*8e734e35Sshess# converted to "one three" in english.
31*8e734e35Sshess#
32*8e734e35Sshessproc fill_multilanguage_fulltext_t1 {} {
33*8e734e35Sshess  set english {one two three four five}
34*8e734e35Sshess  set spanish {un dos tres cuatro cinco}
35*8e734e35Sshess  set german {eine zwei drei vier funf}
36*8e734e35Sshess
37*8e734e35Sshess  for {set i 1} {$i<=31} {incr i} {
38*8e734e35Sshess    set cmd "INSERT INTO t1 VALUES"
39*8e734e35Sshess    set vset {}
40*8e734e35Sshess    foreach lang {english spanish german} {
41*8e734e35Sshess      set words {}
42*8e734e35Sshess      for {set j 0; set k 1} {$j<5} {incr j; incr k $k} {
43*8e734e35Sshess        if {$k&$i} {lappend words [lindex [set $lang] $j]}
44*8e734e35Sshess      }
45*8e734e35Sshess      lappend vset "'$words'"
46*8e734e35Sshess    }
47*8e734e35Sshess    set sql "INSERT INTO t1(english,spanish,german) VALUES([join $vset ,])"
48*8e734e35Sshess    # puts $sql
49*8e734e35Sshess    db eval $sql
50*8e734e35Sshess  }
51*8e734e35Sshess}
52*8e734e35Sshess
53*8e734e35Sshess# Construct a full-text search table containing five keywords:
54*8e734e35Sshess# one, two, three, four, and five, in various combinations.  The
55*8e734e35Sshess# rowid for each will be a bitmask for the elements it contains.
56*8e734e35Sshess#
57*8e734e35Sshessdb eval {
58*8e734e35Sshess  CREATE VIRTUAL TABLE t1 USING fts2(english,spanish,german);
59*8e734e35Sshess}
60*8e734e35Sshessfill_multilanguage_fulltext_t1
61*8e734e35Sshess
62*8e734e35Sshessdo_test fts2b-1.1 {
63*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE english MATCH 'one'}
64*8e734e35Sshess} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31}
65*8e734e35Sshessdo_test fts2b-1.2 {
66*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE spanish MATCH 'one'}
67*8e734e35Sshess} {}
68*8e734e35Sshessdo_test fts2b-1.3 {
69*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE german MATCH 'one'}
70*8e734e35Sshess} {}
71*8e734e35Sshessdo_test fts2b-1.4 {
72*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one'}
73*8e734e35Sshess} {1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31}
74*8e734e35Sshessdo_test fts2b-1.5 {
75*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'one dos drei'}
76*8e734e35Sshess} {7 15 23 31}
77*8e734e35Sshessdo_test fts2b-1.6 {
78*8e734e35Sshess  execsql {SELECT english, spanish, german FROM t1 WHERE rowid=1}
79*8e734e35Sshess} {one un eine}
80*8e734e35Sshessdo_test fts2b-1.7 {
81*8e734e35Sshess  execsql {SELECT rowid FROM t1 WHERE t1 MATCH '"one un"'}
82*8e734e35Sshess} {}
83*8e734e35Sshess
84*8e734e35Sshessdo_test fts2b-2.1 {
85*8e734e35Sshess  execsql {
86*8e734e35Sshess    CREATE VIRTUAL TABLE t2 USING fts2(from,to);
87*8e734e35Sshess    INSERT INTO t2([from],[to]) VALUES ('one two three', 'four five six');
88*8e734e35Sshess    SELECT [from], [to] FROM t2
89*8e734e35Sshess  }
90*8e734e35Sshess} {{one two three} {four five six}}
91*8e734e35Sshess
92*8e734e35Sshess
93*8e734e35Sshess# Compute an SQL string that contains the words one, two, three,... to
94*8e734e35Sshess# describe bits set in the value $i.  Only the lower 5 bits are examined.
95*8e734e35Sshess#
96*8e734e35Sshessproc wordset {i} {
97*8e734e35Sshess  set x {}
98*8e734e35Sshess  for {set j 0; set k 1} {$j<5} {incr j; incr k $k} {
99*8e734e35Sshess    if {$k&$i} {lappend x [lindex {one two three four five} $j]}
100*8e734e35Sshess  }
101*8e734e35Sshess  return '$x'
102*8e734e35Sshess}
103*8e734e35Sshess
104*8e734e35Sshess# Create a new FTS table with three columns:
105*8e734e35Sshess#
106*8e734e35Sshess#    norm:      words for the bits of rowid
107*8e734e35Sshess#    plusone:   words for the bits of rowid+1
108*8e734e35Sshess#    invert:    words for the bits of ~rowid
109*8e734e35Sshess#
110*8e734e35Sshessdb eval {
111*8e734e35Sshess   CREATE VIRTUAL TABLE t4 USING fts2([norm],'plusone',"invert");
112*8e734e35Sshess}
113*8e734e35Sshessfor {set i 1} {$i<=15} {incr i} {
114*8e734e35Sshess  set vset [list [wordset $i] [wordset [expr {$i+1}]] [wordset [expr {~$i}]]]
115*8e734e35Sshess  db eval "INSERT INTO t4(norm,plusone,invert) VALUES([join $vset ,]);"
116*8e734e35Sshess}
117*8e734e35Sshess
118*8e734e35Sshessdo_test fts2b-4.1 {
119*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one'}
120*8e734e35Sshess} {1 3 5 7 9 11 13 15}
121*8e734e35Sshessdo_test fts2b-4.2 {
122*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE norm MATCH 'one'}
123*8e734e35Sshess} {1 3 5 7 9 11 13 15}
124*8e734e35Sshessdo_test fts2b-4.3 {
125*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'one'}
126*8e734e35Sshess} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15}
127*8e734e35Sshessdo_test fts2b-4.4 {
128*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:one'}
129*8e734e35Sshess} {2 4 6 8 10 12 14}
130*8e734e35Sshessdo_test fts2b-4.5 {
131*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE plusone MATCH 'one'}
132*8e734e35Sshess} {2 4 6 8 10 12 14}
133*8e734e35Sshessdo_test fts2b-4.6 {
134*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one plusone:two'}
135*8e734e35Sshess} {1 5 9 13}
136*8e734e35Sshessdo_test fts2b-4.7 {
137*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'norm:one two'}
138*8e734e35Sshess} {1 3 5 7 9 11 13 15}
139*8e734e35Sshessdo_test fts2b-4.8 {
140*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'plusone:two norm:one'}
141*8e734e35Sshess} {1 5 9 13}
142*8e734e35Sshessdo_test fts2b-4.9 {
143*8e734e35Sshess  execsql {SELECT rowid FROM t4 WHERE t4 MATCH 'two norm:one'}
144*8e734e35Sshess} {1 3 5 7 9 11 13 15}
145*8e734e35Sshess
146*8e734e35Sshess
147*8e734e35Sshessfinish_test
148