drh | f764e6f | 2007-05-15 01:13:47 +0000 | [diff] [blame^] | 1 | # 2007 May 14 |
| 2 | # |
| 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # 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. The |
| 12 | # focus of this file is testing the built-in SUBSTR() functions. |
| 13 | # |
| 14 | # $Id: substr.test,v 1.1 2007/05/15 01:13:47 drh Exp $ |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Create a table to work with. |
| 20 | # |
| 21 | execsql { |
| 22 | CREATE TABLE t1(t text, b blob) |
| 23 | } |
| 24 | proc substr-test {id string i1 i2 result} { |
| 25 | db eval { |
| 26 | DELETE FROM t1; |
| 27 | INSERT INTO t1(t) VALUES($string) |
| 28 | } |
| 29 | do_test substr-$id.1 [subst { |
| 30 | execsql { |
| 31 | SELECT substr(t, $i1, $i2) FROM t1 |
| 32 | } |
| 33 | }] [list $result] |
| 34 | set qstr '[string map {' ''} $string]' |
| 35 | do_test substr-$id.2 [subst { |
| 36 | execsql { |
| 37 | SELECT substr($qstr, $i1, $i2) |
| 38 | } |
| 39 | }] [list $result] |
| 40 | } |
| 41 | proc subblob-test {id hex i1 i2 hexresult} { |
| 42 | db eval " |
| 43 | DELETE FROM t1; |
| 44 | INSERT INTO t1(b) VALUES(x'$hex') |
| 45 | " |
| 46 | do_test substr-$id.1 [subst { |
| 47 | execsql { |
| 48 | SELECT hex(substr(b, $i1, $i2)) FROM t1 |
| 49 | } |
| 50 | }] [list $hexresult] |
| 51 | do_test substr-$id.2 [subst { |
| 52 | execsql { |
| 53 | SELECT hex(substr(x'$hex', $i1, $i2)) |
| 54 | } |
| 55 | }] [list $hexresult] |
| 56 | } |
| 57 | |
| 58 | # Basic SUBSTR functionality |
| 59 | # |
| 60 | substr-test 1.1 abcdefg 1 1 a |
| 61 | substr-test 1.2 abcdefg 2 1 b |
| 62 | substr-test 1.3 abcdefg 1 2 ab |
| 63 | substr-test 1.4 abcdefg 1 100 abcdefg |
| 64 | substr-test 1.5 abcdefg 0 1 a |
| 65 | substr-test 1.6 abcdefg -1 1 g |
| 66 | substr-test 1.7 abcdefg -1 10 g |
| 67 | substr-test 1.8 abcdefg -5 3 cde |
| 68 | substr-test 1.9 abcdefg -7 3 abc |
| 69 | substr-test 1.10 abcdefg -100 98 abcde |
| 70 | |
| 71 | # Make sure everything works with long unicode characters |
| 72 | # |
| 73 | substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234 |
| 74 | substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345 |
| 75 | substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345 |
| 76 | substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456 |
| 77 | substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c |
| 78 | |
| 79 | # Basic functionality for BLOBs |
| 80 | # |
| 81 | subblob-test 3.1 61626364656667 1 1 61 |
| 82 | subblob-test 3.2 61626364656667 2 1 62 |
| 83 | subblob-test 3.3 61626364656667 1 2 6162 |
| 84 | subblob-test 3.4 61626364656667 1 100 61626364656667 |
| 85 | subblob-test 3.5 61626364656667 0 1 61 |
| 86 | subblob-test 3.6 61626364656667 -1 1 67 |
| 87 | subblob-test 3.7 61626364656667 -1 10 67 |
| 88 | subblob-test 3.8 61626364656667 -5 3 636465 |
| 89 | subblob-test 3.9 61626364656667 -7 3 616263 |
| 90 | subblob-test 3.10 61626364656667 -100 98 6162636465 |
| 91 | |
| 92 | # If these blobs were strings, then they would contain multi-byte |
| 93 | # characters. But since they are blobs, the substr indices refer |
| 94 | # to bytes. |
| 95 | # |
| 96 | subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61 |
| 97 | subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1 |
| 98 | subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1 |
| 99 | subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96 |
| 100 | subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196 |
| 101 | subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391 |
| 102 | |
| 103 | finish_test |