blob: e8c9417f651421a851b3f09375b43d84133a45a3 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh4794b982000-06-06 13:54:14 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh4794b982000-06-06 13:54:14 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh4794b982000-06-06 13:54:14 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the IN and BETWEEN operator.
13#
drhb19a2bc2001-09-16 00:13:26 +000014# $Id: in.test,v 1.5 2001/09/16 00:13:28 drh Exp $
drh4794b982000-06-06 13:54:14 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Generate the test data we will need for the first squences of tests.
20#
21do_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#
37do_test in-1.1 {
38 execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
39} {4 5}
40do_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}
43do_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}
46do_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}
49do_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}
52do_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#
59do_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}
62do_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}
65do_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}
68do_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}
71do_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
75do_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}}
79do_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}}
83do_test in-2.8 {
84 execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
85} {4 5}
86do_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}}
90do_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}}
94do_test in-2.11 {
95 set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
96 lappend v $msg
drh967e8b72000-06-21 13:59:10 +000097} {1 {no such column: c}}
drh4794b982000-06-06 13:54:14 +000098
99# Testing the IN operator where the right-hand side is a SELECT
100#
101do_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}
108do_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}
115do_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#
123do_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}
130do_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}
drhc4a3c772001-04-04 11:48:57 +0000136do_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}
drh4794b982000-06-06 13:54:14 +0000142
drhd8bc7082000-06-07 23:51:50 +0000143# 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#
147do_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}
drh4794b982000-06-06 13:54:14 +0000158
159finish_test