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 built-in functions.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
20 # func2-1.*: substr implementation (ascii)
21 # func2-2.*: substr implementation (utf8)
22 # func2-3.*: substr implementation (blob)
25 proc bin_to_hex {blob} {
27 binary scan $blob \c* bytes
29 foreach b $bytes {lappend bytes2 [format %02X [expr $b & 0xFF]]}
33 #----------------------------------------------------------------------------
34 # Test cases func2-1.*: substr implementation (ascii)
38 execsql {SELECT 'Supercalifragilisticexpialidocious'}
39 } {Supercalifragilisticexpialidocious}
41 # substr(x,y), substr(x,y,z)
43 catchsql {SELECT SUBSTR()}
44 } {1 {wrong number of arguments to function SUBSTR()}}
46 catchsql {SELECT SUBSTR('Supercalifragilisticexpialidocious')}
47 } {1 {wrong number of arguments to function SUBSTR()}}
49 catchsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1,1,1)}
50 } {1 {wrong number of arguments to function SUBSTR()}}
54 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0)}
55 } {Supercalifragilisticexpialidocious}
57 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1)}
58 } {Supercalifragilisticexpialidocious}
60 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2)}
61 } {upercalifragilisticexpialidocious}
63 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30)}
66 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34)}
69 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35)}
72 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36)}
75 # if p1<0, start from right
77 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -0)}
78 } {Supercalifragilisticexpialidocious}
80 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1)}
83 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -2)}
86 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -30)}
87 } {rcalifragilisticexpialidocious}
89 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34)}
90 } {Supercalifragilisticexpialidocious}
92 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35)}
93 } {Supercalifragilisticexpialidocious}
95 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36)}
96 } {Supercalifragilisticexpialidocious}
98 # p1 is 1-indexed, p2 length to return
99 do_test func2-1.17.1 {
100 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 1)}
102 do_test func2-1.17.2 {
103 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 2)}
106 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, 1)}
108 do_test func2-1.19.0 {
109 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 0)}
111 do_test func2-1.19.1 {
112 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 1)}
114 do_test func2-1.19.2 {
115 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 2)}
118 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, 1)}
121 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34, 1)}
124 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35, 1)}
127 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, 1)}
130 # if p1<0, start from right, p2 length to return
132 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -0, 1)}
134 do_test func2-1.25.0 {
135 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 0)}
137 do_test func2-1.25.1 {
138 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 1)}
140 do_test func2-1.25.2 {
141 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -1, 2)}
144 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -2, 1)}
147 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -30, 1)}
149 do_test func2-1.28.0 {
150 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 0)}
152 do_test func2-1.28.1 {
153 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 1)}
155 do_test func2-1.28.2 {
156 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -34, 2)}
158 do_test func2-1.29.1 {
159 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35, 1)}
161 do_test func2-1.29.2 {
162 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -35, 2)}
164 do_test func2-1.30.0 {
165 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 0)}
167 do_test func2-1.30.1 {
168 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 1)}
170 do_test func2-1.30.2 {
171 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 2)}
173 do_test func2-1.30.3 {
174 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', -36, 3)}
177 # p1 is 1-indexed, p2 length to return, p2<0 return p2 chars before p1
178 do_test func2-1.31.0 {
179 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, 0)}
181 do_test func2-1.31.1 {
182 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, -1)}
184 do_test func2-1.31.2 {
185 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 0, -2)}
187 do_test func2-1.32.0 {
188 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, 0)}
190 do_test func2-1.32.1 {
191 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 1, -1)}
193 do_test func2-1.33.0 {
194 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, 0)}
196 do_test func2-1.33.1 {
197 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, -1)}
199 do_test func2-1.33.2 {
200 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 2, -2)}
202 do_test func2-1.34.0 {
203 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, 0)}
205 do_test func2-1.34.1 {
206 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, -1)}
208 do_test func2-1.34.2 {
209 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 3, -2)}
211 do_test func2-1.35.1 {
212 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, -1)}
214 do_test func2-1.35.2 {
215 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 30, -2)}
218 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 34, -1)}
221 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 35, -1)}
223 do_test func2-1.38.0 {
224 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, 0)}
226 do_test func2-1.38.1 {
227 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, -1)}
229 do_test func2-1.38.2 {
230 execsql {SELECT SUBSTR('Supercalifragilisticexpialidocious', 36, -2)}
234 #----------------------------------------------------------------------------
235 # Test cases func2-2.*: substr implementation (utf8)
238 # Only do the following tests if TCL has UTF-8 capabilities
240 if {"\u1234"!="u1234"} {
242 do_test func2-2.1.1 {
243 execsql "SELECT 'hi\u1234ho'"
246 # substr(x,y), substr(x,y,z)
247 do_test func2-2.1.2 {
248 catchsql "SELECT SUBSTR()"
249 } {1 {wrong number of arguments to function SUBSTR()}}
250 do_test func2-2.1.3 {
251 catchsql "SELECT SUBSTR('hi\u1234ho')"
252 } {1 {wrong number of arguments to function SUBSTR()}}
253 do_test func2-2.1.4 {
254 catchsql "SELECT SUBSTR('hi\u1234ho', 1,1,1)"
255 } {1 {wrong number of arguments to function SUBSTR()}}
257 do_test func2-2.2.0 {
258 execsql "SELECT SUBSTR('hi\u1234ho', 0, 0)"
260 do_test func2-2.2.1 {
261 execsql "SELECT SUBSTR('hi\u1234ho', 0, 1)"
263 do_test func2-2.2.2 {
264 execsql "SELECT SUBSTR('hi\u1234ho', 0, 2)"
266 do_test func2-2.2.3 {
267 execsql "SELECT SUBSTR('hi\u1234ho', 0, 3)"
269 do_test func2-2.2.4 {
270 execsql "SELECT SUBSTR('hi\u1234ho', 0, 4)"
272 do_test func2-2.2.5 {
273 execsql "SELECT SUBSTR('hi\u1234ho', 0, 5)"
275 do_test func2-2.2.6 {
276 execsql "SELECT SUBSTR('hi\u1234ho', 0, 6)"
279 do_test func2-2.3.0 {
280 execsql "SELECT SUBSTR('hi\u1234ho', 1, 0)"
282 do_test func2-2.3.1 {
283 execsql "SELECT SUBSTR('hi\u1234ho', 1, 1)"
285 do_test func2-2.3.2 {
286 execsql "SELECT SUBSTR('hi\u1234ho', 1, 2)"
288 do_test func2-2.3.3 {
289 execsql "SELECT SUBSTR('hi\u1234ho', 1, 3)"
291 do_test func2-2.3.4 {
292 execsql "SELECT SUBSTR('hi\u1234ho', 1, 4)"
294 do_test func2-2.3.5 {
295 execsql "SELECT SUBSTR('hi\u1234ho', 1, 5)"
297 do_test func2-2.3.6 {
298 execsql "SELECT SUBSTR('hi\u1234ho', 1, 6)"
301 do_test func2-2.4.0 {
302 execsql "SELECT SUBSTR('hi\u1234ho', 3, 0)"
304 do_test func2-2.4.1 {
305 execsql "SELECT SUBSTR('hi\u1234ho', 3, 1)"
307 do_test func2-2.4.2 {
308 execsql "SELECT SUBSTR('hi\u1234ho', 3, 2)"
311 do_test func2-2.5.0 {
312 execsql "SELECT SUBSTR('\u1234', 0, 0)"
314 do_test func2-2.5.1 {
315 execsql "SELECT SUBSTR('\u1234', 0, 1)"
317 do_test func2-2.5.2 {
318 execsql "SELECT SUBSTR('\u1234', 0, 2)"
320 do_test func2-2.5.3 {
321 execsql "SELECT SUBSTR('\u1234', 0, 3)"
324 do_test func2-2.6.0 {
325 execsql "SELECT SUBSTR('\u1234', 1, 0)"
327 do_test func2-2.6.1 {
328 execsql "SELECT SUBSTR('\u1234', 1, 1)"
330 do_test func2-2.6.2 {
331 execsql "SELECT SUBSTR('\u1234', 1, 2)"
333 do_test func2-2.6.3 {
334 execsql "SELECT SUBSTR('\u1234', 1, 3)"
337 do_test func2-2.7.0 {
338 execsql "SELECT SUBSTR('\u1234', 2, 0)"
340 do_test func2-2.7.1 {
341 execsql "SELECT SUBSTR('\u1234', 2, 1)"
343 do_test func2-2.7.2 {
344 execsql "SELECT SUBSTR('\u1234', 2, 2)"
347 do_test func2-2.8.0 {
348 execsql "SELECT SUBSTR('\u1234', -1, 0)"
350 do_test func2-2.8.1 {
351 execsql "SELECT SUBSTR('\u1234', -1, 1)"
353 do_test func2-2.8.2 {
354 execsql "SELECT SUBSTR('\u1234', -1, 2)"
356 do_test func2-2.8.3 {
357 execsql "SELECT SUBSTR('\u1234', -1, 3)"
360 } ;# End \u1234!=u1234
362 #----------------------------------------------------------------------------
363 # Test cases func2-3.*: substr implementation (blob)
366 ifcapable {!bloblit} {
371 do_test func2-3.1.1 {
372 set blob [execsql "SELECT x'1234'"]
373 bin_to_hex [lindex $blob 0]
376 # substr(x,y), substr(x,y,z)
377 do_test func2-3.1.2 {
378 catchsql {SELECT SUBSTR()}
379 } {1 {wrong number of arguments to function SUBSTR()}}
380 do_test func2-3.1.3 {
381 catchsql {SELECT SUBSTR(x'1234')}
382 } {1 {wrong number of arguments to function SUBSTR()}}
383 do_test func2-3.1.4 {
384 catchsql {SELECT SUBSTR(x'1234', 1,1,1)}
385 } {1 {wrong number of arguments to function SUBSTR()}}
387 do_test func2-3.2.0 {
388 set blob [execsql "SELECT SUBSTR(x'1234', 0, 0)"]
389 bin_to_hex [lindex $blob 0]
391 do_test func2-3.2.1 {
392 set blob [execsql "SELECT SUBSTR(x'1234', 0, 1)"]
393 bin_to_hex [lindex $blob 0]
395 do_test func2-3.2.2 {
396 set blob [execsql "SELECT SUBSTR(x'1234', 0, 2)"]
397 bin_to_hex [lindex $blob 0]
399 do_test func2-3.2.3 {
400 set blob [execsql "SELECT SUBSTR(x'1234', 0, 3)"]
401 bin_to_hex [lindex $blob 0]
404 do_test func2-3.3.0 {
405 set blob [execsql "SELECT SUBSTR(x'1234', 1, 0)"]
406 bin_to_hex [lindex $blob 0]
408 do_test func2-3.3.1 {
409 set blob [execsql "SELECT SUBSTR(x'1234', 1, 1)"]
410 bin_to_hex [lindex $blob 0]
412 do_test func2-3.3.2 {
413 set blob [execsql "SELECT SUBSTR(x'1234', 1, 2)"]
414 bin_to_hex [lindex $blob 0]
416 do_test func2-3.3.3 {
417 set blob [execsql "SELECT SUBSTR(x'1234', 1, 3)"]
418 bin_to_hex [lindex $blob 0]
421 do_test func2-3.4.0 {
422 set blob [execsql "SELECT SUBSTR(x'1234', -1, 0)"]
423 bin_to_hex [lindex $blob 0]
425 do_test func2-3.4.1 {
426 set blob [execsql "SELECT SUBSTR(x'1234', -1, 1)"]
427 bin_to_hex [lindex $blob 0]
429 do_test func2-3.4.2 {
430 set blob [execsql "SELECT SUBSTR(x'1234', -1, 2)"]
431 bin_to_hex [lindex $blob 0]
433 do_test func2-3.4.3 {
434 set blob [execsql "SELECT SUBSTR(x'1234', -1, 3)"]
435 bin_to_hex [lindex $blob 0]
438 do_test func2-3.5.0 {
439 set blob [execsql "SELECT SUBSTR(x'1234', -2, 0)"]
440 bin_to_hex [lindex $blob 0]
442 do_test func2-3.5.1 {
443 set blob [execsql "SELECT SUBSTR(x'1234', -2, 1)"]
444 bin_to_hex [lindex $blob 0]
446 do_test func2-3.5.2 {
447 set blob [execsql "SELECT SUBSTR(x'1234', -2, 2)"]
448 bin_to_hex [lindex $blob 0]
450 do_test func2-3.5.3 {
451 set blob [execsql "SELECT SUBSTR(x'1234', -2, 3)"]
452 bin_to_hex [lindex $blob 0]
455 do_test func2-3.6.0 {
456 set blob [execsql "SELECT SUBSTR(x'1234', -1, 0)"]
457 bin_to_hex [lindex $blob 0]
459 do_test func2-3.6.1 {
460 set blob [execsql "SELECT SUBSTR(x'1234', -1, -1)"]
461 bin_to_hex [lindex $blob 0]
463 do_test func2-3.6.2 {
464 set blob [execsql "SELECT SUBSTR(x'1234', -1, -2)"]
465 bin_to_hex [lindex $blob 0]
467 do_test func2-3.6.3 {
468 set blob [execsql "SELECT SUBSTR(x'1234', -1, -3)"]
469 bin_to_hex [lindex $blob 0]
472 do_test func2-3.7.0 {
473 set blob [execsql "SELECT SUBSTR(x'1234', -2, 0)"]
474 bin_to_hex [lindex $blob 0]
476 do_test func2-3.7.1 {
477 set blob [execsql "SELECT SUBSTR(x'1234', -2, -1)"]
478 bin_to_hex [lindex $blob 0]
480 do_test func2-3.7.2 {
481 set blob [execsql "SELECT SUBSTR(x'1234', -2, -2)"]
482 bin_to_hex [lindex $blob 0]
485 do_test func2-3.8.0 {
486 set blob [execsql "SELECT SUBSTR(x'1234', 1, 0)"]
487 bin_to_hex [lindex $blob 0]
489 do_test func2-3.8.1 {
490 set blob [execsql "SELECT SUBSTR(x'1234', 1, -1)"]
491 bin_to_hex [lindex $blob 0]
493 do_test func2-3.8.2 {
494 set blob [execsql "SELECT SUBSTR(x'1234', 1, -2)"]
495 bin_to_hex [lindex $blob 0]
498 do_test func2-3.9.0 {
499 set blob [execsql "SELECT SUBSTR(x'1234', 2, 0)"]
500 bin_to_hex [lindex $blob 0]
502 do_test func2-3.9.1 {
503 set blob [execsql "SELECT SUBSTR(x'1234', 2, -1)"]
504 bin_to_hex [lindex $blob 0]
506 do_test func2-3.9.2 {
507 set blob [execsql "SELECT SUBSTR(x'1234', 2, -2)"]
508 bin_to_hex [lindex $blob 0]