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 SUBSTR() functions.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
23 # Create a table to work with.
26 CREATE TABLE t1(t text, b blob)
28 proc substr-test {id string i1 i2 result} {
31 INSERT INTO t1(t) VALUES($string)
33 do_test substr-$id.1 [subst {
35 SELECT substr(t, $i1, $i2) FROM t1
38 set qstr '[string map {' ''} $string]'
39 do_test substr-$id.2 [subst {
41 SELECT substring($qstr, $i1, $i2)
45 proc subblob-test {id hex i1 i2 hexresult} {
48 INSERT INTO t1(b) VALUES(x'$hex')
50 do_test substr-$id.1 [subst {
52 SELECT hex(substr(b, $i1, $i2)) FROM t1
55 do_test substr-$id.2 [subst {
57 SELECT hex(substring(x'$hex', $i1, $i2))
62 # Basic SUBSTR functionality
64 substr-test 1.1 abcdefg 1 1 a
65 substr-test 1.2 abcdefg 2 1 b
66 substr-test 1.3 abcdefg 1 2 ab
67 substr-test 1.4 abcdefg 1 100 abcdefg
68 substr-test 1.5 abcdefg 0 2 a
69 substr-test 1.6 abcdefg -1 1 g
70 substr-test 1.7 abcdefg -1 10 g
71 substr-test 1.8 abcdefg -5 3 cde
72 substr-test 1.9 abcdefg -7 3 abc
73 substr-test 1.10 abcdefg -100 98 abcde
74 substr-test 1.11 abcdefg 5 -1 d
75 substr-test 1.12 abcdefg 5 -4 abcd
76 substr-test 1.13 abcdefg 5 -5 abcd
77 substr-test 1.14 abcdefg -5 -1 b
78 substr-test 1.15 abcdefg -5 -2 ab
79 substr-test 1.16 abcdefg -5 -3 ab
80 substr-test 1.17 abcdefg 100 200 {}
81 substr-test 1.18 abcdefg 200 100 {}
83 # Make sure NULL is returned if any parameter is NULL
86 db eval {SELECT ifnull(substr(NULL,1,1),'nil')}
89 db eval {SELECT ifnull(substr(NULL,1),'nil')}
92 db eval {SELECT ifnull(substr('abcdefg',NULL,1),'nil')}
95 db eval {SELECT ifnull(substring('abcdefg',NULL),'nil')}
98 db eval {SELECT ifnull(substr('abcdefg',1,NULL),'nil')}
101 # Make sure everything works with long unicode characters
103 substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234
104 substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345
105 substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345
106 substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456
107 substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c
108 substr-test 2.6 a\u1234b\u2345c\u3456c -2 -3 b\u2345c
110 # Basic functionality for BLOBs
112 subblob-test 3.1 61626364656667 1 1 61
113 subblob-test 3.2 61626364656667 2 1 62
114 subblob-test 3.3 61626364656667 1 2 6162
115 subblob-test 3.4 61626364656667 1 100 61626364656667
116 subblob-test 3.5 61626364656667 0 2 61
117 subblob-test 3.6 61626364656667 -1 1 67
118 subblob-test 3.7 61626364656667 -1 10 67
119 subblob-test 3.8 61626364656667 -5 3 636465
120 subblob-test 3.9 61626364656667 -7 3 616263
121 subblob-test 3.10 61626364656667 -100 98 6162636465
122 subblob-test 3.11 61626364656667 100 200 {}
123 subblob-test 3.12 61626364656667 200 100 {}
125 # If these blobs were strings, then they would contain multi-byte
126 # characters. But since they are blobs, the substr indices refer
129 subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61
130 subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1
131 subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1
132 subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96
133 subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196
134 subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391
136 # Two-argument SUBSTR
138 proc substr-2-test {id string idx result} {
141 INSERT INTO t1(t) VALUES($string)
143 do_test substr-$id.1 [subst {
145 SELECT substr(t, $idx) FROM t1
148 set qstr '[string map {' ''} $string]'
149 do_test substr-$id.2 [subst {
151 SELECT substring($qstr, $idx)
155 substr-2-test 5.1 abcdefghijklmnop 5 efghijklmnop
156 substr-2-test 5.2 abcdef -5 bcdef