substr.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.7 2009/02/03 13:10:54 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !tclvar {
  18. finish_test
  19. return
  20. }
  21. # Create a table to work with.
  22. #
  23. execsql {
  24. CREATE TABLE t1(t text, b blob)
  25. }
  26. proc substr-test {id string i1 i2 result} {
  27. db eval {
  28. DELETE FROM t1;
  29. INSERT INTO t1(t) VALUES($string)
  30. }
  31. do_test substr-$id.1 [subst {
  32. execsql {
  33. SELECT substr(t, $i1, $i2) FROM t1
  34. }
  35. }] [list $result]
  36. set qstr '[string map {' ''} $string]'
  37. do_test substr-$id.2 [subst {
  38. execsql {
  39. SELECT substr($qstr, $i1, $i2)
  40. }
  41. }] [list $result]
  42. }
  43. proc subblob-test {id hex i1 i2 hexresult} {
  44. db eval "
  45. DELETE FROM t1;
  46. INSERT INTO t1(b) VALUES(x'$hex')
  47. "
  48. do_test substr-$id.1 [subst {
  49. execsql {
  50. SELECT hex(substr(b, $i1, $i2)) FROM t1
  51. }
  52. }] [list $hexresult]
  53. do_test substr-$id.2 [subst {
  54. execsql {
  55. SELECT hex(substr(x'$hex', $i1, $i2))
  56. }
  57. }] [list $hexresult]
  58. }
  59. # Basic SUBSTR functionality
  60. #
  61. substr-test 1.1 abcdefg 1 1 a
  62. substr-test 1.2 abcdefg 2 1 b
  63. substr-test 1.3 abcdefg 1 2 ab
  64. substr-test 1.4 abcdefg 1 100 abcdefg
  65. substr-test 1.5 abcdefg 0 2 a
  66. substr-test 1.6 abcdefg -1 1 g
  67. substr-test 1.7 abcdefg -1 10 g
  68. substr-test 1.8 abcdefg -5 3 cde
  69. substr-test 1.9 abcdefg -7 3 abc
  70. substr-test 1.10 abcdefg -100 98 abcde
  71. substr-test 1.11 abcdefg 5 -1 d
  72. substr-test 1.12 abcdefg 5 -4 abcd
  73. substr-test 1.13 abcdefg 5 -5 abcd
  74. substr-test 1.14 abcdefg -5 -1 b
  75. substr-test 1.15 abcdefg -5 -2 ab
  76. substr-test 1.16 abcdefg -5 -3 ab
  77. substr-test 1.17 abcdefg 100 200 {}
  78. substr-test 1.18 abcdefg 200 100 {}
  79. # Make sure NULL is returned if any parameter is NULL
  80. #
  81. do_test substr-1.90 {
  82. db eval {SELECT ifnull(substr(NULL,1,1),'nil')}
  83. } nil
  84. do_test substr-1.91 {
  85. db eval {SELECT ifnull(substr(NULL,1),'nil')}
  86. } nil
  87. do_test substr-1.92 {
  88. db eval {SELECT ifnull(substr('abcdefg',NULL,1),'nil')}
  89. } nil
  90. do_test substr-1.93 {
  91. db eval {SELECT ifnull(substr('abcdefg',NULL),'nil')}
  92. } nil
  93. do_test substr-1.94 {
  94. db eval {SELECT ifnull(substr('abcdefg',1,NULL),'nil')}
  95. } nil
  96. # Make sure everything works with long unicode characters
  97. #
  98. substr-test 2.1 \u1234\u2345\u3456 1 1 \u1234
  99. substr-test 2.2 \u1234\u2345\u3456 2 1 \u2345
  100. substr-test 2.3 \u1234\u2345\u3456 1 2 \u1234\u2345
  101. substr-test 2.4 \u1234\u2345\u3456 -1 1 \u3456
  102. substr-test 2.5 a\u1234b\u2345c\u3456c -5 3 b\u2345c
  103. substr-test 2.6 a\u1234b\u2345c\u3456c -2 -3 b\u2345c
  104. # Basic functionality for BLOBs
  105. #
  106. subblob-test 3.1 61626364656667 1 1 61
  107. subblob-test 3.2 61626364656667 2 1 62
  108. subblob-test 3.3 61626364656667 1 2 6162
  109. subblob-test 3.4 61626364656667 1 100 61626364656667
  110. subblob-test 3.5 61626364656667 0 2 61
  111. subblob-test 3.6 61626364656667 -1 1 67
  112. subblob-test 3.7 61626364656667 -1 10 67
  113. subblob-test 3.8 61626364656667 -5 3 636465
  114. subblob-test 3.9 61626364656667 -7 3 616263
  115. subblob-test 3.10 61626364656667 -100 98 6162636465
  116. subblob-test 3.11 61626364656667 100 200 {}
  117. subblob-test 3.12 61626364656667 200 100 {}
  118. # If these blobs were strings, then they would contain multi-byte
  119. # characters. But since they are blobs, the substr indices refer
  120. # to bytes.
  121. #
  122. subblob-test 4.1 61E188B462E28D8563E3919663 1 1 61
  123. subblob-test 4.2 61E188B462E28D8563E3919663 2 1 E1
  124. subblob-test 4.3 61E188B462E28D8563E3919663 1 2 61E1
  125. subblob-test 4.4 61E188B462E28D8563E3919663 -2 1 96
  126. subblob-test 4.5 61E188B462E28D8563E3919663 -5 4 63E39196
  127. subblob-test 4.6 61E188B462E28D8563E3919663 -100 98 61E188B462E28D8563E391
  128. # Two-argument SUBSTR
  129. #
  130. proc substr-2-test {id string idx result} {
  131. db eval {
  132. DELETE FROM t1;
  133. INSERT INTO t1(t) VALUES($string)
  134. }
  135. do_test substr-$id.1 [subst {
  136. execsql {
  137. SELECT substr(t, $idx) FROM t1
  138. }
  139. }] [list $result]
  140. set qstr '[string map {' ''} $string]'
  141. do_test substr-$id.2 [subst {
  142. execsql {
  143. SELECT substr($qstr, $idx)
  144. }
  145. }] [list $result]
  146. }
  147. substr-2-test 5.1 abcdefghijklmnop 5 efghijklmnop
  148. substr-2-test 5.2 abcdef -5 bcdef
  149. finish_test