drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 1 | # 2001 September 15 |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 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. |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 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 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame^] | 14 | # $Id: in.test,v 1.5 2001/09/16 00:13:28 drh Exp $ |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Generate the test data we will need for the first squences of tests. |
| 20 | # |
| 21 | do_test in-1.0 { |
| 22 | set fd [open data1.txt w] |
| 23 | for {set i 1} {$i<=10} {incr i} { |
| 24 | puts $fd "$i\t[expr {int(pow(2,$i))}]" |
| 25 | } |
| 26 | close $fd |
| 27 | execsql { |
| 28 | CREATE TABLE t1(a int, b int); |
| 29 | COPY t1 FROM 'data1.txt'; |
| 30 | } |
| 31 | file delete -force data1.txt |
| 32 | execsql {SELECT count(*) FROM t1} |
| 33 | } {10} |
| 34 | |
| 35 | # Do basic testing of BETWEEN. |
| 36 | # |
| 37 | do_test in-1.1 { |
| 38 | execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a} |
| 39 | } {4 5} |
| 40 | do_test in-1.2 { |
| 41 | execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a} |
| 42 | } {1 2 3 6 7 8 9 10} |
| 43 | do_test in-1.3 { |
| 44 | execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a} |
| 45 | } {1 2 3 4} |
| 46 | do_test in-1.4 { |
| 47 | execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a} |
| 48 | } {5 6 7 8 9 10} |
| 49 | do_test in-1.6 { |
| 50 | execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a} |
| 51 | } {1 2 3 4 9} |
| 52 | do_test in-1.7 { |
| 53 | execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b} |
| 54 | } {101 102 103 4 5 6 7 8 9 10} |
| 55 | |
| 56 | |
| 57 | # Testing of the IN operator using static lists on the right-hand side. |
| 58 | # |
| 59 | do_test in-2.1 { |
| 60 | execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a} |
| 61 | } {3 4 5} |
| 62 | do_test in-2.2 { |
| 63 | execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a} |
| 64 | } {1 2 6 7 8 9 10} |
| 65 | do_test in-2.3 { |
| 66 | execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a} |
| 67 | } {3 4 5 9} |
| 68 | do_test in-2.4 { |
| 69 | execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a} |
| 70 | } {1 2 6 7 8 9 10} |
| 71 | do_test in-2.5 { |
| 72 | execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b} |
| 73 | } {1 2 103 104 5 6 7 8 9 10} |
| 74 | |
| 75 | do_test in-2.6 { |
| 76 | set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg] |
| 77 | lappend v $msg |
| 78 | } {1 {right-hand side of IN operator must be constant}} |
| 79 | do_test in-2.7 { |
| 80 | set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg] |
| 81 | lappend v $msg |
| 82 | } {1 {right-hand side of IN operator must be constant}} |
| 83 | do_test in-2.8 { |
| 84 | execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b} |
| 85 | } {4 5} |
| 86 | do_test in-2.9 { |
| 87 | set v [catch {execsql {SELECT a FROM t1 WHERE b IN (xyz(5,10),20)}} msg] |
| 88 | lappend v $msg |
| 89 | } {1 {no such function: xyz}} |
| 90 | do_test in-2.10 { |
| 91 | set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg] |
| 92 | lappend v $msg |
| 93 | } {1 {right-hand side of IN operator must be constant}} |
| 94 | do_test in-2.11 { |
| 95 | set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg] |
| 96 | lappend v $msg |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 97 | } {1 {no such column: c}} |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 98 | |
| 99 | # Testing the IN operator where the right-hand side is a SELECT |
| 100 | # |
| 101 | do_test in-3.1 { |
| 102 | execsql { |
| 103 | SELECT a FROM t1 |
| 104 | WHERE b IN (SELECT b FROM t1 WHERE a<5) |
| 105 | ORDER BY a |
| 106 | } |
| 107 | } {1 2 3 4} |
| 108 | do_test in-3.2 { |
| 109 | execsql { |
| 110 | SELECT a FROM t1 |
| 111 | WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512 |
| 112 | ORDER BY a |
| 113 | } |
| 114 | } {1 2 3 4 9} |
| 115 | do_test in-3.3 { |
| 116 | execsql { |
| 117 | SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b |
| 118 | } |
| 119 | } {101 102 103 104 5 6 7 8 9 10} |
| 120 | |
| 121 | # Make sure the UPDATE and DELETE commands work with IN-SELECT |
| 122 | # |
| 123 | do_test in-4.1 { |
| 124 | execsql { |
| 125 | UPDATE t1 SET b=b*2 |
| 126 | WHERE b IN (SELECT b FROM t1 WHERE a>8) |
| 127 | } |
| 128 | execsql {SELECT b FROM t1 ORDER BY b} |
| 129 | } {2 4 8 16 32 64 128 256 1024 2048} |
| 130 | do_test in-4.2 { |
| 131 | execsql { |
| 132 | DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8) |
| 133 | } |
| 134 | execsql {SELECT a FROM t1 ORDER BY a} |
| 135 | } {1 2 3 4 5 6 7 8} |
drh | c4a3c77 | 2001-04-04 11:48:57 +0000 | [diff] [blame] | 136 | do_test in-4.3 { |
| 137 | execsql { |
| 138 | DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4) |
| 139 | } |
| 140 | execsql {SELECT a FROM t1 ORDER BY a} |
| 141 | } {5 6 7 8} |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 142 | |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 143 | # Do an IN with a constant RHS but where the RHS has many, many |
| 144 | # elements. We need to test that collisions in the hash table |
| 145 | # are resolved properly. |
| 146 | # |
| 147 | do_test in-5.1 { |
| 148 | execsql { |
| 149 | INSERT INTO t1 VALUES('hello', 'world'); |
| 150 | SELECT * FROM t1 |
| 151 | WHERE a IN ( |
| 152 | 'Do','an','IN','with','a','constant','RHS','but','where','the', |
| 153 | 'has','many','elements','We','need','to','test','that', |
| 154 | 'collisions','hash','table','are','resolved','properly', |
| 155 | 'This','in-set','contains','thirty','one','entries','hello'); |
| 156 | } |
| 157 | } {hello world} |
drh | 4794b98 | 2000-06-06 13:54:14 +0000 | [diff] [blame] | 158 | |
| 159 | finish_test |