blob: a4bdd8b9d9c18b964f11b1394e0a25455d0236b2 [file] [log] [blame]
drh4794b982000-06-06 13:54:14 +00001# Copyright (c) 1999, 2000 D. Richard Hipp
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public
5# License as published by the Free Software Foundation; either
6# version 2 of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11# General Public License for more details.
12#
13# You should have received a copy of the GNU General Public
14# License along with this library; if not, write to the
15# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16# Boston, MA 02111-1307, USA.
17#
18# Author contact information:
19# drh@hwaci.com
20# http://www.hwaci.com/drh/
21#
22#***********************************************************************
23# This file implements regression tests for SQLite library. The
24# focus of this file is testing the IN and BETWEEN operator.
25#
drhc4a3c772001-04-04 11:48:57 +000026# $Id: in.test,v 1.4 2001/04/04 11:48:58 drh Exp $
drh4794b982000-06-06 13:54:14 +000027
28set testdir [file dirname $argv0]
29source $testdir/tester.tcl
30
31# Generate the test data we will need for the first squences of tests.
32#
33do_test in-1.0 {
34 set fd [open data1.txt w]
35 for {set i 1} {$i<=10} {incr i} {
36 puts $fd "$i\t[expr {int(pow(2,$i))}]"
37 }
38 close $fd
39 execsql {
40 CREATE TABLE t1(a int, b int);
41 COPY t1 FROM 'data1.txt';
42 }
43 file delete -force data1.txt
44 execsql {SELECT count(*) FROM t1}
45} {10}
46
47# Do basic testing of BETWEEN.
48#
49do_test in-1.1 {
50 execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
51} {4 5}
52do_test in-1.2 {
53 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a}
54} {1 2 3 6 7 8 9 10}
55do_test in-1.3 {
56 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a}
57} {1 2 3 4}
58do_test in-1.4 {
59 execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a}
60} {5 6 7 8 9 10}
61do_test in-1.6 {
62 execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a}
63} {1 2 3 4 9}
64do_test in-1.7 {
65 execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b}
66} {101 102 103 4 5 6 7 8 9 10}
67
68
69# Testing of the IN operator using static lists on the right-hand side.
70#
71do_test in-2.1 {
72 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a}
73} {3 4 5}
74do_test in-2.2 {
75 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a}
76} {1 2 6 7 8 9 10}
77do_test in-2.3 {
78 execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a}
79} {3 4 5 9}
80do_test in-2.4 {
81 execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a}
82} {1 2 6 7 8 9 10}
83do_test in-2.5 {
84 execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b}
85} {1 2 103 104 5 6 7 8 9 10}
86
87do_test in-2.6 {
88 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg]
89 lappend v $msg
90} {1 {right-hand side of IN operator must be constant}}
91do_test in-2.7 {
92 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg]
93 lappend v $msg
94} {1 {right-hand side of IN operator must be constant}}
95do_test in-2.8 {
96 execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
97} {4 5}
98do_test in-2.9 {
99 set v [catch {execsql {SELECT a FROM t1 WHERE b IN (xyz(5,10),20)}} msg]
100 lappend v $msg
101} {1 {no such function: xyz}}
102do_test in-2.10 {
103 set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg]
104 lappend v $msg
105} {1 {right-hand side of IN operator must be constant}}
106do_test in-2.11 {
107 set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
108 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000109} {1 {no such column: c}}
drh4794b982000-06-06 13:54:14 +0000110
111# Testing the IN operator where the right-hand side is a SELECT
112#
113do_test in-3.1 {
114 execsql {
115 SELECT a FROM t1
116 WHERE b IN (SELECT b FROM t1 WHERE a<5)
117 ORDER BY a
118 }
119} {1 2 3 4}
120do_test in-3.2 {
121 execsql {
122 SELECT a FROM t1
123 WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
124 ORDER BY a
125 }
126} {1 2 3 4 9}
127do_test in-3.3 {
128 execsql {
129 SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
130 }
131} {101 102 103 104 5 6 7 8 9 10}
132
133# Make sure the UPDATE and DELETE commands work with IN-SELECT
134#
135do_test in-4.1 {
136 execsql {
137 UPDATE t1 SET b=b*2
138 WHERE b IN (SELECT b FROM t1 WHERE a>8)
139 }
140 execsql {SELECT b FROM t1 ORDER BY b}
141} {2 4 8 16 32 64 128 256 1024 2048}
142do_test in-4.2 {
143 execsql {
144 DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
145 }
146 execsql {SELECT a FROM t1 ORDER BY a}
147} {1 2 3 4 5 6 7 8}
drhc4a3c772001-04-04 11:48:57 +0000148do_test in-4.3 {
149 execsql {
150 DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
151 }
152 execsql {SELECT a FROM t1 ORDER BY a}
153} {5 6 7 8}
drh4794b982000-06-06 13:54:14 +0000154
drhd8bc7082000-06-07 23:51:50 +0000155# Do an IN with a constant RHS but where the RHS has many, many
156# elements. We need to test that collisions in the hash table
157# are resolved properly.
158#
159do_test in-5.1 {
160 execsql {
161 INSERT INTO t1 VALUES('hello', 'world');
162 SELECT * FROM t1
163 WHERE a IN (
164 'Do','an','IN','with','a','constant','RHS','but','where','the',
165 'has','many','elements','We','need','to','test','that',
166 'collisions','hash','table','are','resolved','properly',
167 'This','in-set','contains','thirty','one','entries','hello');
168 }
169} {hello world}
drh4794b982000-06-06 13:54:14 +0000170
171finish_test