blob: 21383c2d336df3a5319dcb3579bda40bcf1defd7 [file] [log] [blame]
dan85127522012-07-13 19:26:34 +00001# 2012 July 12
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#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix spellfix
16
dan81f07402012-08-07 14:18:18 +000017ifcapable !vtab { finish_test ; return }
18
drhea41dc42013-04-25 19:31:33 +000019load_static_extension db spellfix nextchar
dan85127522012-07-13 19:26:34 +000020
21set vocab {
22rabbi rabbit rabbits rabble rabid rabies raccoon raccoons race raced racer
23racers races racetrack racial racially racing rack racked racket racketeer
24racketeering racketeers rackets racking racks radar radars radial radially
25radian radiance radiant radiantly radiate radiated radiates radiating radiation
26radiations radiator radiators radical radically radicals radices radii radio
27radioactive radioastronomy radioed radiography radioing radiology radios radish
28radishes radium radius radix radon raft rafter rafters rafts rag rage raged
29rages ragged raggedly raggedness raging rags ragweed raid raided raider raiders
30raiding raids rail railed railer railers railing railroad railroaded railroader
31railroaders railroading railroads rails railway railways raiment rain rainbow
32raincoat raincoats raindrop raindrops rained rainfall rainier rainiest raining
33rains rainstorm rainy raise raised raiser raisers raises raisin raising rake
34raked rakes raking rallied rallies rally rallying ram ramble rambler rambles
35rambling ramblings ramification ramifications ramp rampage rampant rampart
36ramps ramrod rams ran ranch ranched rancher ranchers ranches ranching rancid
37random randomization randomize randomized randomizes randomly randomness randy
38rang range ranged rangeland ranger rangers ranges ranging rangy rank ranked
39ranker rankers rankest ranking rankings rankle rankly rankness ranks ransack
40ransacked ransacking ransacks ransom ransomer ransoming ransoms rant ranted
41ranter ranters ranting rants rap rapacious rape raped raper rapes rapid
42rapidity rapidly rapids rapier raping rapport rapprochement raps rapt raptly
43rapture raptures rapturous rare rarely rareness rarer rarest rarity rascal
44rascally rascals rash rasher rashly rashness rasp raspberry rasped rasping
45rasps raster rat rate rated rater raters rates rather ratification ratified
46ratifies ratify ratifying rating ratings ratio ration rational rationale
47rationales rationalities rationality rationalization rationalizations
48rationalize rationalized rationalizes rationalizing rationally rationals
49rationing rations ratios rats rattle rattled rattler rattlers rattles
50rattlesnake rattlesnakes rattling raucous ravage ravaged ravager ravagers
51ravages ravaging rave raved raven ravening ravenous ravenously ravens raves
52ravine ravines raving ravings raw rawer rawest rawly rawness ray rays raze
53razor razors re reabbreviate reabbreviated reabbreviates reabbreviating reach
54reachability reachable reachably reached reacher reaches reaching reacquired
55react reacted reacting reaction reactionaries reactionary reactions reactivate
56reactivated reactivates reactivating reactivation reactive reactively
57reactivity reactor reactors reacts read readability readable reader readers
58readied readier readies readiest readily readiness reading readings readjusted
59readout readouts reads ready readying real realest realign realigned realigning
60realigns realism realist realistic realistically realists realities reality
61}
62
63do_test 1.1 {
64 execsql { CREATE VIRTUAL TABLE t1 USING spellfix1 }
65 foreach word $vocab {
66 execsql { INSERT INTO t1(word) VALUES($word) }
67 }
68} {}
69
70foreach {tn word res} {
drhe0d058c2012-08-31 14:55:05 +000071 1 raxpi* {rasping 5 rasped 5 ragweed 5 raspberry 6 rasp 4}
drh342ced42012-08-15 16:43:07 +000072 2 ril* {rail 4 railed 4 railer 4 railers 4 railing 4}
73 3 rilis* {realism 6 realist 6 realistic 6 realistically 6 realists 6}
74 4 reail* {real 3 realest 3 realign 3 realigned 3 realigning 3}
75 5 ras* {rascal 3 rascally 3 rascals 3 rash 3 rasher 3}
dan85127522012-07-13 19:26:34 +000076 6 realistss* {realists 8 realigns 8 realistic 9 realistically 9 realest 7}
77 7 realistss {realists 8 realist 7 realigns 8 realistic 9 realest 7}
78 8 rllation* {realities 9 reality 7 rallied 7 railed 4}
drh342ced42012-08-15 16:43:07 +000079 9 renstom* {rainstorm 8 ransom 6 ransomer 6 ransoming 6 ransoms 6}
dan85127522012-07-13 19:26:34 +000080} {
81 do_execsql_test 1.2.$tn {
drh342ced42012-08-15 16:43:07 +000082 SELECT word, matchlen FROM t1 WHERE word MATCH $word
83 ORDER BY score, word LIMIT 5
dan85127522012-07-13 19:26:34 +000084 } $res
85}
86
drhea41dc42013-04-25 19:31:33 +000087# Tests of the next_char function.
88#
89do_test 1.10 {
90 db eval {
91 CREATE TABLE vocab(w TEXT PRIMARY KEY);
92 INSERT INTO vocab SELECT word FROM t1;
93 }
94} {}
95do_execsql_test 1.11 {
96 SELECT next_char('re','vocab','w');
97} {a}
drhf4274722013-09-28 13:28:40 +000098do_execsql_test 1.11sub {
99 SELECT next_char('re','(SELECT w AS x FROM vocab)','x');
100} {a}
drhea41dc42013-04-25 19:31:33 +0000101do_execsql_test 1.12 {
102 SELECT next_char('r','vocab','w');
103} {ae}
104do_execsql_test 1.13 {
105 SELECT next_char('','vocab','w');
106} {r}
107do_test 1.14 {
108 catchsql {SELECT next_char('','xyzzy','a')}
109} {1 {no such table: xyzzy}}
dan85127522012-07-13 19:26:34 +0000110
drhd4b473b2013-07-08 01:27:43 +0000111do_execsql_test 1.20 {
112 CREATE TABLE vocab2(w TEXT);
113 CREATE INDEX vocab2w ON vocab2(w COLLATE nocase);
114 INSERT INTO vocab2 VALUES('abc'), ('ABD'), ('aBe'), ('AbF');
115 SELECT next_char('ab', 'vocab2', 'w', null, 'nocase');
116} {cDeF}
117do_execsql_test 1.21 {
118 SELECT next_char('ab','vocab2','w',null,null);
119} {c}
120do_execsql_test 1.22 {
121 SELECT next_char('AB','vocab2','w',null,'NOCASE');
122} {cDeF}
123do_execsql_test 1.23 {
124 SELECT next_char('ab','vocab2','w',null,'binary');
125} {c}
126
dan85127522012-07-13 19:26:34 +0000127do_execsql_test 2.1 {
128 CREATE VIRTUAL TABLE t2 USING spellfix1;
129 INSERT INTO t2 (word, soundslike) VALUES('school', 'skuul');
130 INSERT INTO t2 (word, soundslike) VALUES('psalm', 'sarm');
131 SELECT word, matchlen FROM t2 WHERE word MATCH 'sar*' LIMIT 5;
132} {psalm 4}
133
134do_execsql_test 2.2 {
135 SELECT word, matchlen FROM t2 WHERE word MATCH 'skol*' LIMIT 5;
136} {school 6}
137
138set vocab {
139kangaroo kanji kappa karate keel keeled keeling keels keen keener keenest
140keenly keenness keep keeper keepers keeping keeps ken kennel kennels kept
141kerchief kerchiefs kern kernel kernels kerosene ketchup kettle
142kettles key keyboard keyboards keyed keyhole keying keynote keypad keypads keys
143keystroke keystrokes keyword keywords kick kicked kicker kickers kicking
144kickoff kicks kid kidded kiddie kidding kidnap kidnapper kidnappers kidnapping
145kidnappings kidnaps kidney kidneys kids kill killed killer killers killing
146killingly killings killjoy kills kilobit kilobits kiloblock kilobyte kilobytes
147kilogram kilograms kilohertz kilohm kilojoule kilometer kilometers kiloton
148kilovolt kilowatt kiloword kimono kin kind kinder kindergarten kindest
149kindhearted kindle kindled kindles kindling kindly kindness kindred kinds
150kinetic king kingdom kingdoms kingly kingpin kings kink kinky kinship kinsman
151kiosk kiss kissed kisser kissers kisses kissing kit kitchen kitchenette
152kitchens kite kited kites kiting kits kitten kittenish kittens kitty klaxon
153kludge kludges klystron knack knapsack knapsacks knave knaves knead kneads knee
154kneecap kneed kneeing kneel kneeled kneeling kneels knees knell knells knelt
155knew knife knifed knifes knifing knight knighted knighthood knighting knightly
156knights knit knits knives knob knobs knock knockdown knocked knocker knockers
157knocking knockout knocks knoll knolls knot knots knotted knotting know knowable
158knower knowhow knowing knowingly knowledge knowledgeable known knows knuckle
159knuckled knuckles koala kosher kudo
160}
161
162do_execsql_test 3.1 {
163 CREATE TABLE costs(iLang, cFrom, cTo, iCost);
164 INSERT INTO costs VALUES(0, 'a', 'e', 1);
165 INSERT INTO costs VALUES(0, 'e', 'i', 1);
166 INSERT INTO costs VALUES(0, 'i', 'o', 1);
167 INSERT INTO costs VALUES(0, 'o', 'u', 1);
168 INSERT INTO costs VALUES(0, 'u', 'a', 1);
169 CREATE VIRTUAL TABLE t3 USING spellfix1(edit_cost_table=costs);
170}
171
172do_test 3.2 {
173 foreach w $vocab {
174 execsql { INSERT INTO t3(word) VALUES($w) }
175 }
176} {}
177
dan85127522012-07-13 19:26:34 +0000178foreach {tn word res} {
drhc16eb692012-08-14 17:29:27 +0000179 1 kos* {kosher 3 kiosk 4 kudo 2 kiss 3 kissed 3}
180 2 kellj* {killjoy 5 kill 4 killed 4 killer 4 killers 4}
dan85127522012-07-13 19:26:34 +0000181 3 kellj {kill 4 kills 5 killjoy 7 keel 4 killed 6}
182} {
drh5cee8a72013-02-19 11:51:27 +0000183 do_execsql_test 3.2.$tn {
drhc16eb692012-08-14 17:29:27 +0000184 SELECT word, matchlen FROM t3 WHERE word MATCH $word
185 ORDER BY score, word LIMIT 5
dan85127522012-07-13 19:26:34 +0000186 } $res
drh5cee8a72013-02-19 11:51:27 +0000187}
188
189do_execsql_test 4.0 {
190 INSERT INTO t3(command) VALUES('edit_cost_table=NULL');
191}
192foreach {tn word res} {
193 1 kosher {kosher 0 kisser 51 kissers 76 kissed 126 kisses 126}
194 2 kellj {keels 60 killjoy 68 kills 80 keel 120 kill 125}
195 3 kashar {kosher 80 kisser 91 kissers 116 kissed 166 kisses 166}
196} {
197 do_execsql_test 4.1.$tn {
198 SELECT word, distance FROM t3 WHERE word MATCH $word
199 ORDER BY score, word LIMIT 5
200 } $res
201}
202do_execsql_test 5.0 {
203 CREATE TABLE costs2(iLang, cFrom, cTo, iCost);
204 INSERT INTO costs2 VALUES(0, 'a', 'o', 1);
205 INSERT INTO costs2 VALUES(0, 'e', 'o', 4);
206 INSERT INTO costs2 VALUES(0, 'i', 'o', 8);
207 INSERT INTO costs2 VALUES(0, 'u', 'o', 16);
208 INSERT INTO t3(command) VALUES('edit_cost_table="costs2"');
209}
210
211foreach {tn word res} {
212 1 kasher {kosher 1}
213 2 kesher {kosher 4}
214 3 kisher {kosher 8}
215 4 kosher {kosher 0}
216 5 kusher {kosher 16}
217} {
218 do_execsql_test 5.1.$tn {
219 SELECT word, distance FROM t3 WHERE word MATCH $word
220 ORDER BY score, word LIMIT 1
221 } $res
222}
223
dana8a06172014-01-22 17:43:16 +0000224#-------------------------------------------------------------------------
225# Try some queries by rowid.
226#
227do_execsql_test 6.1.1 {
228 SELECT word FROM t3 WHERE rowid = 10;
229} {keener}
230do_execsql_test 6.1.2 {
231 SELECT word, distance FROM t3 WHERE rowid = 10;
232} {keener {}}
233do_execsql_test 6.1.3 {
234 SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner';
235} {keener 300}
236
danabfe0342014-01-27 08:48:10 +0000237ifcapable trace {
238 proc trace_callback {sql} {
239 if {[string range $sql 0 2] == "-- "} {
240 lappend ::trace [string range $sql 3 end]
241 }
dana8a06172014-01-22 17:43:16 +0000242 }
danabfe0342014-01-27 08:48:10 +0000243
244 proc do_tracesql_test {tn sql {res {}}} {
245 set ::trace [list]
246 uplevel [list do_test $tn [subst -nocommands {
247 set vals [execsql {$sql}]
248 concat [set vals] [set ::trace]
249 }] [list {*}$res]]
250 }
251
252 db trace trace_callback
253 do_tracesql_test 6.2.1 {
254 SELECT word FROM t3 WHERE rowid = 10;
255 } {keener
256 {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?}
257 }
258 do_tracesql_test 6.2.2 {
259 SELECT word, distance FROM t3 WHERE rowid = 10;
260 } {keener {}
261 {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?}
262 }
263 do_tracesql_test 6.2.3 {
264 SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner';
265 } {keener 300
266 {SELECT id, word, rank, k1 FROM "main"."t3_vocab" WHERE langid=0 AND k2>=?1 AND k2<?2}
267 }
dana8a06172014-01-22 17:43:16 +0000268}
269
270
drh5cee8a72013-02-19 11:51:27 +0000271
dan85127522012-07-13 19:26:34 +0000272
273finish_test