xref: /titanic_44/usr/src/lib/libsqlite/test/func.test (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
1
2#pragma ident	"%Z%%M%	%I%	%E% SMI"
3
4# 2001 September 15
5#
6# The author disclaims copyright to this source code.  In place of
7# a legal notice, here is a blessing:
8#
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.
12#
13#***********************************************************************
14# This file implements regression tests for SQLite library.  The
15# focus of this file is testing built-in functions.
16#
17# $Id: func.test,v 1.16.2.2 2004/07/18 21:14:05 drh Exp $
18
19set testdir [file dirname $argv0]
20source $testdir/tester.tcl
21
22# Create a table to work with.
23#
24do_test func-0.0 {
25  execsql {CREATE TABLE tbl1(t1 text)}
26  foreach word {this program is free software} {
27    execsql "INSERT INTO tbl1 VALUES('$word')"
28  }
29  execsql {SELECT t1 FROM tbl1 ORDER BY t1}
30} {free is program software this}
31do_test func-0.1 {
32  execsql {
33     CREATE TABLE t2(a);
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);
39     SELECT * FROM t2;
40  }
41} {1 {} 345 {} 67890}
42
43# Check out the length() function
44#
45do_test func-1.0 {
46  execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
47} {4 2 7 8 4}
48do_test func-1.1 {
49  set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
50  lappend r $msg
51} {1 {wrong number of arguments to function length()}}
52do_test func-1.2 {
53  set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
54  lappend r $msg
55} {1 {wrong number of arguments to function length()}}
56do_test func-1.3 {
57  execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
58           ORDER BY length(t1)}
59} {2 1 4 2 7 1 8 1}
60do_test func-1.4 {
61  execsql {SELECT coalesce(length(a),-1) FROM t2}
62} {1 -1 3 -1 5}
63
64# Check out the substr() function
65#
66do_test func-2.0 {
67  execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
68} {fr is pr so th}
69do_test func-2.1 {
70  execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
71} {r s r o h}
72do_test func-2.2 {
73  execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
74} {ee {} ogr ftw is}
75do_test func-2.3 {
76  execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
77} {e s m e s}
78do_test func-2.4 {
79  execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
80} {e s m e s}
81do_test func-2.5 {
82  execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
83} {e i a r i}
84do_test func-2.6 {
85  execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
86} {ee is am re is}
87do_test func-2.7 {
88  execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
89} {fr {} gr wa th}
90do_test func-2.8 {
91  execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
92} {this software free program is}
93do_test func-2.9 {
94  execsql {SELECT substr(a,1,1) FROM t2}
95} {1 {} 3 {} 6}
96do_test func-2.10 {
97  execsql {SELECT substr(a,2,2) FROM t2}
98} {{} {} 45 {} 78}
99
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.
102#
103if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
104
105# Put some UTF-8 characters in the database
106#
107do_test func-3.0 {
108  execsql {DELETE FROM tbl1}
109  foreach word "contains UTF-8 characters hi\u1234ho" {
110    execsql "INSERT INTO tbl1 VALUES('$word')"
111  }
112  execsql {SELECT t1 FROM tbl1 ORDER BY t1}
113} "UTF-8 characters contains hi\u1234ho"
114do_test func-3.1 {
115  execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
116} {5 10 8 5}
117do_test func-3.2 {
118  execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
119} {UT ch co hi}
120do_test func-3.3 {
121  execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
122} "UTF cha con hi\u1234"
123do_test func-3.4 {
124  execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
125} "TF ha on i\u1234"
126do_test func-3.5 {
127  execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
128} "TF- har ont i\u1234h"
129do_test func-3.6 {
130  execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
131} "F- ar nt \u1234h"
132do_test func-3.7 {
133  execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
134} "-8 ra ta ho"
135do_test func-3.8 {
136  execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
137} "8 s s o"
138do_test func-3.9 {
139  execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
140} "F- er in \u1234h"
141do_test func-3.10 {
142  execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
143} "TF- ter ain i\u1234h"
144do_test func-3.99 {
145  execsql {DELETE FROM tbl1}
146  foreach word {this program is free software} {
147    execsql "INSERT INTO tbl1 VALUES('$word')"
148  }
149  execsql {SELECT t1 FROM tbl1}
150} {this program is free software}
151
152} ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
153
154# Test the abs() and round() functions.
155#
156do_test func-4.1 {
157  execsql {
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);
162  }
163  catchsql {SELECT abs(a,b) FROM t1}
164} {1 {wrong number of arguments to function abs()}}
165do_test func-4.2 {
166  catchsql {SELECT abs() FROM t1}
167} {1 {wrong number of arguments to function abs()}}
168do_test func-4.3 {
169  catchsql {SELECT abs(b) FROM t1 ORDER BY a}
170} {0 {2 1.2345678901234 2}}
171do_test func-4.4 {
172  catchsql {SELECT abs(c) FROM t1 ORDER BY a}
173} {0 {3 12345.67890 5}}
174do_test func-4.4.1 {
175  execsql {SELECT abs(a) FROM t2}
176} {1 {} 345 {} 67890}
177do_test func-4.4.2 {
178  execsql {SELECT abs(t1) FROM tbl1}
179} {this program is free software}
180
181do_test func-4.5 {
182  catchsql {SELECT round(a,b,c) FROM t1}
183} {1 {wrong number of arguments to function round()}}
184do_test func-4.6 {
185  catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
186} {0 {-2.00 1.23 2.00}}
187do_test func-4.7 {
188  catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
189} {0 {2 1 -2}}
190do_test func-4.8 {
191  catchsql {SELECT round(c) FROM t1 ORDER BY a}
192} {0 {3 -12346 -5}}
193do_test func-4.9 {
194  catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
195} {0 {3.0 -12345.68 -5.000}}
196do_test func-4.10 {
197  catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
198} {0 {x3.0y x-12345.68y x-5.000y}}
199do_test func-4.11 {
200  catchsql {SELECT round() FROM t1 ORDER BY a}
201} {1 {wrong number of arguments to function round()}}
202do_test func-4.12 {
203  execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
204} {1.00 nil 345.00 nil 67890.00}
205do_test func-4.13 {
206  execsql {SELECT round(t1,2) FROM tbl1}
207} {0.00 0.00 0.00 0.00 0.00}
208
209# Test the upper() and lower() functions
210#
211do_test func-5.1 {
212  execsql {SELECT upper(t1) FROM tbl1}
213} {THIS PROGRAM IS FREE SOFTWARE}
214do_test func-5.2 {
215  execsql {SELECT lower(upper(t1)) FROM tbl1}
216} {this program is free software}
217do_test func-5.3 {
218  execsql {SELECT upper(a), lower(a) FROM t2}
219} {1 1 {} {} 345 345 {} {} 67890 67890}
220do_test func-5.4 {
221  catchsql {SELECT upper(a,5) FROM t2}
222} {1 {wrong number of arguments to function upper()}}
223do_test func-5.5 {
224  catchsql {SELECT upper(*) FROM t2}
225} {1 {wrong number of arguments to function upper()}}
226
227# Test the coalesce() and nullif() functions
228#
229do_test func-6.1 {
230  execsql {SELECT coalesce(a,'xyz') FROM t2}
231} {1 xyz 345 xyz 67890}
232do_test func-6.2 {
233  execsql {SELECT coalesce(upper(a),'nil') FROM t2}
234} {1 nil 345 nil 67890}
235do_test func-6.3 {
236  execsql {SELECT coalesce(nullif(1,1),'nil')}
237} {nil}
238do_test func-6.4 {
239  execsql {SELECT coalesce(nullif(1,2),'nil')}
240} {1}
241do_test func-6.5 {
242  execsql {SELECT coalesce(nullif(1,NULL),'nil')}
243} {1}
244
245
246# Test the last_insert_rowid() function
247#
248do_test func-7.1 {
249  execsql {SELECT last_insert_rowid()}
250} [db last_insert_rowid]
251
252# Tests for aggregate functions and how they handle NULLs.
253#
254do_test func-8.1 {
255  execsql {
256    SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
257  }
258} {68236 3 22745.33 1 67890 5}
259do_test func-8.2 {
260  execsql {
261    SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2;
262  }
263} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
264do_test func-8.3 {
265  execsql {
266    CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC;
267    SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
268  }
269} {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
270do_test func-8.4 {
271  execsql {
272    SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3;
273  }
274} {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP}
275
276# How do you test the random() function in a meaningful, deterministic way?
277#
278do_test func-9.1 {
279  execsql {
280    SELECT random() is not null;
281  }
282} {1}
283
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
287# functions.
288#
289db close
290set ::DB [sqlite db test.db]
291sqlite_register_test_function $::DB testfunc
292do_test func-10.1 {
293  catchsql {
294    SELECT testfunc(NULL,NULL);
295  }
296} {1 {first argument to test function may not be NULL}}
297do_test func-10.2 {
298  execsql {
299    SELECT testfunc(
300     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
301     'int', 1234
302    );
303  }
304} {1234}
305do_test func-10.3 {
306  execsql {
307    SELECT testfunc(
308     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
309     'string', NULL
310    );
311  }
312} {{}}
313do_test func-10.4 {
314  execsql {
315    SELECT testfunc(
316     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
317     'double', 1.234
318    );
319  }
320} {1.234}
321do_test func-10.5 {
322  execsql {
323    SELECT testfunc(
324     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
325     'int', 1234,
326     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
327     'string', NULL,
328     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
329     'double', 1.234,
330     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
331     'int', 1234,
332     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
333     'string', NULL,
334     'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
335     'double', 1.234
336    );
337  }
338} {1.234}
339
340# Test the built-in sqlite_version(*) SQL function.
341#
342do_test func-11.1 {
343  execsql {
344    SELECT sqlite_version(*);
345  }
346} [sqlite -version]
347
348finish_test
349