sl@0: # 2007 May 14 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this file is testing the built-in SUBSTR() functions. sl@0: # sl@0: # $Id: substr.test,v 1.3 2007/10/12 19:11:55 drh Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !tclvar { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Create a table to work with. sl@0: # sl@0: execsql { sl@0: CREATE TABLE t1(t text, b blob) sl@0: } sl@0: proc substr-test {id string i1 i2 result} { sl@0: db eval { sl@0: DELETE FROM t1; sl@0: INSERT INTO t1(t) VALUES($string) sl@0: } sl@0: do_test substr-$id.1 [subst { sl@0: execsql { sl@0: SELECT substr(t, $i1, $i2) FROM t1 sl@0: } sl@0: }] [list $result] sl@0: set qstr '[string map {' ''} $string]' sl@0: do_test substr-$id.2 [subst { sl@0: execsql { sl@0: SELECT substr($qstr, $i1, $i2) sl@0: } sl@0: }] [list $result] sl@0: } sl@0: proc subblob-test {id hex i1 i2 hexresult} { sl@0: db eval " sl@0: DELETE FROM t1; sl@0: INSERT INTO t1(b) VALUES(x'$hex') sl@0: " sl@0: do_test substr-$id.1 [subst { sl@0: execsql { sl@0: SELECT hex(substr(b, $i1, $i2)) FROM t1 sl@0: } sl@0: }] [list $hexresult] sl@0: do_test substr-$id.2 [subst { sl@0: execsql { sl@0: SELECT hex(substr(x'$hex', $i1, $i2)) sl@0: } sl@0: }] [list $hexresult] sl@0: } sl@0: sl@0: # Basic SUBSTR functionality sl@0: # sl@0: substr-test 1.1 abcdefg 1 1 a sl@0: substr-test 1.2 abcdefg 2 1 b sl@0: substr-test 1.3 abcdefg 1 2 ab sl@0: substr-test 1.4 abcdefg 1 100 abcdefg sl@0: substr-test 1.5 abcdefg 0 1 a sl@0: substr-test 1.6 abcdefg -1 1 g sl@0: substr-test 1.7 abcdefg -1 10 g sl@0: substr-test 1.8 abcdefg -5 3 cde sl@0: substr-test 1.9 abcdefg -7 3 abc sl@0: substr-test 1.10 abcdefg -100 98 abcde sl@0: sl@0: # Make sure everything works with long unicode characters sl@0: # sl@0: substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234 sl@0: substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345 sl@0: substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345 sl@0: substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456 sl@0: substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c sl@0: sl@0: # Basic functionality for BLOBs sl@0: # sl@0: subblob-test 3.1 61626364656667 1 1 61 sl@0: subblob-test 3.2 61626364656667 2 1 62 sl@0: subblob-test 3.3 61626364656667 1 2 6162 sl@0: subblob-test 3.4 61626364656667 1 100 61626364656667 sl@0: subblob-test 3.5 61626364656667 0 1 61 sl@0: subblob-test 3.6 61626364656667 -1 1 67 sl@0: subblob-test 3.7 61626364656667 -1 10 67 sl@0: subblob-test 3.8 61626364656667 -5 3 636465 sl@0: subblob-test 3.9 61626364656667 -7 3 616263 sl@0: subblob-test 3.10 61626364656667 -100 98 6162636465 sl@0: sl@0: # If these blobs were strings, then they would contain multi-byte sl@0: # characters. But since they are blobs, the substr indices refer sl@0: # to bytes. sl@0: # sl@0: subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61 sl@0: subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1 sl@0: subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1 sl@0: subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96 sl@0: subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196 sl@0: subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391 sl@0: sl@0: # Two-argument SUBSTR sl@0: # sl@0: proc substr-2-test {id string idx result} { sl@0: db eval { sl@0: DELETE FROM t1; sl@0: INSERT INTO t1(t) VALUES($string) sl@0: } sl@0: do_test substr-$id.1 [subst { sl@0: execsql { sl@0: SELECT substr(t, $idx) FROM t1 sl@0: } sl@0: }] [list $result] sl@0: set qstr '[string map {' ''} $string]' sl@0: do_test substr-$id.2 [subst { sl@0: execsql { sl@0: SELECT substr($qstr, $idx) sl@0: } sl@0: }] [list $result] sl@0: } sl@0: substr-2-test 5.1 abcdefghijklmnop 5 efghijklmnop sl@0: substr-2-test 5.2 abcdef -5 bcdef sl@0: sl@0: finish_test