AryaWu/sqlite
0
1# 2007 May 142#3# The author disclaims copyright to this source code. In place of4# 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# This file implements regression tests for SQLite library. The12# focus of this file is testing the built-in SUBSTR() functions.13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17 18ifcapable !tclvar {19 finish_test20 return21}22 23# Create a table to work with.24#25execsql { 26 CREATE TABLE t1(t text, b blob)27}28proc substr-test {id string i1 i2 result} {29 db eval {30 DELETE FROM t1;31 INSERT INTO t1(t) VALUES($string)32 }33 do_test substr-$id.1 [subst {34 execsql {35 SELECT substr(t, $i1, $i2) FROM t136 }37 }] [list $result]38 set qstr '[string map {' ''} $string]'39 do_test substr-$id.2 [subst {40 execsql {41 SELECT substring($qstr, $i1, $i2)42 }43 }] [list $result]44}45proc subblob-test {id hex i1 i2 hexresult} {46 db eval "47 DELETE FROM t1;48 INSERT INTO t1(b) VALUES(x'$hex')49 "50 do_test substr-$id.1 [subst {51 execsql {52 SELECT hex(substr(b, $i1, $i2)) FROM t153 }54 }] [list $hexresult]55 do_test substr-$id.2 [subst {56 execsql {57 SELECT hex(substring(x'$hex', $i1, $i2))58 }59 }] [list $hexresult]60}61 62# Basic SUBSTR functionality63#64substr-test 1.1 abcdefg 1 1 a65substr-test 1.2 abcdefg 2 1 b66substr-test 1.3 abcdefg 1 2 ab67substr-test 1.4 abcdefg 1 100 abcdefg68substr-test 1.5 abcdefg 0 2 a69substr-test 1.6 abcdefg -1 1 g70substr-test 1.7 abcdefg -1 10 g71substr-test 1.8 abcdefg -5 3 cde72substr-test 1.9 abcdefg -7 3 abc73substr-test 1.10 abcdefg -100 98 abcde74substr-test 1.11 abcdefg 5 -1 d75substr-test 1.12 abcdefg 5 -4 abcd76substr-test 1.13 abcdefg 5 -5 abcd77substr-test 1.14 abcdefg -5 -1 b78substr-test 1.15 abcdefg -5 -2 ab79substr-test 1.16 abcdefg -5 -3 ab80substr-test 1.17 abcdefg 100 200 {}81substr-test 1.18 abcdefg 200 100 {}82 83# Make sure NULL is returned if any parameter is NULL84#85do_test substr-1.90 {86 db eval {SELECT ifnull(substr(NULL,1,1),'nil')}87} nil88do_test substr-1.91 {89 db eval {SELECT ifnull(substr(NULL,1),'nil')}90} nil91do_test substr-1.92 {92 db eval {SELECT ifnull(substr('abcdefg',NULL,1),'nil')}93} nil94do_test substr-1.93 {95 db eval {SELECT ifnull(substring('abcdefg',NULL),'nil')}96} nil97do_test substr-1.94 {98 db eval {SELECT ifnull(substr('abcdefg',1,NULL),'nil')}99} nil100 101# Make sure everything works with long unicode characters102#103substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234104substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345105substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345106substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456107substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c108substr-test 2.6 a\u1234b\u2345c\u3456c -2 -3 b\u2345c109 110# Basic functionality for BLOBs111#112subblob-test 3.1 61626364656667 1 1 61113subblob-test 3.2 61626364656667 2 1 62114subblob-test 3.3 61626364656667 1 2 6162115subblob-test 3.4 61626364656667 1 100 61626364656667116subblob-test 3.5 61626364656667 0 2 61117subblob-test 3.6 61626364656667 -1 1 67118subblob-test 3.7 61626364656667 -1 10 67119subblob-test 3.8 61626364656667 -5 3 636465120subblob-test 3.9 61626364656667 -7 3 616263121subblob-test 3.10 61626364656667 -100 98 6162636465122subblob-test 3.11 61626364656667 100 200 {}123subblob-test 3.12 61626364656667 200 100 {}124 125# If these blobs were strings, then they would contain multi-byte126# characters. But since they are blobs, the substr indices refer127# to bytes.128#129subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61130subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1131subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1132subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96133subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196134subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391 135 136# Two-argument SUBSTR137#138proc substr-2-test {id string idx result} {139 db eval {140 DELETE FROM t1;141 INSERT INTO t1(t) VALUES($string)142 }143 do_test substr-$id.1 [subst {144 execsql {145 SELECT substr(t, $idx) FROM t1146 }147 }] [list $result]148 set qstr '[string map {' ''} $string]'149 do_test substr-$id.2 [subst {150 execsql {151 SELECT substring($qstr, $idx)152 }153 }] [list $result]154}155substr-2-test 5.1 abcdefghijklmnop 5 efghijklmnop156substr-2-test 5.2 abcdef -5 bcdef157 158finish_test159 