2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library. The
15 # focus of this file is testing built-in functions.
17 # $Id: func.test,v 1.16.2.2 2004/07/18 21:14:05 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Create a table to work with.
25 execsql {CREATE TABLE tbl1(t1 text)}
26 foreach word {this program is free software} {
27 execsql "INSERT INTO tbl1 VALUES('$word')"
29 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
30 } {free is program software this}
34 INSERT INTO t2 VALUES(1);
35 INSERT INTO t2 VALUES(NULL);
36 INSERT INTO t2 VALUES(345);
37 INSERT INTO t2 VALUES(NULL);
38 INSERT INTO t2 VALUES(67890);
43 # Check out the length() function
46 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
49 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
51 } {1 {wrong number of arguments to function length()}}
53 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
55 } {1 {wrong number of arguments to function length()}}
57 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
61 execsql {SELECT coalesce(length(a),-1) FROM t2}
64 # Check out the substr() function
67 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
70 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
73 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
76 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
79 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
82 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
85 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
88 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
91 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
92 } {this software free program is}
94 execsql {SELECT substr(a,1,1) FROM t2}
97 execsql {SELECT substr(a,2,2) FROM t2}
100 # Only do the following tests if TCL has UTF-8 capabilities and
101 # the UTF-8 encoding is turned on in the SQLite library.
103 if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
105 # Put some UTF-8 characters in the database
108 execsql {DELETE FROM tbl1}
109 foreach word "contains UTF-8 characters hi\u1234ho" {
110 execsql "INSERT INTO tbl1 VALUES('$word')"
112 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
113 } "UTF-8 characters contains hi\u1234ho"
115 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
118 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
121 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
122 } "UTF cha con hi\u1234"
124 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
127 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
128 } "TF- har ont i\u1234h"
130 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
133 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
136 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
139 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
142 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
143 } "TF- ter ain i\u1234h"
145 execsql {DELETE FROM tbl1}
146 foreach word {this program is free software} {
147 execsql "INSERT INTO tbl1 VALUES('$word')"
149 execsql {SELECT t1 FROM tbl1}
150 } {this program is free software}
152 } ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
154 # Test the abs() and round() functions.
158 CREATE TABLE t1(a,b,c);
159 INSERT INTO t1 VALUES(1,2,3);
160 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
161 INSERT INTO t1 VALUES(3,-2,-5);
163 catchsql {SELECT abs(a,b) FROM t1}
164 } {1 {wrong number of arguments to function abs()}}
166 catchsql {SELECT abs() FROM t1}
167 } {1 {wrong number of arguments to function abs()}}
169 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
170 } {0 {2 1.2345678901234 2}}
172 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
173 } {0 {3 12345.67890 5}}
175 execsql {SELECT abs(a) FROM t2}
176 } {1 {} 345 {} 67890}
178 execsql {SELECT abs(t1) FROM tbl1}
179 } {this program is free software}
182 catchsql {SELECT round(a,b,c) FROM t1}
183 } {1 {wrong number of arguments to function round()}}
185 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
186 } {0 {-2.00 1.23 2.00}}
188 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
191 catchsql {SELECT round(c) FROM t1 ORDER BY a}
194 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
195 } {0 {3.0 -12345.68 -5.000}}
197 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
198 } {0 {x3.0y x-12345.68y x-5.000y}}
200 catchsql {SELECT round() FROM t1 ORDER BY a}
201 } {1 {wrong number of arguments to function round()}}
203 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
204 } {1.00 nil 345.00 nil 67890.00}
206 execsql {SELECT round(t1,2) FROM tbl1}
207 } {0.00 0.00 0.00 0.00 0.00}
209 # Test the upper() and lower() functions
212 execsql {SELECT upper(t1) FROM tbl1}
213 } {THIS PROGRAM IS FREE SOFTWARE}
215 execsql {SELECT lower(upper(t1)) FROM tbl1}
216 } {this program is free software}
218 execsql {SELECT upper(a), lower(a) FROM t2}
219 } {1 1 {} {} 345 345 {} {} 67890 67890}
221 catchsql {SELECT upper(a,5) FROM t2}
222 } {1 {wrong number of arguments to function upper()}}
224 catchsql {SELECT upper(*) FROM t2}
225 } {1 {wrong number of arguments to function upper()}}
227 # Test the coalesce() and nullif() functions
230 execsql {SELECT coalesce(a,'xyz') FROM t2}
231 } {1 xyz 345 xyz 67890}
233 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
234 } {1 nil 345 nil 67890}
236 execsql {SELECT coalesce(nullif(1,1),'nil')}
239 execsql {SELECT coalesce(nullif(1,2),'nil')}
242 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
246 # Test the last_insert_rowid() function
249 execsql {SELECT last_insert_rowid()}
250 } [db last_insert_rowid]
252 # Tests for aggregate functions and how they handle NULLs.
256 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
258 } {68236 3 22745.33 1 67890 5}
261 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
263 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
266 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
267 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
269 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
272 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
274 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
276 # How do you test the random() function in a meaningful, deterministic way?
280 SELECT random() is not null;
284 # Use the "sqlite_register_test_function" TCL command which is part of
285 # the text fixture in order to verify correct operation of some of
286 # the user-defined SQL function APIs that are not used by the built-in
290 set ::DB [sqlite db test.db]
291 sqlite_register_test_function $::DB testfunc
294 SELECT testfunc(NULL,NULL);
296 } {1 {first argument to test function may not be NULL}}
300 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
308 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
316 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
324 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
326 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
328 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
330 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
332 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
334 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
340 # Test the built-in sqlite_version(*) SQL function.
344 SELECT sqlite_version(*);