blob: 14c013ffc93250e8bd65d99bf24b976a61369660 [file] [log] [blame]
drh8abed7b2018-02-26 18:49:05 +00001# 2018-02-26
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 expressions of the form
13#
14# x IS TRUE
15# x IS FALSE
16# x IS NOT TRUE
17# x IS NOT FALSE
18#
19# Tests are also included for the use of TRUE and FALSE as
20# literal values.
21
22set testdir [file dirname $argv0]
23source $testdir/tester.tcl
24
25do_execsql_test istrue-100 {
26 CREATE TABLE t1(x INTEGER PRIMARY KEY, y BOOLEAN);
27 INSERT INTO t1 VALUES(1, true),(2, false),(3, null);
28 SELECT x FROM t1 WHERE y IS TRUE;
29} {1}
30do_execsql_test istrue-110 {
31 SELECT x FROM t1 WHERE y IS FALSE;
32} {2}
33do_execsql_test istrue-120 {
34 SELECT x FROM t1 WHERE y IS NULL;
35} {3}
36do_execsql_test istrue-130 {
37 SELECT x FROM t1 WHERE y IS NOT TRUE;
38} {2 3}
39do_execsql_test istrue-140 {
40 SELECT x FROM t1 WHERE y IS NOT FALSE;
41} {1 3}
42do_execsql_test istrue-150 {
43 SELECT x FROM t1 WHERE y IS NOT NULL;
44} {1 2}
45unset -nocomplain X
46set X 9
47do_execsql_test istrue-160 {
48 SELECT x FROM t1 WHERE y IS TRUE OR (8==$X)
49} {1}
50do_execsql_test istrue-170 {
51 SELECT x FROM t1 WHERE y IS FALSE OR (8==$X)
52} {2}
53do_execsql_test istrue-180 {
54 SELECT x FROM t1 WHERE y IS NULL OR (8==$X);
55} {3}
56do_execsql_test istrue-190 {
57 SELECT x FROM t1 WHERE y IS NOT TRUE OR (8==$X);
58} {2 3}
59do_execsql_test istrue-200 {
60 SELECT x FROM t1 WHERE y IS NOT FALSE OR (8==$X);
61} {1 3}
62do_execsql_test istrue-210 {
63 SELECT x FROM t1 WHERE y IS NOT NULL OR (8==$X);
64} {1 2}
65
66do_execsql_test istrue-300 {
67 SELECT x,
68 y IS TRUE, y IS FALSE, y is NULL,
69 y IS NOT TRUE, y IS NOT FALSE, y IS NOT NULL, '|'
70 FROM t1 ORDER BY x;
71} {1 1 0 0 0 1 1 | 2 0 1 0 1 0 1 | 3 0 0 1 1 1 0 |}
72
73do_execsql_test istrue-400 {
74 SELECT x FROM t1 WHERE true;
75} {1 2 3}
76do_execsql_test istrue-410 {
77 SELECT x FROM t1 WHERE false;
78} {}
79
drh171d16b2018-02-26 20:15:54 +000080do_execsql_test istrue-500 {
81 CREATE TABLE t2(
82 a INTEGER PRIMARY KEY,
83 b BOOLEAN DEFAULT true,
84 c BOOLEAN DEFAULT(true),
85 d BOOLEAN DEFAULT false,
86 e BOOLEAN DEFAULT(false)
87 );
88 INSERT INTO t2 DEFAULT VALUES;
89 SELECT * FROM t2;
90} {1 1 1 0 0}
drh43c4ac82018-02-26 21:26:27 +000091do_execsql_test istrue-510 {
92 DROP TABLE t2;
93 CREATE TABLE t2(
94 a INTEGER PRIMARY KEY,
95 b BOOLEAN DEFAULT(not true),
96 c BOOLEAN DEFAULT(not false)
97 );
98 INSERT INTO t2(a) VALUES(99);
99 SELECT * FROM t2;
100} {99 0 1}
101do_execsql_test istrue-520 {
102 DROP TABLE t2;
103 CREATE TABLE t2(
104 a INTEGER PRIMARY KEY,
105 b BOOLEAN CHECK(b IS TRUE),
106 c BOOLEAN CHECK(c IS FALSE),
107 d BOOLEAN CHECK(d IS NOT TRUE),
108 e BOOLEAN CHECK(e IS NOT FALSE)
109 );
110 INSERT INTO t2 VALUES(1,true,false,null,null);
111 SELECT * FROM t2;
112} {1 1 0 {} {}}
113do_catchsql_test istrue-521 {
114 INSERT INTO t2 VALUES(2,false,false,null,null);
115} {1 {CHECK constraint failed: t2}}
116do_catchsql_test istrue-522 {
117 INSERT INTO t2 VALUES(2,true,true,null,null);
118} {1 {CHECK constraint failed: t2}}
119do_catchsql_test istrue-523 {
120 INSERT INTO t2 VALUES(2,true,false,true,null);
121} {1 {CHECK constraint failed: t2}}
122do_catchsql_test istrue-524 {
123 INSERT INTO t2 VALUES(2,true,false,null,false);
124} {1 {CHECK constraint failed: t2}}
drh8abed7b2018-02-26 18:49:05 +0000125
mistachkincf847d42018-03-23 14:56:05 +0000126foreach {tn val} [list 1 NaN 2 -NaN 3 NaN0 4 -NaN0 5 Inf 6 -Inf] {
127 do_execsql_test istrue-600.$tn.1 {
128 DROP TABLE IF EXISTS t1;
129 CREATE TABLE t1(x);
130 }
131 do_test istrue-600.$tn.2 {
132 set ::STMT [sqlite3_prepare db "INSERT INTO t1 VALUES(?)" -1 TAIL]
133 sqlite3_bind_double $::STMT 1 $val
134 sqlite3_step $::STMT
135 sqlite3_reset $::STMT
136 sqlite3_finalize $::STMT
137 } {SQLITE_OK}
138 do_execsql_test istrue-600.$tn.3 {
139 SELECT x IS TRUE FROM t1;
140 } [expr {$tn in [list 5 6] ? {1} : {0}}]
141 do_execsql_test istrue-600.$tn.4 {
142 SELECT x IS FALSE FROM t1;
143 } {0}
144}
145
drh3bc43152018-04-18 11:35:35 +0000146do_execsql_test istrue-700 {
147 CREATE TABLE t7(
148 a INTEGER PRIMARY KEY,
149 b BOOLEAN DEFAULT false,
150 c BOOLEAN DEFAULT true
151 );
152 INSERT INTO t7(a) VALUES(1);
153 INSERT INTO t7(a,b,c) VALUES(2,true,false);
154 ALTER TABLE t7 ADD COLUMN d BOOLEAN DEFAULT false;
155 ALTER TABLE t7 ADD COLUMN e BOOLEAN DEFAULT true;
156 INSERT INTO t7(a,b,c) VALUES(3,true,false);
157 INSERT INTO t7 VALUES(4,false,true,true,false);
158 SELECT *,'x' FROM t7 ORDER BY a;
159} {1 0 1 0 1 x 2 1 0 0 1 x 3 1 0 0 1 x 4 0 1 1 0 x}
160
drh8abed7b2018-02-26 18:49:05 +0000161finish_test