in.test 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # 2001 September 15
  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 IN and BETWEEN operator.
  13. #
  14. # $Id: in.test,v 1.6 2002/01/28 15:53:05 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Generate the test data we will need for the first squences of tests.
  18. #
  19. do_test in-1.0 {
  20. set fd [open data1.txt w]
  21. for {set i 1} {$i<=10} {incr i} {
  22. puts $fd "$i\t[expr {int(pow(2,$i))}]"
  23. }
  24. close $fd
  25. execsql {
  26. CREATE TABLE t1(a int, b int);
  27. COPY t1 FROM 'data1.txt';
  28. }
  29. file delete -force data1.txt
  30. execsql {SELECT count(*) FROM t1}
  31. } {10}
  32. # Do basic testing of BETWEEN.
  33. #
  34. do_test in-1.1 {
  35. execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
  36. } {4 5}
  37. do_test in-1.2 {
  38. execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a}
  39. } {1 2 3 6 7 8 9 10}
  40. do_test in-1.3 {
  41. execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a}
  42. } {1 2 3 4}
  43. do_test in-1.4 {
  44. execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a}
  45. } {5 6 7 8 9 10}
  46. do_test in-1.6 {
  47. execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a}
  48. } {1 2 3 4 9}
  49. do_test in-1.7 {
  50. execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b}
  51. } {101 102 103 4 5 6 7 8 9 10}
  52. # Testing of the IN operator using static lists on the right-hand side.
  53. #
  54. do_test in-2.1 {
  55. execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a}
  56. } {3 4 5}
  57. do_test in-2.2 {
  58. execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a}
  59. } {1 2 6 7 8 9 10}
  60. do_test in-2.3 {
  61. execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a}
  62. } {3 4 5 9}
  63. do_test in-2.4 {
  64. execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a}
  65. } {1 2 6 7 8 9 10}
  66. do_test in-2.5 {
  67. execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b}
  68. } {1 2 103 104 5 6 7 8 9 10}
  69. do_test in-2.6 {
  70. set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg]
  71. lappend v $msg
  72. } {1 {right-hand side of IN operator must be constant}}
  73. do_test in-2.7 {
  74. set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg]
  75. lappend v $msg
  76. } {1 {right-hand side of IN operator must be constant}}
  77. do_test in-2.8 {
  78. execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
  79. } {4 5}
  80. do_test in-2.9 {
  81. set v [catch {execsql {SELECT a FROM t1 WHERE b IN (xyz(5,10),20)}} msg]
  82. lappend v $msg
  83. } {1 {no such function: xyz}}
  84. do_test in-2.10 {
  85. set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg]
  86. lappend v $msg
  87. } {1 {right-hand side of IN operator must be constant}}
  88. do_test in-2.11 {
  89. set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
  90. lappend v $msg
  91. } {1 {no such column: c}}
  92. # Testing the IN operator where the right-hand side is a SELECT
  93. #
  94. do_test in-3.1 {
  95. execsql {
  96. SELECT a FROM t1
  97. WHERE b IN (SELECT b FROM t1 WHERE a<5)
  98. ORDER BY a
  99. }
  100. } {1 2 3 4}
  101. do_test in-3.2 {
  102. execsql {
  103. SELECT a FROM t1
  104. WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
  105. ORDER BY a
  106. }
  107. } {1 2 3 4 9}
  108. do_test in-3.3 {
  109. execsql {
  110. SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
  111. }
  112. } {101 102 103 104 5 6 7 8 9 10}
  113. # Make sure the UPDATE and DELETE commands work with IN-SELECT
  114. #
  115. do_test in-4.1 {
  116. execsql {
  117. UPDATE t1 SET b=b*2
  118. WHERE b IN (SELECT b FROM t1 WHERE a>8)
  119. }
  120. execsql {SELECT b FROM t1 ORDER BY b}
  121. } {2 4 8 16 32 64 128 256 1024 2048}
  122. do_test in-4.2 {
  123. execsql {
  124. DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
  125. }
  126. execsql {SELECT a FROM t1 ORDER BY a}
  127. } {1 2 3 4 5 6 7 8}
  128. do_test in-4.3 {
  129. execsql {
  130. DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
  131. }
  132. execsql {SELECT a FROM t1 ORDER BY a}
  133. } {5 6 7 8}
  134. # Do an IN with a constant RHS but where the RHS has many, many
  135. # elements. We need to test that collisions in the hash table
  136. # are resolved properly.
  137. #
  138. do_test in-5.1 {
  139. execsql {
  140. INSERT INTO t1 VALUES('hello', 'world');
  141. SELECT * FROM t1
  142. WHERE a IN (
  143. 'Do','an','IN','with','a','constant','RHS','but','where','the',
  144. 'has','many','elements','We','need','to','test','that',
  145. 'collisions','hash','table','are','resolved','properly',
  146. 'This','in-set','contains','thirty','one','entries','hello');
  147. }
  148. } {hello world}
  149. # Make sure the IN operator works with INTEGER PRIMARY KEY fields.
  150. #
  151. do_test in-6.1 {
  152. execsql {
  153. CREATE TABLE ta(a INTEGER PRIMARY KEY, b);
  154. INSERT INTO ta VALUES(1,1);
  155. INSERT INTO ta VALUES(2,2);
  156. INSERT INTO ta VALUES(3,3);
  157. INSERT INTO ta VALUES(4,4);
  158. INSERT INTO ta VALUES(6,6);
  159. INSERT INTO ta VALUES(8,8);
  160. SELECT * FROM ta;
  161. }
  162. } {1 1 2 2 3 3 4 4 6 6 8 8}
  163. do_test in-6.2 {
  164. execsql {
  165. CREATE TABLE tb(a INTEGER PRIMARY KEY, b);
  166. INSERT INTO tb VALUES(1,1);
  167. INSERT INTO tb VALUES(2,2);
  168. INSERT INTO tb VALUES(3,3);
  169. INSERT INTO tb VALUES(5,5);
  170. INSERT INTO tb VALUES(7,7);
  171. INSERT INTO tb VALUES(9,9);
  172. SELECT * FROM tb;
  173. }
  174. } {1 1 2 2 3 3 5 5 7 7 9 9}
  175. do_test in-6.3 {
  176. execsql {
  177. SELECT a FROM ta WHERE b IN (SELECT a FROM tb);
  178. }
  179. } {1 2 3}
  180. do_test in-6.4 {
  181. execsql {
  182. SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb);
  183. }
  184. } {4 6 8}
  185. do_test in-6.5 {
  186. execsql {
  187. SELECT a FROM ta WHERE b IN (SELECT b FROM tb);
  188. }
  189. } {1 2 3}
  190. do_test in-6.6 {
  191. execsql {
  192. SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb);
  193. }
  194. } {4 6 8}
  195. do_test in-6.7 {
  196. execsql {
  197. SELECT a FROM ta WHERE a IN (SELECT a FROM tb);
  198. }
  199. } {1 2 3}
  200. do_test in-6.8 {
  201. execsql {
  202. SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb);
  203. }
  204. } {4 6 8}
  205. do_test in-6.9 {
  206. execsql {
  207. SELECT a FROM ta WHERE a IN (SELECT b FROM tb);
  208. }
  209. } {1 2 3}
  210. do_test in-6.10 {
  211. execsql {
  212. SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb);
  213. }
  214. } {4 6 8}
  215. finish_test