3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the built-in INSTR() functions.
14 # EVIDENCE-OF: R-27549-59611 The instr(X,Y) function finds the first
15 # occurrence of string Y within string X and returns the number of prior
16 # characters plus 1, or 0 if Y is nowhere found within X.
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Create a table to work with.
26 db eval {SELECT instr('abcdefg','a');}
29 db eval {SELECT instr('abcdefg','b');}
32 db eval {SELECT instr('abcdefg','c');}
35 db eval {SELECT instr('abcdefg','d');}
38 db eval {SELECT instr('abcdefg','e');}
41 db eval {SELECT instr('abcdefg','f');}
44 db eval {SELECT instr('abcdefg','g');}
47 db eval {SELECT instr('abcdefg','h');}
50 db eval {SELECT instr('abcdefg','abcdefg');}
53 db eval {SELECT instr('abcdefg','abcdefgh');}
56 db eval {SELECT instr('abcdefg','bcdefg');}
59 db eval {SELECT instr('abcdefg','bcdefgh');}
62 db eval {SELECT instr('abcdefg','cdefg');}
65 db eval {SELECT instr('abcdefg','cdefgh');}
68 db eval {SELECT instr('abcdefg','defg');}
71 db eval {SELECT instr('abcdefg','defgh');}
74 db eval {SELECT instr('abcdefg','efg');}
77 db eval {SELECT instr('abcdefg','efgh');}
80 db eval {SELECT instr('abcdefg','fg');}
83 db eval {SELECT instr('abcdefg','fgh');}
86 db eval {SELECT coalesce(instr('abcdefg',NULL),'nil');}
89 db eval {SELECT coalesce(instr(NULL,'x'),'nil');}
92 db eval {SELECT instr(12345,34);}
95 db eval {SELECT instr(123456.78,34);}
98 db eval {SELECT instr(123456.78,x'3334');}
101 db eval {SELECT instr('äbcdefg','efg');}
104 db eval {SELECT instr('€xyzzy','xyz');}
107 db eval {SELECT instr('abc€xyzzy','xyz');}
110 db eval {SELECT instr('abc€xyzzy','€xyz');}
113 db eval {SELECT instr('abc€xyzzy','c€xyz');}
116 db eval {SELECT instr(x'0102030405',x'01');}
119 db eval {SELECT instr(x'0102030405',x'02');}
122 db eval {SELECT instr(x'0102030405',x'03');}
125 db eval {SELECT instr(x'0102030405',x'04');}
128 db eval {SELECT instr(x'0102030405',x'05');}
131 db eval {SELECT instr(x'0102030405',x'06');}
134 db eval {SELECT instr(x'0102030405',x'0102030405');}
137 db eval {SELECT instr(x'0102030405',x'02030405');}
140 db eval {SELECT instr(x'0102030405',x'030405');}
143 db eval {SELECT instr(x'0102030405',x'0405');}
146 db eval {SELECT instr(x'0102030405',x'0506');}
149 db eval {SELECT instr(x'0102030405',x'');}
152 db eval {SELECT instr(x'',x'');}
155 db eval {SELECT instr('','');}
158 db eval {SELECT instr('abcdefg','');}
160 unset -nocomplain longstr
161 set longstr abcdefghijklmonpqrstuvwxyz
162 append longstr $longstr
163 append longstr $longstr
164 append longstr $longstr
165 append longstr $longstr
166 append longstr $longstr
167 append longstr $longstr
168 append longstr $longstr
169 append longstr $longstr
170 append longstr $longstr
171 append longstr $longstr
172 append longstr $longstr
173 append longstr $longstr
174 # puts [string length $longstr]
175 append longstr Xabcde
177 db eval {SELECT instr($longstr,'X');}
180 db eval {SELECT instr($longstr,'Y');}
183 db eval {SELECT instr($longstr,'Xa');}
186 db eval {SELECT instr($longstr,'zXa');}
188 set longstr [string map {a ä} $longstr]
190 db eval {SELECT instr($longstr,'X');}
193 db eval {SELECT instr($longstr,'Y');}
196 db eval {SELECT instr($longstr,'Xä');}
199 db eval {SELECT instr($longstr,'zXä');}
202 db eval {SELECT instr(x'78c3a4e282ac79','x');}
205 db eval {SELECT instr(x'78c3a4e282ac79','y');}
208 # EVIDENCE-OF: R-46421-32541 Or, if X and Y are both BLOBs, then
209 # instr(X,Y) returns one more than the number bytes prior to the first
210 # occurrence of Y, or 0 if Y does not occur anywhere within X.
212 do_test instr-1.56.1 {
213 db eval {SELECT instr(x'78c3a4e282ac79',x'79');}
215 do_test instr-1.56.2 {
216 db eval {SELECT instr(x'78c3a4e282ac79',x'7a');}
218 do_test instr-1.56.3 {
219 db eval {SELECT instr(x'78c3a4e282ac79',x'78');}
221 do_test instr-1.56.3 {
222 db eval {SELECT instr(x'78c3a4e282ac79',x'a4');}
225 # EVIDENCE-OF: R-17329-35644 If both arguments X and Y to instr(X,Y) are
226 # non-NULL and are not BLOBs then both are interpreted as strings.
228 do_test instr-1.57.1 {
229 db eval {SELECT instr('xä€y',x'79');}
231 do_test instr-1.57.2 {
232 db eval {SELECT instr('xä€y',x'a4');}
234 do_test instr-1.57.3 {
235 db eval {SELECT instr(x'78c3a4e282ac79','y');}
238 # EVIDENCE-OF: R-14708-27487 If either X or Y are NULL in instr(X,Y)
239 # then the result is NULL.
241 do_execsql_test instr-1.60 {
242 SELECT coalesce(instr(NULL,'abc'), 999);
244 do_execsql_test instr-1.61 {
245 SELECT coalesce(instr('abc',NULL), 999);
247 do_execsql_test instr-1.62 {
248 SELECT coalesce(instr(NULL,NULL), 999);
251 do_execsql_test instr-1.63 {
252 SELECT instr(X'', 'abc')
254 do_execsql_test instr-1.64 {
255 CREATE TABLE x1(a, b);
256 INSERT INTO x1 VALUES(X'', 'abc');
257 SELECT instr(a, b) FROM x1;
260 # 2019-09-16 ticket https://www.sqlite.org/src/info/587791f92620090e
262 do_execsql_test instr-2.0 {
263 DROP TABLE IF EXISTS t0;
264 CREATE TABLE t0(c0 PRIMARY KEY, c1);
265 INSERT INTO t0(c0) VALUES (x'bb'), (0);
266 SELECT COUNT(*) FROM t0 WHERE INSTR(x'aabb', t0.c0) ORDER BY t0.c0, t0.c1;
268 do_execsql_test instr-2.1 {
269 SELECT quote(c0) FROM t0 WHERE INSTR(x'aabb', t0.c0) ORDER BY t0.c0, t0.c1;
271 do_execsql_test instr-2.2 {
272 DROP TABLE IF EXISTS t1;
274 INSERT INTO t1(x) VALUES('text'),(x'bb');
275 SELECT quote(x) FROM t1 WHERE instr(x'aabb',x);
277 do_execsql_test instr-2.3 {
278 SELECT quote(x) FROM t1 WHERE x>'zzz' AND instr(x'aabb',x);