2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing the IN and BETWEEN operator.
2N/A#
2N/A# $Id: in.test,v 1.11 2004/01/15 03:30:25 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Generate the test data we will need for the first squences of tests.
2N/A#
2N/Ado_test in-1.0 {
2N/A set fd [open data1.txt w]
2N/A for {set i 1} {$i<=10} {incr i} {
2N/A puts $fd "$i\t[expr {int(pow(2,$i))}]"
2N/A }
2N/A close $fd
2N/A execsql {
2N/A CREATE TABLE t1(a int, b int);
2N/A COPY t1 FROM 'data1.txt';
2N/A }
2N/A file delete -force data1.txt
2N/A execsql {SELECT count(*) FROM t1}
2N/A} {10}
2N/A
2N/A# Do basic testing of BETWEEN.
2N/A#
2N/Ado_test in-1.1 {
2N/A execsql {SELECT a FROM t1 WHERE b BETWEEN 10 AND 50 ORDER BY a}
2N/A} {4 5}
2N/Ado_test in-1.2 {
2N/A execsql {SELECT a FROM t1 WHERE b NOT BETWEEN 10 AND 50 ORDER BY a}
2N/A} {1 2 3 6 7 8 9 10}
2N/Ado_test in-1.3 {
2N/A execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 ORDER BY a}
2N/A} {1 2 3 4}
2N/Ado_test in-1.4 {
2N/A execsql {SELECT a FROM t1 WHERE b NOT BETWEEN a AND a*5 ORDER BY a}
2N/A} {5 6 7 8 9 10}
2N/Ado_test in-1.6 {
2N/A execsql {SELECT a FROM t1 WHERE b BETWEEN a AND a*5 OR b=512 ORDER BY a}
2N/A} {1 2 3 4 9}
2N/Ado_test in-1.7 {
2N/A execsql {SELECT a+ 100*(a BETWEEN 1 and 3) FROM t1 ORDER BY b}
2N/A} {101 102 103 4 5 6 7 8 9 10}
2N/A
2N/A
2N/A# Testing of the IN operator using static lists on the right-hand side.
2N/A#
2N/Ado_test in-2.1 {
2N/A execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) ORDER BY a}
2N/A} {3 4 5}
2N/Ado_test in-2.2 {
2N/A execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) ORDER BY a}
2N/A} {1 2 6 7 8 9 10}
2N/Ado_test in-2.3 {
2N/A execsql {SELECT a FROM t1 WHERE b IN (8,12,16,24,32) OR b=512 ORDER BY a}
2N/A} {3 4 5 9}
2N/Ado_test in-2.4 {
2N/A execsql {SELECT a FROM t1 WHERE b NOT IN (8,12,16,24,32) OR b=512 ORDER BY a}
2N/A} {1 2 6 7 8 9 10}
2N/Ado_test in-2.5 {
2N/A execsql {SELECT a+100*(b IN (8,16,24)) FROM t1 ORDER BY b}
2N/A} {1 2 103 104 5 6 7 8 9 10}
2N/A
2N/Ado_test in-2.6 {
2N/A set v [catch {execsql {SELECT a FROM t1 WHERE b IN (b+10,20)}} msg]
2N/A lappend v $msg
2N/A} {1 {right-hand side of IN operator must be constant}}
2N/Ado_test in-2.7 {
2N/A set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10,b),20)}} msg]
2N/A lappend v $msg
2N/A} {1 {right-hand side of IN operator must be constant}}
2N/Ado_test in-2.8 {
2N/A execsql {SELECT a FROM t1 WHERE b IN (8*2,64/2) ORDER BY b}
2N/A} {4 5}
2N/Ado_test in-2.9 {
2N/A set v [catch {execsql {SELECT a FROM t1 WHERE b IN (max(5,10),20)}} msg]
2N/A lappend v $msg
2N/A} {1 {right-hand side of IN operator must be constant}}
2N/Ado_test in-2.10 {
2N/A set v [catch {execsql {SELECT a FROM t1 WHERE min(0,b IN (a,30))}} msg]
2N/A lappend v $msg
2N/A} {1 {right-hand side of IN operator must be constant}}
2N/Ado_test in-2.11 {
2N/A set v [catch {execsql {SELECT a FROM t1 WHERE c IN (10,20)}} msg]
2N/A lappend v $msg
2N/A} {1 {no such column: c}}
2N/A
2N/A# Testing the IN operator where the right-hand side is a SELECT
2N/A#
2N/Ado_test in-3.1 {
2N/A execsql {
2N/A SELECT a FROM t1
2N/A WHERE b IN (SELECT b FROM t1 WHERE a<5)
2N/A ORDER BY a
2N/A }
2N/A} {1 2 3 4}
2N/Ado_test in-3.2 {
2N/A execsql {
2N/A SELECT a FROM t1
2N/A WHERE b IN (SELECT b FROM t1 WHERE a<5) OR b==512
2N/A ORDER BY a
2N/A }
2N/A} {1 2 3 4 9}
2N/Ado_test in-3.3 {
2N/A execsql {
2N/A SELECT a + 100*(b IN (SELECT b FROM t1 WHERE a<5)) FROM t1 ORDER BY b
2N/A }
2N/A} {101 102 103 104 5 6 7 8 9 10}
2N/A
2N/A# Make sure the UPDATE and DELETE commands work with IN-SELECT
2N/A#
2N/Ado_test in-4.1 {
2N/A execsql {
2N/A UPDATE t1 SET b=b*2
2N/A WHERE b IN (SELECT b FROM t1 WHERE a>8)
2N/A }
2N/A execsql {SELECT b FROM t1 ORDER BY b}
2N/A} {2 4 8 16 32 64 128 256 1024 2048}
2N/Ado_test in-4.2 {
2N/A execsql {
2N/A DELETE FROM t1 WHERE b IN (SELECT b FROM t1 WHERE a>8)
2N/A }
2N/A execsql {SELECT a FROM t1 ORDER BY a}
2N/A} {1 2 3 4 5 6 7 8}
2N/Ado_test in-4.3 {
2N/A execsql {
2N/A DELETE FROM t1 WHERE b NOT IN (SELECT b FROM t1 WHERE a>4)
2N/A }
2N/A execsql {SELECT a FROM t1 ORDER BY a}
2N/A} {5 6 7 8}
2N/A
2N/A# Do an IN with a constant RHS but where the RHS has many, many
2N/A# elements. We need to test that collisions in the hash table
2N/A# are resolved properly.
2N/A#
2N/Ado_test in-5.1 {
2N/A execsql {
2N/A INSERT INTO t1 VALUES('hello', 'world');
2N/A SELECT * FROM t1
2N/A WHERE a IN (
2N/A 'Do','an','IN','with','a','constant','RHS','but','where','the',
2N/A 'has','many','elements','We','need','to','test','that',
2N/A 'collisions','hash','table','are','resolved','properly',
2N/A 'This','in-set','contains','thirty','one','entries','hello');
2N/A }
2N/A} {hello world}
2N/A
2N/A# Make sure the IN operator works with INTEGER PRIMARY KEY fields.
2N/A#
2N/Ado_test in-6.1 {
2N/A execsql {
2N/A CREATE TABLE ta(a INTEGER PRIMARY KEY, b);
2N/A INSERT INTO ta VALUES(1,1);
2N/A INSERT INTO ta VALUES(2,2);
2N/A INSERT INTO ta VALUES(3,3);
2N/A INSERT INTO ta VALUES(4,4);
2N/A INSERT INTO ta VALUES(6,6);
2N/A INSERT INTO ta VALUES(8,8);
2N/A INSERT INTO ta VALUES(10,
2N/A 'This is a key that is long enough to require a malloc in the VDBE');
2N/A SELECT * FROM ta WHERE a<10;
2N/A }
2N/A} {1 1 2 2 3 3 4 4 6 6 8 8}
2N/Ado_test in-6.2 {
2N/A execsql {
2N/A CREATE TABLE tb(a INTEGER PRIMARY KEY, b);
2N/A INSERT INTO tb VALUES(1,1);
2N/A INSERT INTO tb VALUES(2,2);
2N/A INSERT INTO tb VALUES(3,3);
2N/A INSERT INTO tb VALUES(5,5);
2N/A INSERT INTO tb VALUES(7,7);
2N/A INSERT INTO tb VALUES(9,9);
2N/A INSERT INTO tb VALUES(11,
2N/A 'This is a key that is long enough to require a malloc in the VDBE');
2N/A SELECT * FROM tb WHERE a<10;
2N/A }
2N/A} {1 1 2 2 3 3 5 5 7 7 9 9}
2N/Ado_test in-6.3 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE b IN (SELECT a FROM tb);
2N/A }
2N/A} {1 2 3}
2N/Ado_test in-6.4 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE b NOT IN (SELECT a FROM tb);
2N/A }
2N/A} {4 6 8 10}
2N/Ado_test in-6.5 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE b IN (SELECT b FROM tb);
2N/A }
2N/A} {1 2 3 10}
2N/Ado_test in-6.6 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE b NOT IN (SELECT b FROM tb);
2N/A }
2N/A} {4 6 8}
2N/Ado_test in-6.7 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a IN (SELECT a FROM tb);
2N/A }
2N/A} {1 2 3}
2N/Ado_test in-6.8 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a NOT IN (SELECT a FROM tb);
2N/A }
2N/A} {4 6 8 10}
2N/Ado_test in-6.9 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a IN (SELECT b FROM tb);
2N/A }
2N/A} {1 2 3}
2N/Ado_test in-6.10 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a NOT IN (SELECT b FROM tb);
2N/A }
2N/A} {4 6 8 10}
2N/A
2N/A# Tests of IN operator against empty sets. (Ticket #185)
2N/A#
2N/Ado_test in-7.1 {
2N/A execsql {
2N/A SELECT a FROM t1 WHERE a IN ();
2N/A }
2N/A} {}
2N/Ado_test in-7.2 {
2N/A execsql {
2N/A SELECT a FROM t1 WHERE a IN (5);
2N/A }
2N/A} {5}
2N/Ado_test in-7.3 {
2N/A execsql {
2N/A SELECT a FROM t1 WHERE a NOT IN () ORDER BY a;
2N/A }
2N/A} {5 6 7 8 hello}
2N/Ado_test in-7.4 {
2N/A execsql {
2N/A SELECT a FROM t1 WHERE a IN (5) AND b IN ();
2N/A }
2N/A} {}
2N/Ado_test in-7.5 {
2N/A execsql {
2N/A SELECT a FROM t1 WHERE a IN (5) AND b NOT IN ();
2N/A }
2N/A} {5}
2N/Ado_test in-7.6 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a IN ();
2N/A }
2N/A} {}
2N/Ado_test in-7.7 {
2N/A execsql {
2N/A SELECT a FROM ta WHERE a NOT IN ();
2N/A }
2N/A} {1 2 3 4 6 8 10}
2N/A
2N/Ado_test in-8.1 {
2N/A execsql {
2N/A SELECT b FROM t1 WHERE a IN ('hello','there')
2N/A }
2N/A} {world}
2N/Ado_test in-8.2 {
2N/A execsql {
2N/A SELECT b FROM t1 WHERE a IN ("hello",'there')
2N/A }
2N/A} {world}
2N/A
2N/A# Test constructs of the form: expr IN tablename
2N/A#
2N/Ado_test in-9.1 {
2N/A execsql {
2N/A CREATE TABLE t4 AS SELECT a FROM tb;
2N/A SELECT * FROM t4;
2N/A }
2N/A} {1 2 3 5 7 9 11}
2N/Ado_test in-9.2 {
2N/A execsql {
2N/A SELECT b FROM t1 WHERE a IN t4;
2N/A }
2N/A} {32 128}
2N/Ado_test in-9.3 {
2N/A execsql {
2N/A SELECT b FROM t1 WHERE a NOT IN t4;
2N/A }
2N/A} {64 256 world}
2N/Ado_test in-9.4 {
2N/A catchsql {
2N/A SELECT b FROM t1 WHERE a NOT IN tb;
2N/A }
2N/A} {1 {only a single result allowed for a SELECT that is part of an expression}}
2N/A
2N/Afinish_test