blob: 2f3fc314d8f95892ec2ba49833678749e8b228b4 [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#
drh309b3382007-03-17 17:52:42 +000014# $Id: func.test,v 1.59 2007/03/17 17:52:42 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}
drhd589a922006-03-02 03:02:48 +0000182} {0 {-2.0 1.23 2.0}}
drhbf4133c2001-10-13 02:59:08 +0000183do_test func-4.7 {
184 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
drhd589a922006-03-02 03:02:48 +0000185} {0 {2.0 1.0 -2.0}}
drhbf4133c2001-10-13 02:59:08 +0000186do_test func-4.8 {
187 catchsql {SELECT round(c) FROM t1 ORDER BY a}
drhd589a922006-03-02 03:02:48 +0000188} {0 {3.0 -12346.0 -5.0}}
drhbf4133c2001-10-13 02:59:08 +0000189do_test func-4.9 {
190 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
drhd589a922006-03-02 03:02:48 +0000191} {0 {3.0 -12345.68 -5.0}}
drhbf4133c2001-10-13 02:59:08 +0000192do_test func-4.10 {
drh01a34662001-10-20 12:30:10 +0000193 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
drhd589a922006-03-02 03:02:48 +0000194} {0 {x3.0y x-12345.68y x-5.0y}}
drh01a34662001-10-20 12:30:10 +0000195do_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}
drhd589a922006-03-02 03:02:48 +0000200} {1.0 nil 345.0 nil 67890.0}
drh832508b2002-03-02 17:04:07 +0000201do_test func-4.13 {
202 execsql {SELECT round(t1,2) FROM tbl1}
drhd589a922006-03-02 03:02:48 +0000203} {0.0 0.0 0.0 0.0 0.0}
204do_test func-4.14 {
205 execsql {SELECT typeof(round(5.1,1));}
206} {real}
207do_test func-4.15 {
208 execsql {SELECT typeof(round(5.1));}
209} {real}
210
drh832508b2002-03-02 17:04:07 +0000211
212# Test the upper() and lower() functions
213#
214do_test func-5.1 {
215 execsql {SELECT upper(t1) FROM tbl1}
216} {THIS PROGRAM IS FREE SOFTWARE}
217do_test func-5.2 {
218 execsql {SELECT lower(upper(t1)) FROM tbl1}
219} {this program is free software}
220do_test func-5.3 {
221 execsql {SELECT upper(a), lower(a) FROM t2}
222} {1 1 {} {} 345 345 {} {} 67890 67890}
223do_test func-5.4 {
224 catchsql {SELECT upper(a,5) FROM t2}
225} {1 {wrong number of arguments to function upper()}}
226do_test func-5.5 {
227 catchsql {SELECT upper(*) FROM t2}
228} {1 {wrong number of arguments to function upper()}}
229
drha9f9d1c2002-06-29 02:20:08 +0000230# Test the coalesce() and nullif() functions
drh832508b2002-03-02 17:04:07 +0000231#
232do_test func-6.1 {
233 execsql {SELECT coalesce(a,'xyz') FROM t2}
234} {1 xyz 345 xyz 67890}
235do_test func-6.2 {
236 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
237} {1 nil 345 nil 67890}
drha9f9d1c2002-06-29 02:20:08 +0000238do_test func-6.3 {
239 execsql {SELECT coalesce(nullif(1,1),'nil')}
240} {nil}
241do_test func-6.4 {
242 execsql {SELECT coalesce(nullif(1,2),'nil')}
243} {1}
244do_test func-6.5 {
245 execsql {SELECT coalesce(nullif(1,NULL),'nil')}
246} {1}
247
drh832508b2002-03-02 17:04:07 +0000248
drh6ed41ad2002-04-06 14:10:47 +0000249# Test the last_insert_rowid() function
250#
251do_test func-7.1 {
252 execsql {SELECT last_insert_rowid()}
253} [db last_insert_rowid]
254
drh739105c2002-05-29 23:22:23 +0000255# Tests for aggregate functions and how they handle NULLs.
256#
257do_test func-8.1 {
danielk19774489f9b2005-01-20 02:17:01 +0000258 ifcapable explain {
259 execsql {EXPLAIN SELECT sum(a) FROM t2;}
260 }
drh739105c2002-05-29 23:22:23 +0000261 execsql {
262 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
263 }
drh3d1d95e2005-09-08 10:37:01 +0000264} {68236 3 22745.33 1 67890 5}
drha9f9d1c2002-06-29 02:20:08 +0000265do_test func-8.2 {
266 execsql {
267 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
268 }
269} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
danielk197753c0f742005-03-29 03:10:59 +0000270
271ifcapable tempdb {
272 do_test func-8.3 {
273 execsql {
274 CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
275 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
276 }
277 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
278} else {
279 do_test func-8.3 {
280 execsql {
281 CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
282 SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
283 }
284 } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
285}
danielk19773aeab9e2004-06-24 00:20:04 +0000286do_test func-8.4 {
287 execsql {
288 SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
289 }
290} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
drh739105c2002-05-29 23:22:23 +0000291
drha9f9d1c2002-06-29 02:20:08 +0000292# How do you test the random() function in a meaningful, deterministic way?
293#
294do_test func-9.1 {
295 execsql {
296 SELECT random() is not null;
297 }
298} {1}
drh63cf66f2007-01-29 15:50:05 +0000299do_test func-9.2 {
300 execsql {
301 SELECT typeof(random());
302 }
303} {integer}
304do_test func-9.3 {
305 execsql {
drh137c7282007-01-29 17:58:28 +0000306 SELECT randomblob(32) is not null;
drh63cf66f2007-01-29 15:50:05 +0000307 }
308} {1}
309do_test func-9.4 {
310 execsql {
drh137c7282007-01-29 17:58:28 +0000311 SELECT typeof(randomblob(32));
drh63cf66f2007-01-29 15:50:05 +0000312 }
drh137c7282007-01-29 17:58:28 +0000313} {blob}
drh63cf66f2007-01-29 15:50:05 +0000314do_test func-9.5 {
315 execsql {
drh137c7282007-01-29 17:58:28 +0000316 SELECT length(randomblob(32)), length(randomblob(-5)),
317 length(randomblob(2000))
drh63cf66f2007-01-29 15:50:05 +0000318 }
drh137c7282007-01-29 17:58:28 +0000319} {32 1 2000}
drh63cf66f2007-01-29 15:50:05 +0000320
drh137c7282007-01-29 17:58:28 +0000321# The "hex()" function was added in order to be able to render blobs
322# generated by randomblob(). So this seems like a good place to test
323# hex().
324#
325do_test func-9.10 {
326 execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
327} {00112233445566778899AABBCCDDEEFF}
drhbf4133c2001-10-13 02:59:08 +0000328
drh6cbe1f12002-07-01 00:31:36 +0000329# Use the "sqlite_register_test_function" TCL command which is part of
330# the text fixture in order to verify correct operation of some of
331# the user-defined SQL function APIs that are not used by the built-in
332# functions.
333#
drhdddca282006-01-03 00:33:50 +0000334set ::DB [sqlite3_connection_pointer db]
drh6cbe1f12002-07-01 00:31:36 +0000335sqlite_register_test_function $::DB testfunc
336do_test func-10.1 {
337 catchsql {
338 SELECT testfunc(NULL,NULL);
339 }
danielk19776d88bad2004-05-27 14:23:36 +0000340} {1 {first argument should be one of: int int64 string double null value}}
drh6cbe1f12002-07-01 00:31:36 +0000341do_test func-10.2 {
342 execsql {
343 SELECT testfunc(
344 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
345 'int', 1234
346 );
347 }
348} {1234}
349do_test func-10.3 {
350 execsql {
351 SELECT testfunc(
352 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
353 'string', NULL
354 );
355 }
356} {{}}
357do_test func-10.4 {
358 execsql {
359 SELECT testfunc(
360 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
361 'double', 1.234
362 );
363 }
364} {1.234}
365do_test func-10.5 {
366 execsql {
367 SELECT testfunc(
368 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
369 'int', 1234,
370 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
371 'string', NULL,
372 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
373 'double', 1.234,
374 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
375 'int', 1234,
376 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
377 'string', NULL,
378 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
379 'double', 1.234
380 );
381 }
382} {1.234}
383
drh647cb0e2002-11-04 19:32:25 +0000384# Test the built-in sqlite_version(*) SQL function.
385#
386do_test func-11.1 {
387 execsql {
388 SELECT sqlite_version(*);
389 }
drhef4ac8f2004-06-19 00:16:31 +0000390} [sqlite3 -version]
drh647cb0e2002-11-04 19:32:25 +0000391
drhef4ac8f2004-06-19 00:16:31 +0000392# Test that destructors passed to sqlite3 by calls to sqlite3_result_text()
danielk19773f6b0872004-06-17 05:36:44 +0000393# etc. are called. These tests use two special user-defined functions
394# (implemented in func.c) only available in test builds.
395#
396# Function test_destructor() takes one argument and returns a copy of the
397# text form of that argument. A destructor is associated with the return
398# value. Function test_destructor_count() returns the number of outstanding
399# destructor calls for values returned by test_destructor().
400#
danielk1977d8123362004-06-12 09:25:12 +0000401do_test func-12.1 {
402 execsql {
403 SELECT test_destructor('hello world'), test_destructor_count();
404 }
405} {{hello world} 1}
406do_test func-12.2 {
407 execsql {
408 SELECT test_destructor_count();
409 }
410} {0}
411do_test func-12.3 {
412 execsql {
413 SELECT test_destructor('hello')||' world', test_destructor_count();
414 }
415} {{hello world} 0}
416do_test func-12.4 {
417 execsql {
418 SELECT test_destructor_count();
419 }
420} {0}
421do_test func-12.5 {
422 execsql {
423 CREATE TABLE t4(x);
424 INSERT INTO t4 VALUES(test_destructor('hello'));
425 INSERT INTO t4 VALUES(test_destructor('world'));
426 SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4;
427 }
428} {hello world}
429do_test func-12.6 {
430 execsql {
431 SELECT test_destructor_count();
432 }
433} {0}
danielk19773f6b0872004-06-17 05:36:44 +0000434do_test func-12.7 {
435 execsql {
436 DROP TABLE t4;
437 }
438} {}
439
440# Test that the auxdata API for scalar functions works. This test uses
441# a special user-defined function only available in test builds,
442# test_auxdata(). Function test_auxdata() takes any number of arguments.
443do_test func-13.1 {
444 execsql {
445 SELECT test_auxdata('hello world');
446 }
447} {0}
danielk1977ece80f12004-06-23 01:05:26 +0000448
danielk19773f6b0872004-06-17 05:36:44 +0000449do_test func-13.2 {
450 execsql {
451 CREATE TABLE t4(a, b);
452 INSERT INTO t4 VALUES('abc', 'def');
453 INSERT INTO t4 VALUES('ghi', 'jkl');
454 }
455} {}
456do_test func-13.3 {
457 execsql {
458 SELECT test_auxdata('hello world') FROM t4;
459 }
460} {0 1}
461do_test func-13.4 {
462 execsql {
463 SELECT test_auxdata('hello world', 123) FROM t4;
464 }
465} {{0 0} {1 1}}
466do_test func-13.5 {
467 execsql {
468 SELECT test_auxdata('hello world', a) FROM t4;
469 }
470} {{0 0} {1 0}}
471do_test func-13.6 {
472 execsql {
473 SELECT test_auxdata('hello'||'world', a) FROM t4;
474 }
475} {{0 0} {1 0}}
476
477# Test that auxilary data is preserved between calls for SQL variables.
478do_test func-13.7 {
drhdddca282006-01-03 00:33:50 +0000479 set DB [sqlite3_connection_pointer db]
danielk19773f6b0872004-06-17 05:36:44 +0000480 set sql "SELECT test_auxdata( ? , a ) FROM t4;"
481 set STMT [sqlite3_prepare $DB $sql -1 TAIL]
482 sqlite3_bind_text $STMT 1 hello -1
483 set res [list]
484 while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
485 lappend res [sqlite3_column_text $STMT 0]
486 }
487 lappend res [sqlite3_finalize $STMT]
488} {{0 0} {1 0} SQLITE_OK}
danielk1977d8123362004-06-12 09:25:12 +0000489
danielk1977312d6b32004-06-29 13:18:23 +0000490# Make sure that a function with a very long name is rejected
491do_test func-14.1 {
492 catch {
493 db function [string repeat X 254] {return "hello"}
494 }
495} {0}
496do_test func-14.2 {
497 catch {
498 db function [string repeat X 256] {return "hello"}
499 }
500} {1}
501
danielk197701427a62005-01-11 13:02:33 +0000502do_test func-15.1 {
503 catchsql {
504 select test_error(NULL);
505 }
drh90669c12006-01-20 15:45:36 +0000506} {1 {}}
danielk197701427a62005-01-11 13:02:33 +0000507
danielk1977576ec6b2005-01-21 11:55:25 +0000508# Test the quote function for BLOB and NULL values.
509do_test func-16.1 {
510 execsql {
511 CREATE TABLE tbl2(a, b);
512 }
513 set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL]
514 sqlite3_bind_blob $::STMT 1 abc 3
515 sqlite3_step $::STMT
516 sqlite3_finalize $::STMT
517 execsql {
518 SELECT quote(a), quote(b) FROM tbl2;
519 }
520} {X'616263' NULL}
521
drh2501eb12005-08-12 23:20:53 +0000522# Correctly handle function error messages that include %. Ticket #1354
523#
524do_test func-17.1 {
525 proc testfunc1 args {error "Error %d with %s percents %p"}
526 db function testfunc1 ::testfunc1
527 catchsql {
528 SELECT testfunc1(1,2,3);
529 }
530} {1 {Error %d with %s percents %p}}
531
drh3d1d95e2005-09-08 10:37:01 +0000532# The SUM function should return integer results when all inputs are integer.
533#
534do_test func-18.1 {
535 execsql {
536 CREATE TABLE t5(x);
537 INSERT INTO t5 VALUES(1);
538 INSERT INTO t5 VALUES(-99);
539 INSERT INTO t5 VALUES(10000);
540 SELECT sum(x) FROM t5;
541 }
542} {9902}
543do_test func-18.2 {
544 execsql {
545 INSERT INTO t5 VALUES(0.0);
546 SELECT sum(x) FROM t5;
547 }
drh8a512562005-11-14 22:29:05 +0000548} {9902.0}
danielk1977576ec6b2005-01-21 11:55:25 +0000549
drhc2bd9132005-09-08 20:37:43 +0000550# The sum of nothing is NULL. But the sum of all NULLs is NULL.
drh3f219f42005-09-08 19:45:57 +0000551#
drh76c730c2006-02-09 17:47:42 +0000552# The TOTAL of nothing is 0.0.
553#
drh3f219f42005-09-08 19:45:57 +0000554do_test func-18.3 {
555 execsql {
556 DELETE FROM t5;
drh76c730c2006-02-09 17:47:42 +0000557 SELECT sum(x), total(x) FROM t5;
drh3f219f42005-09-08 19:45:57 +0000558 }
drh76c730c2006-02-09 17:47:42 +0000559} {{} 0.0}
drh3f219f42005-09-08 19:45:57 +0000560do_test func-18.4 {
561 execsql {
562 INSERT INTO t5 VALUES(NULL);
drh76c730c2006-02-09 17:47:42 +0000563 SELECT sum(x), total(x) FROM t5
drh3f219f42005-09-08 19:45:57 +0000564 }
drh76c730c2006-02-09 17:47:42 +0000565} {{} 0.0}
drh3f219f42005-09-08 19:45:57 +0000566do_test func-18.5 {
567 execsql {
568 INSERT INTO t5 VALUES(NULL);
drh76c730c2006-02-09 17:47:42 +0000569 SELECT sum(x), total(x) FROM t5
drh3f219f42005-09-08 19:45:57 +0000570 }
drh76c730c2006-02-09 17:47:42 +0000571} {{} 0.0}
drh3f219f42005-09-08 19:45:57 +0000572do_test func-18.6 {
573 execsql {
574 INSERT INTO t5 VALUES(123);
drh76c730c2006-02-09 17:47:42 +0000575 SELECT sum(x), total(x) FROM t5
drh3f219f42005-09-08 19:45:57 +0000576 }
drh76c730c2006-02-09 17:47:42 +0000577} {123 123.0}
drh5708d2d2005-06-22 10:53:59 +0000578
drh8c08e862006-02-11 17:34:00 +0000579# Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes
580# an error. The non-standard TOTAL() function continues to give a helpful
581# result.
drhfc6ad392006-02-09 13:38:19 +0000582#
583do_test func-18.10 {
584 execsql {
585 CREATE TABLE t6(x INTEGER);
586 INSERT INTO t6 VALUES(1);
587 INSERT INTO t6 VALUES(1<<62);
588 SELECT sum(x) - ((1<<62)+1) from t6;
589 }
590} 0
drh76c730c2006-02-09 17:47:42 +0000591do_test func-18.11 {
592 execsql {
593 SELECT typeof(sum(x)) FROM t6
594 }
595} integer
596do_test func-18.12 {
drh8c08e862006-02-11 17:34:00 +0000597 catchsql {
drh76c730c2006-02-09 17:47:42 +0000598 INSERT INTO t6 VALUES(1<<62);
599 SELECT sum(x) - ((1<<62)*2.0+1) from t6;
600 }
drh8c08e862006-02-11 17:34:00 +0000601} {1 {integer overflow}}
drh76c730c2006-02-09 17:47:42 +0000602do_test func-18.13 {
603 execsql {
604 SELECT total(x) - ((1<<62)*2.0+1) FROM t6
605 }
606} 0.0
drh8c08e862006-02-11 17:34:00 +0000607do_test func-18.14 {
608 execsql {
609 SELECT sum(-9223372036854775805);
610 }
611} -9223372036854775805
danielk19774b2688a2006-06-20 11:01:07 +0000612
613ifcapable compound&&subquery {
614
drh8c08e862006-02-11 17:34:00 +0000615do_test func-18.15 {
616 catchsql {
617 SELECT sum(x) FROM
618 (SELECT 9223372036854775807 AS x UNION ALL
619 SELECT 10 AS x);
620 }
621} {1 {integer overflow}}
622do_test func-18.16 {
623 catchsql {
624 SELECT sum(x) FROM
625 (SELECT 9223372036854775807 AS x UNION ALL
626 SELECT -10 AS x);
627 }
628} {0 9223372036854775797}
629do_test func-18.17 {
630 catchsql {
631 SELECT sum(x) FROM
632 (SELECT -9223372036854775807 AS x UNION ALL
633 SELECT 10 AS x);
634 }
635} {0 -9223372036854775797}
636do_test func-18.18 {
637 catchsql {
638 SELECT sum(x) FROM
639 (SELECT -9223372036854775807 AS x UNION ALL
640 SELECT -10 AS x);
641 }
642} {1 {integer overflow}}
643do_test func-18.19 {
644 catchsql {
645 SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x);
646 }
647} {0 -1}
648do_test func-18.20 {
649 catchsql {
650 SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x);
651 }
652} {0 1}
653do_test func-18.21 {
654 catchsql {
655 SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x);
656 }
657} {0 -1}
658do_test func-18.22 {
659 catchsql {
660 SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x);
661 }
662} {0 1}
drh76c730c2006-02-09 17:47:42 +0000663
danielk19774b2688a2006-06-20 11:01:07 +0000664} ;# ifcapable compound&&subquery
665
drh52fc8492006-02-23 21:43:55 +0000666# Integer overflow on abs()
667#
668do_test func-18.31 {
669 catchsql {
670 SELECT abs(-9223372036854775807);
671 }
672} {0 9223372036854775807}
673do_test func-18.32 {
674 catchsql {
675 SELECT abs(-9223372036854775807-1);
676 }
677} {1 {integer overflow}}
678
drh7f375902006-06-13 17:38:59 +0000679# The MATCH function exists but is only a stub and always throws an error.
680#
681do_test func-19.1 {
682 execsql {
683 SELECT match(a,b) FROM t1 WHERE 0;
684 }
685} {}
686do_test func-19.2 {
687 catchsql {
688 SELECT 'abc' MATCH 'xyz';
689 }
drhb7481e72006-09-16 21:45:14 +0000690} {1 {unable to use function MATCH in the requested context}}
drh7f375902006-06-13 17:38:59 +0000691do_test func-19.3 {
692 catchsql {
693 SELECT 'abc' NOT MATCH 'xyz';
694 }
drhb7481e72006-09-16 21:45:14 +0000695} {1 {unable to use function MATCH in the requested context}}
drh7f375902006-06-13 17:38:59 +0000696do_test func-19.4 {
697 catchsql {
698 SELECT match(1,2,3);
699 }
700} {1 {wrong number of arguments to function match()}}
drh76c730c2006-02-09 17:47:42 +0000701
drhbdf67e02006-08-19 11:34:01 +0000702# Soundex tests.
703#
704if {![catch {db eval {SELECT soundex('hello')}}]} {
705 set i 0
706 foreach {name sdx} {
707 euler E460
708 EULER E460
709 Euler E460
710 ellery E460
711 gauss G200
712 ghosh G200
713 hilbert H416
714 Heilbronn H416
715 knuth K530
716 kant K530
717 Lloyd L300
718 LADD L300
719 Lukasiewicz L222
720 Lissajous L222
721 A A000
722 12345 ?000
723 } {
724 incr i
725 do_test func-20.$i {
726 execsql {SELECT soundex($name)}
727 } $sdx
728 }
729}
730
drh26b6d902007-03-17 13:27:54 +0000731# Tests of the REPLACE function.
732#
733do_test func-21.1 {
734 catchsql {
735 SELECT replace(1,2);
736 }
737} {1 {wrong number of arguments to function replace()}}
738do_test func-21.2 {
739 catchsql {
740 SELECT replace(1,2,3,4);
741 }
742} {1 {wrong number of arguments to function replace()}}
743do_test func-21.3 {
744 execsql {
745 SELECT typeof(replace("This is the main test string", NULL, "ALT"));
746 }
747} {null}
748do_test func-21.4 {
749 execsql {
750 SELECT typeof(replace(NULL, "main", "ALT"));
751 }
752} {null}
753do_test func-21.5 {
754 execsql {
755 SELECT typeof(replace("This is the main test string", "main", NULL));
756 }
757} {null}
758do_test func-21.6 {
759 execsql {
760 SELECT replace("This is the main test string", "main", "ALT");
761 }
762} {{This is the ALT test string}}
763do_test func-21.7 {
764 execsql {
765 SELECT replace("This is the main test string", "main", "larger-main");
766 }
767} {{This is the larger-main test string}}
768do_test func-21.8 {
769 execsql {
770 SELECT replace("aaaaaaa", "a", "0123456789");
771 }
772} {0123456789012345678901234567890123456789012345678901234567890123456789}
773
drh309b3382007-03-17 17:52:42 +0000774# Tests for the TRIM, LTRIM and RTRIM functions.
775#
776do_test func-22.1 {
777 catchsql {SELECT trim(1,2,3)}
778} {1 {wrong number of arguments to function trim()}}
779do_test func-22.2 {
780 catchsql {SELECT ltrim(1,2,3)}
781} {1 {wrong number of arguments to function ltrim()}}
782do_test func-22.3 {
783 catchsql {SELECT rtrim(1,2,3)}
784} {1 {wrong number of arguments to function rtrim()}}
785do_test func-22.4 {
786 execsql {SELECT trim(' hi ');}
787} {hi}
788do_test func-22.5 {
789 execsql {SELECT ltrim(' hi ');}
790} {{hi }}
791do_test func-22.6 {
792 execsql {SELECT rtrim(' hi ');}
793} {{ hi}}
794do_test func-22.7 {
795 execsql {SELECT trim(' hi ','xyz');}
796} {{ hi }}
797do_test func-22.8 {
798 execsql {SELECT ltrim(' hi ','xyz');}
799} {{ hi }}
800do_test func-22.9 {
801 execsql {SELECT rtrim(' hi ','xyz');}
802} {{ hi }}
803do_test func-22.10 {
804 execsql {SELECT trim('xyxzy hi zzzy','xyz');}
805} {{ hi }}
806do_test func-22.11 {
807 execsql {SELECT ltrim('xyxzy hi zzzy','xyz');}
808} {{ hi zzzy}}
809do_test func-22.12 {
810 execsql {SELECT rtrim('xyxzy hi zzzy','xyz');}
811} {{xyxzy hi }}
812do_test func-22.13 {
813 execsql {SELECT trim(' hi ','');}
814} {{ hi }}
815do_test func-22.20 {
816 execsql {SELECT typeof(trim(NULL));}
817} {null}
818do_test func-22.21 {
819 execsql {SELECT typeof(trim(NULL,'xyz'));}
820} {null}
821do_test func-22.22 {
822 execsql {SELECT typeof(trim('hello',NULL));}
823} {null}
drh26b6d902007-03-17 13:27:54 +0000824
drh5708d2d2005-06-22 10:53:59 +0000825finish_test