os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/substr.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2007 May 14
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#***********************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this file is testing the built-in SUBSTR() functions.
sl@0
    13
#
sl@0
    14
# $Id: substr.test,v 1.3 2007/10/12 19:11:55 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sl@0
    19
ifcapable !tclvar {
sl@0
    20
  finish_test
sl@0
    21
  return
sl@0
    22
}
sl@0
    23
sl@0
    24
# Create a table to work with.
sl@0
    25
#
sl@0
    26
execsql { 
sl@0
    27
  CREATE TABLE t1(t text, b blob)
sl@0
    28
}
sl@0
    29
proc substr-test {id string i1 i2 result} {
sl@0
    30
  db eval {
sl@0
    31
    DELETE FROM t1;
sl@0
    32
    INSERT INTO t1(t) VALUES($string)
sl@0
    33
  }
sl@0
    34
  do_test substr-$id.1 [subst {
sl@0
    35
    execsql {
sl@0
    36
      SELECT substr(t, $i1, $i2) FROM t1
sl@0
    37
    }
sl@0
    38
  }] [list $result]
sl@0
    39
  set qstr '[string map {' ''} $string]'
sl@0
    40
  do_test substr-$id.2 [subst {
sl@0
    41
    execsql {
sl@0
    42
      SELECT substr($qstr, $i1, $i2)
sl@0
    43
    }
sl@0
    44
  }] [list $result]
sl@0
    45
}
sl@0
    46
proc subblob-test {id hex i1 i2 hexresult} {
sl@0
    47
  db eval "
sl@0
    48
    DELETE FROM t1;
sl@0
    49
    INSERT INTO t1(b) VALUES(x'$hex')
sl@0
    50
  "
sl@0
    51
  do_test substr-$id.1 [subst {
sl@0
    52
    execsql {
sl@0
    53
      SELECT hex(substr(b, $i1, $i2)) FROM t1
sl@0
    54
    }
sl@0
    55
  }] [list $hexresult]
sl@0
    56
  do_test substr-$id.2 [subst {
sl@0
    57
    execsql {
sl@0
    58
      SELECT hex(substr(x'$hex', $i1, $i2))
sl@0
    59
    }
sl@0
    60
  }] [list $hexresult]
sl@0
    61
}
sl@0
    62
sl@0
    63
# Basic SUBSTR functionality
sl@0
    64
#
sl@0
    65
substr-test 1.1 abcdefg 1 1 a
sl@0
    66
substr-test 1.2 abcdefg 2 1 b
sl@0
    67
substr-test 1.3 abcdefg 1 2 ab
sl@0
    68
substr-test 1.4 abcdefg 1 100 abcdefg
sl@0
    69
substr-test 1.5 abcdefg 0 1 a
sl@0
    70
substr-test 1.6 abcdefg -1 1 g
sl@0
    71
substr-test 1.7 abcdefg -1 10 g
sl@0
    72
substr-test 1.8 abcdefg -5 3 cde
sl@0
    73
substr-test 1.9 abcdefg -7 3 abc
sl@0
    74
substr-test 1.10 abcdefg -100 98 abcde
sl@0
    75
sl@0
    76
# Make sure everything works with long unicode characters
sl@0
    77
#
sl@0
    78
substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234
sl@0
    79
substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345
sl@0
    80
substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345
sl@0
    81
substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456
sl@0
    82
substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c
sl@0
    83
sl@0
    84
# Basic functionality for BLOBs
sl@0
    85
#
sl@0
    86
subblob-test 3.1 61626364656667 1 1 61
sl@0
    87
subblob-test 3.2 61626364656667 2 1 62
sl@0
    88
subblob-test 3.3 61626364656667 1 2 6162
sl@0
    89
subblob-test 3.4 61626364656667 1 100 61626364656667
sl@0
    90
subblob-test 3.5 61626364656667 0 1 61
sl@0
    91
subblob-test 3.6 61626364656667 -1 1 67
sl@0
    92
subblob-test 3.7 61626364656667 -1 10 67
sl@0
    93
subblob-test 3.8 61626364656667 -5 3 636465
sl@0
    94
subblob-test 3.9 61626364656667 -7 3 616263
sl@0
    95
subblob-test 3.10 61626364656667 -100 98 6162636465
sl@0
    96
sl@0
    97
# If these blobs were strings, then they would contain multi-byte
sl@0
    98
# characters.  But since they are blobs, the substr indices refer
sl@0
    99
# to bytes.
sl@0
   100
#
sl@0
   101
subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61
sl@0
   102
subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1
sl@0
   103
subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1
sl@0
   104
subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96
sl@0
   105
subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196
sl@0
   106
subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391 
sl@0
   107
sl@0
   108
# Two-argument SUBSTR
sl@0
   109
#
sl@0
   110
proc substr-2-test {id string idx result} {
sl@0
   111
  db eval {
sl@0
   112
    DELETE FROM t1;
sl@0
   113
    INSERT INTO t1(t) VALUES($string)
sl@0
   114
  }
sl@0
   115
  do_test substr-$id.1 [subst {
sl@0
   116
    execsql {
sl@0
   117
      SELECT substr(t, $idx) FROM t1
sl@0
   118
    }
sl@0
   119
  }] [list $result]
sl@0
   120
  set qstr '[string map {' ''} $string]'
sl@0
   121
  do_test substr-$id.2 [subst {
sl@0
   122
    execsql {
sl@0
   123
      SELECT substr($qstr, $idx)
sl@0
   124
    }
sl@0
   125
  }] [list $result]
sl@0
   126
}
sl@0
   127
substr-2-test 5.1 abcdefghijklmnop 5 efghijklmnop
sl@0
   128
substr-2-test 5.2 abcdef -5 bcdef
sl@0
   129
sl@0
   130
finish_test