3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing built-in functions.
14 # $Id: func.test,v 1.34 2005/03/29 03:11:00 danielk1977 Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Create a table to work with.
22 execsql {CREATE TABLE tbl1(t1 text)}
23 foreach word {this program is free software} {
24 execsql "INSERT INTO tbl1 VALUES('$word')"
26 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
27 } {free is program software this}
31 INSERT INTO t2 VALUES(1);
32 INSERT INTO t2 VALUES(NULL);
33 INSERT INTO t2 VALUES(345);
34 INSERT INTO t2 VALUES(NULL);
35 INSERT INTO t2 VALUES(67890);
40 # Check out the length() function
43 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
46 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
48 } {1 {wrong number of arguments to function length()}}
50 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
52 } {1 {wrong number of arguments to function length()}}
54 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
58 execsql {SELECT coalesce(length(a),-1) FROM t2}
61 # Check out the substr() function
64 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
67 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
70 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
73 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
76 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
79 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
82 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
85 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
88 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
89 } {this software free program is}
91 execsql {SELECT substr(a,1,1) FROM t2}
94 execsql {SELECT substr(a,2,2) FROM t2}
97 # Only do the following tests if TCL has UTF-8 capabilities
99 if {"\u1234"!="u1234"} {
101 # Put some UTF-8 characters in the database
104 execsql {DELETE FROM tbl1}
105 foreach word "contains UTF-8 characters hi\u1234ho" {
106 execsql "INSERT INTO tbl1 VALUES('$word')"
108 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
109 } "UTF-8 characters contains hi\u1234ho"
111 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
114 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
117 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
118 } "UTF cha con hi\u1234"
120 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
123 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
124 } "TF- har ont i\u1234h"
126 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
129 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
132 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
135 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
138 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
139 } "TF- ter ain i\u1234h"
141 execsql {DELETE FROM tbl1}
142 foreach word {this program is free software} {
143 execsql "INSERT INTO tbl1 VALUES('$word')"
145 execsql {SELECT t1 FROM tbl1}
146 } {this program is free software}
148 } ;# End \u1234!=u1234
150 # Test the abs() and round() functions.
154 CREATE TABLE t1(a,b,c);
155 INSERT INTO t1 VALUES(1,2,3);
156 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
157 INSERT INTO t1 VALUES(3,-2,-5);
159 catchsql {SELECT abs(a,b) FROM t1}
160 } {1 {wrong number of arguments to function abs()}}
162 catchsql {SELECT abs() FROM t1}
163 } {1 {wrong number of arguments to function abs()}}
165 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
166 } {0 {2 1.2345678901234 2}}
168 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
169 } {0 {3 12345.6789 5}}
171 execsql {SELECT abs(a) FROM t2}
172 } {1 {} 345 {} 67890}
174 execsql {SELECT abs(t1) FROM tbl1}
175 } {0.0 0.0 0.0 0.0 0.0}
178 catchsql {SELECT round(a,b,c) FROM t1}
179 } {1 {wrong number of arguments to function round()}}
181 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
182 } {0 {-2.00 1.23 2.00}}
184 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
187 catchsql {SELECT round(c) FROM t1 ORDER BY a}
190 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
191 } {0 {3.0 -12345.68 -5.000}}
193 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
194 } {0 {x3.0y x-12345.68y x-5.000y}}
196 catchsql {SELECT round() FROM t1 ORDER BY a}
197 } {1 {wrong number of arguments to function round()}}
199 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
200 } {1.00 nil 345.00 nil 67890.00}
202 execsql {SELECT round(t1,2) FROM tbl1}
203 } {0.00 0.00 0.00 0.00 0.00}
205 # Test the upper() and lower() functions
208 execsql {SELECT upper(t1) FROM tbl1}
209 } {THIS PROGRAM IS FREE SOFTWARE}
211 execsql {SELECT lower(upper(t1)) FROM tbl1}
212 } {this program is free software}
214 execsql {SELECT upper(a), lower(a) FROM t2}
215 } {1 1 {} {} 345 345 {} {} 67890 67890}
217 catchsql {SELECT upper(a,5) FROM t2}
218 } {1 {wrong number of arguments to function upper()}}
220 catchsql {SELECT upper(*) FROM t2}
221 } {1 {wrong number of arguments to function upper()}}
223 # Test the coalesce() and nullif() functions
226 execsql {SELECT coalesce(a,'xyz') FROM t2}
227 } {1 xyz 345 xyz 67890}
229 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
230 } {1 nil 345 nil 67890}
232 execsql {SELECT coalesce(nullif(1,1),'nil')}
235 execsql {SELECT coalesce(nullif(1,2),'nil')}
238 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
242 # Test the last_insert_rowid() function
245 execsql {SELECT last_insert_rowid()}
246 } [db last_insert_rowid]
248 # Tests for aggregate functions and how they handle NULLs.
252 execsql {EXPLAIN SELECT sum(a) FROM t2;}
255 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
257 } {68236.0 3 22745.33 1 67890 5}
260 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
262 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
267 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
268 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
270 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
274 CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
275 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
277 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
281 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
283 } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
285 # How do you test the random() function in a meaningful, deterministic way?
289 SELECT random() is not null;
293 # Use the "sqlite_register_test_function" TCL command which is part of
294 # the text fixture in order to verify correct operation of some of
295 # the user-defined SQL function APIs that are not used by the built-in
299 set ::DB [sqlite3 db test.db]
300 sqlite_register_test_function $::DB testfunc
303 SELECT testfunc(NULL,NULL);
305 } {1 {first argument should be one of: int int64 string double null value}}
309 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
317 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
325 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
333 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
335 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
337 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
339 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
341 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
343 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
349 # Test the built-in sqlite_version(*) SQL function.
353 SELECT sqlite_version(*);
357 # Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
358 # etc. are called. These tests use two special user-defined functions
359 # (implemented in func.c) only available in test builds.
361 # Function test_destructor() takes one argument and returns a copy of the
362 # text form of that argument. A destructor is associated with the return
363 # value. Function test_destructor_count() returns the number of outstanding
364 # destructor calls for values returned by test_destructor().
368 SELECT test_destructor('hello world'), test_destructor_count();
373 SELECT test_destructor_count();
378 SELECT test_destructor('hello')||' world', test_destructor_count();
383 SELECT test_destructor_count();
389 INSERT INTO t4 VALUES(test_destructor('hello'));
390 INSERT INTO t4 VALUES(test_destructor('world'));
391 SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
396 SELECT test_destructor_count();
405 # Test that the auxdata API for scalar functions works. This test uses
406 # a special user-defined function only available in test builds,
407 # test_auxdata(). Function test_auxdata() takes any number of arguments.
410 SELECT test_auxdata('hello world');
416 CREATE TABLE t4(a, b);
417 INSERT INTO t4 VALUES('abc', 'def');
418 INSERT INTO t4 VALUES('ghi', 'jkl');
423 SELECT test_auxdata('hello world') FROM t4;
428 SELECT test_auxdata('hello world', 123) FROM t4;
433 SELECT test_auxdata('hello world', a) FROM t4;
438 SELECT test_auxdata('hello'||'world', a) FROM t4;
442 # Test that auxilary data is preserved between calls for SQL variables.
445 set DB [sqlite3 db test.db]
446 set sql "SELECT test_auxdata( ? , a ) FROM t4;"
447 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
448 sqlite3_bind_text $STMT 1 hello -1
450 while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
451 lappend res [sqlite3_column_text $STMT 0]
453 lappend res [sqlite3_finalize $STMT]
454 } {{0 0} {1 0} SQLITE_OK}
456 # Make sure that a function with a very long name is rejected
459 db function [string repeat X 254] {return "hello"}
464 db function [string repeat X 256] {return "hello"}
470 select test_error(NULL);
472 } {1 {user function error}}
474 # Test the quote function for BLOB and NULL values.
477 CREATE TABLE tbl2(a, b);
479 set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL]
480 sqlite3_bind_blob $::STMT 1 abc 3
482 sqlite3_finalize $::STMT
484 SELECT quote(a), quote(b) FROM tbl2;