blob: cb53360c7ab14d49027c9f6c1d99328dacbab7ac [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhff6e9112000-08-28 16:21:58 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhff6e9112000-08-28 16:21:58 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhff6e9112000-08-28 16:21:58 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing built-in functions.
13#
drh92febd92004-08-20 18:34:20 +000014# $Id: func.test,v 1.29 2004/08/20 18:34:20 drh Exp $
drhff6e9112000-08-28 16:21:58 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Create a table to work with.
20#
drhff6e9112000-08-28 16:21:58 +000021do_test func-0.0 {
drh297ecf12001-04-05 15:57:13 +000022 execsql {CREATE TABLE tbl1(t1 text)}
23 foreach word {this program is free software} {
24 execsql "INSERT INTO tbl1 VALUES('$word')"
25 }
drhff6e9112000-08-28 16:21:58 +000026 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
27} {free is program software this}
drh832508b2002-03-02 17:04:07 +000028do_test func-0.1 {
29 execsql {
30 CREATE TABLE t2(a);
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);
36 SELECT * FROM t2;
37 }
38} {1 {} 345 {} 67890}
drhff6e9112000-08-28 16:21:58 +000039
40# Check out the length() function
41#
42do_test func-1.0 {
43 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
44} {4 2 7 8 4}
45do_test func-1.1 {
46 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
47 lappend r $msg
drh89425d52002-02-28 03:04:48 +000048} {1 {wrong number of arguments to function length()}}
drhff6e9112000-08-28 16:21:58 +000049do_test func-1.2 {
50 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
51 lappend r $msg
drh89425d52002-02-28 03:04:48 +000052} {1 {wrong number of arguments to function length()}}
drhff6e9112000-08-28 16:21:58 +000053do_test func-1.3 {
54 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
55 ORDER BY length(t1)}
56} {2 1 4 2 7 1 8 1}
drh832508b2002-03-02 17:04:07 +000057do_test func-1.4 {
drhbb113512002-05-27 01:04:51 +000058 execsql {SELECT coalesce(length(a),-1) FROM t2}
59} {1 -1 3 -1 5}
drhff6e9112000-08-28 16:21:58 +000060
61# Check out the substr() function
62#
63do_test func-2.0 {
64 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
65} {fr is pr so th}
66do_test func-2.1 {
67 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
68} {r s r o h}
69do_test func-2.2 {
70 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
71} {ee {} ogr ftw is}
72do_test func-2.3 {
73 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
74} {e s m e s}
75do_test func-2.4 {
76 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
77} {e s m e s}
78do_test func-2.5 {
79 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
80} {e i a r i}
81do_test func-2.6 {
82 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
83} {ee is am re is}
84do_test func-2.7 {
85 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
86} {fr {} gr wa th}
87do_test func-2.8 {
88 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
89} {this software free program is}
drh832508b2002-03-02 17:04:07 +000090do_test func-2.9 {
91 execsql {SELECT substr(a,1,1) FROM t2}
92} {1 {} 3 {} 6}
93do_test func-2.10 {
94 execsql {SELECT substr(a,2,2) FROM t2}
95} {{} {} 45 {} 78}
drhff6e9112000-08-28 16:21:58 +000096
drhdf014892004-06-02 00:41:09 +000097# Only do the following tests if TCL has UTF-8 capabilities
drh297ecf12001-04-05 15:57:13 +000098#
drhdf014892004-06-02 00:41:09 +000099if {"\u1234"!="u1234"} {
drh297ecf12001-04-05 15:57:13 +0000100
101# Put some UTF-8 characters in the database
102#
103do_test func-3.0 {
104 execsql {DELETE FROM tbl1}
105 foreach word "contains UTF-8 characters hi\u1234ho" {
106 execsql "INSERT INTO tbl1 VALUES('$word')"
107 }
108 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000109} "UTF-8 characters contains hi\u1234ho"
drh297ecf12001-04-05 15:57:13 +0000110do_test func-3.1 {
111 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000112} {5 10 8 5}
drh297ecf12001-04-05 15:57:13 +0000113do_test func-3.2 {
114 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000115} {UT ch co hi}
drh297ecf12001-04-05 15:57:13 +0000116do_test func-3.3 {
117 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000118} "UTF cha con hi\u1234"
drh297ecf12001-04-05 15:57:13 +0000119do_test func-3.4 {
120 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000121} "TF ha on i\u1234"
drh297ecf12001-04-05 15:57:13 +0000122do_test func-3.5 {
123 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000124} "TF- har ont i\u1234h"
drh297ecf12001-04-05 15:57:13 +0000125do_test func-3.6 {
126 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000127} "F- ar nt \u1234h"
drh297ecf12001-04-05 15:57:13 +0000128do_test func-3.7 {
129 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000130} "-8 ra ta ho"
drh297ecf12001-04-05 15:57:13 +0000131do_test func-3.8 {
132 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000133} "8 s s o"
drh297ecf12001-04-05 15:57:13 +0000134do_test func-3.9 {
135 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000136} "F- er in \u1234h"
drh297ecf12001-04-05 15:57:13 +0000137do_test func-3.10 {
138 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
drha9e99ae2002-08-13 23:02:57 +0000139} "TF- ter ain i\u1234h"
drh832508b2002-03-02 17:04:07 +0000140do_test func-3.99 {
141 execsql {DELETE FROM tbl1}
142 foreach word {this program is free software} {
143 execsql "INSERT INTO tbl1 VALUES('$word')"
144 }
145 execsql {SELECT t1 FROM tbl1}
146} {this program is free software}
drh297ecf12001-04-05 15:57:13 +0000147
drhdf014892004-06-02 00:41:09 +0000148} ;# End \u1234!=u1234
drh297ecf12001-04-05 15:57:13 +0000149
drhbf4133c2001-10-13 02:59:08 +0000150# Test the abs() and round() functions.
151#
152do_test func-4.1 {
153 execsql {
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);
158 }
159 catchsql {SELECT abs(a,b) FROM t1}
drh89425d52002-02-28 03:04:48 +0000160} {1 {wrong number of arguments to function abs()}}
drhbf4133c2001-10-13 02:59:08 +0000161do_test func-4.2 {
162 catchsql {SELECT abs() FROM t1}
drh89425d52002-02-28 03:04:48 +0000163} {1 {wrong number of arguments to function abs()}}
drhbf4133c2001-10-13 02:59:08 +0000164do_test func-4.3 {
165 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
166} {0 {2 1.2345678901234 2}}
167do_test func-4.4 {
168 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
danielk19773d1bfea2004-05-14 11:00:53 +0000169} {0 {3 12345.6789 5}}
drh832508b2002-03-02 17:04:07 +0000170do_test func-4.4.1 {
171 execsql {SELECT abs(a) FROM t2}
172} {1 {} 345 {} 67890}
173do_test func-4.4.2 {
174 execsql {SELECT abs(t1) FROM tbl1}
drh92febd92004-08-20 18:34:20 +0000175} {0.0 0.0 0.0 0.0 0.0}
drhbf4133c2001-10-13 02:59:08 +0000176
177do_test func-4.5 {
178 catchsql {SELECT round(a,b,c) FROM t1}
drh89425d52002-02-28 03:04:48 +0000179} {1 {wrong number of arguments to function round()}}
drhbf4133c2001-10-13 02:59:08 +0000180do_test func-4.6 {
drh8aff1012001-12-22 14:49:24 +0000181 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
182} {0 {-2.00 1.23 2.00}}
drhbf4133c2001-10-13 02:59:08 +0000183do_test func-4.7 {
184 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
185} {0 {2 1 -2}}
186do_test func-4.8 {
187 catchsql {SELECT round(c) FROM t1 ORDER BY a}
188} {0 {3 -12346 -5}}
189do_test func-4.9 {
190 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
191} {0 {3.0 -12345.68 -5.000}}
192do_test func-4.10 {
drh01a34662001-10-20 12:30:10 +0000193 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
194} {0 {x3.0y x-12345.68y x-5.000y}}
195do_test func-4.11 {
drhbf4133c2001-10-13 02:59:08 +0000196 catchsql {SELECT round() FROM t1 ORDER BY a}
drh89425d52002-02-28 03:04:48 +0000197} {1 {wrong number of arguments to function round()}}
drh832508b2002-03-02 17:04:07 +0000198do_test func-4.12 {
drhbb113512002-05-27 01:04:51 +0000199 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
200} {1.00 nil 345.00 nil 67890.00}
drh832508b2002-03-02 17:04:07 +0000201do_test func-4.13 {
202 execsql {SELECT round(t1,2) FROM tbl1}
203} {0.00 0.00 0.00 0.00 0.00}
204
205# Test the upper() and lower() functions
206#
207do_test func-5.1 {
208 execsql {SELECT upper(t1) FROM tbl1}
209} {THIS PROGRAM IS FREE SOFTWARE}
210do_test func-5.2 {
211 execsql {SELECT lower(upper(t1)) FROM tbl1}
212} {this program is free software}
213do_test func-5.3 {
214 execsql {SELECT upper(a), lower(a) FROM t2}
215} {1 1 {} {} 345 345 {} {} 67890 67890}
216do_test func-5.4 {
217 catchsql {SELECT upper(a,5) FROM t2}
218} {1 {wrong number of arguments to function upper()}}
219do_test func-5.5 {
220 catchsql {SELECT upper(*) FROM t2}
221} {1 {wrong number of arguments to function upper()}}
222
drha9f9d1c2002-06-29 02:20:08 +0000223# Test the coalesce() and nullif() functions
drh832508b2002-03-02 17:04:07 +0000224#
225do_test func-6.1 {
226 execsql {SELECT coalesce(a,'xyz') FROM t2}
227} {1 xyz 345 xyz 67890}
228do_test func-6.2 {
229 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
230} {1 nil 345 nil 67890}
drha9f9d1c2002-06-29 02:20:08 +0000231do_test func-6.3 {
232 execsql {SELECT coalesce(nullif(1,1),'nil')}
233} {nil}
234do_test func-6.4 {
235 execsql {SELECT coalesce(nullif(1,2),'nil')}
236} {1}
237do_test func-6.5 {
238 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
239} {1}
240
drh832508b2002-03-02 17:04:07 +0000241
drh6ed41ad2002-04-06 14:10:47 +0000242# Test the last_insert_rowid() function
243#
244do_test func-7.1 {
245 execsql {SELECT last_insert_rowid()}
246} [db last_insert_rowid]
247
drh739105c2002-05-29 23:22:23 +0000248# Tests for aggregate functions and how they handle NULLs.
249#
250do_test func-8.1 {
251 execsql {
252 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
253 }
drh92febd92004-08-20 18:34:20 +0000254} {68236.0 3 22745.33 1 67890 5}
drha9f9d1c2002-06-29 02:20:08 +0000255do_test func-8.2 {
256 execsql {
257 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
258 }
259} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
260do_test func-8.3 {
261 execsql {
262 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
263 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
264 }
drh9eb516c2004-07-18 20:52:32 +0000265} {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
danielk19773aeab9e2004-06-24 00:20:04 +0000266do_test func-8.4 {
267 execsql {
268 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
269 }
270} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
drh739105c2002-05-29 23:22:23 +0000271
drha9f9d1c2002-06-29 02:20:08 +0000272# How do you test the random() function in a meaningful, deterministic way?
273#
274do_test func-9.1 {
275 execsql {
276 SELECT random() is not null;
277 }
278} {1}
drhbf4133c2001-10-13 02:59:08 +0000279
drh6cbe1f12002-07-01 00:31:36 +0000280# Use the "sqlite_register_test_function" TCL command which is part of
281# the text fixture in order to verify correct operation of some of
282# the user-defined SQL function APIs that are not used by the built-in
283# functions.
284#
285db close
drhef4ac8f2004-06-19 00:16:31 +0000286set ::DB [sqlite3 db test.db]
drh6cbe1f12002-07-01 00:31:36 +0000287sqlite_register_test_function $::DB testfunc
288do_test func-10.1 {
289 catchsql {
290 SELECT testfunc(NULL,NULL);
291 }
danielk19776d88bad2004-05-27 14:23:36 +0000292} {1 {first argument should be one of: int int64 string double null value}}
drh6cbe1f12002-07-01 00:31:36 +0000293do_test func-10.2 {
294 execsql {
295 SELECT testfunc(
296 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
297 'int', 1234
298 );
299 }
300} {1234}
301do_test func-10.3 {
302 execsql {
303 SELECT testfunc(
304 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
305 'string', NULL
306 );
307 }
308} {{}}
309do_test func-10.4 {
310 execsql {
311 SELECT testfunc(
312 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
313 'double', 1.234
314 );
315 }
316} {1.234}
317do_test func-10.5 {
318 execsql {
319 SELECT testfunc(
320 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
321 'int', 1234,
322 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
323 'string', NULL,
324 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
325 'double', 1.234,
326 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
327 'int', 1234,
328 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
329 'string', NULL,
330 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
331 'double', 1.234
332 );
333 }
334} {1.234}
335
drh647cb0e2002-11-04 19:32:25 +0000336# Test the built-in sqlite_version(*) SQL function.
337#
338do_test func-11.1 {
339 execsql {
340 SELECT sqlite_version(*);
341 }
drhef4ac8f2004-06-19 00:16:31 +0000342} [sqlite3 -version]
drh647cb0e2002-11-04 19:32:25 +0000343
drhef4ac8f2004-06-19 00:16:31 +0000344# Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
danielk19773f6b0872004-06-17 05:36:44 +0000345# etc. are called. These tests use two special user-defined functions
346# (implemented in func.c) only available in test builds.
347#
348# Function test_destructor() takes one argument and returns a copy of the
349# text form of that argument. A destructor is associated with the return
350# value. Function test_destructor_count() returns the number of outstanding
351# destructor calls for values returned by test_destructor().
352#
danielk1977d8123362004-06-12 09:25:12 +0000353do_test func-12.1 {
354 execsql {
355 SELECT test_destructor('hello world'), test_destructor_count();
356 }
357} {{hello world} 1}
358do_test func-12.2 {
359 execsql {
360 SELECT test_destructor_count();
361 }
362} {0}
363do_test func-12.3 {
364 execsql {
365 SELECT test_destructor('hello')||' world', test_destructor_count();
366 }
367} {{hello world} 0}
368do_test func-12.4 {
369 execsql {
370 SELECT test_destructor_count();
371 }
372} {0}
373do_test func-12.5 {
374 execsql {
375 CREATE TABLE t4(x);
376 INSERT INTO t4 VALUES(test_destructor('hello'));
377 INSERT INTO t4 VALUES(test_destructor('world'));
378 SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
379 }
380} {hello world}
381do_test func-12.6 {
382 execsql {
383 SELECT test_destructor_count();
384 }
385} {0}
danielk19773f6b0872004-06-17 05:36:44 +0000386do_test func-12.7 {
387 execsql {
388 DROP TABLE t4;
389 }
390} {}
391
392# Test that the auxdata API for scalar functions works. This test uses
393# a special user-defined function only available in test builds,
394# test_auxdata(). Function test_auxdata() takes any number of arguments.
395do_test func-13.1 {
396 execsql {
397 SELECT test_auxdata('hello world');
398 }
399} {0}
danielk1977ece80f12004-06-23 01:05:26 +0000400
danielk19773f6b0872004-06-17 05:36:44 +0000401do_test func-13.2 {
402 execsql {
403 CREATE TABLE t4(a, b);
404 INSERT INTO t4 VALUES('abc', 'def');
405 INSERT INTO t4 VALUES('ghi', 'jkl');
406 }
407} {}
408do_test func-13.3 {
409 execsql {
410 SELECT test_auxdata('hello world') FROM t4;
411 }
412} {0 1}
413do_test func-13.4 {
414 execsql {
415 SELECT test_auxdata('hello world', 123) FROM t4;
416 }
417} {{0 0} {1 1}}
418do_test func-13.5 {
419 execsql {
420 SELECT test_auxdata('hello world', a) FROM t4;
421 }
422} {{0 0} {1 0}}
423do_test func-13.6 {
424 execsql {
425 SELECT test_auxdata('hello'||'world', a) FROM t4;
426 }
427} {{0 0} {1 0}}
428
429# Test that auxilary data is preserved between calls for SQL variables.
430do_test func-13.7 {
431 db close
drhef4ac8f2004-06-19 00:16:31 +0000432 set DB [sqlite3 db test.db]
danielk19773f6b0872004-06-17 05:36:44 +0000433 set sql "SELECT test_auxdata( ? , a ) FROM t4;"
434 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
435 sqlite3_bind_text $STMT 1 hello -1
436 set res [list]
437 while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
438 lappend res [sqlite3_column_text $STMT 0]
439 }
440 lappend res [sqlite3_finalize $STMT]
441} {{0 0} {1 0} SQLITE_OK}
danielk1977d8123362004-06-12 09:25:12 +0000442
danielk1977312d6b32004-06-29 13:18:23 +0000443# Make sure that a function with a very long name is rejected
444do_test func-14.1 {
445 catch {
446 db function [string repeat X 254] {return "hello"}
447 }
448} {0}
449do_test func-14.2 {
450 catch {
451 db function [string repeat X 256] {return "hello"}
452 }
453} {1}
454
drhff6e9112000-08-28 16:21:58 +0000455finish_test