blob: 98e2a97f3cd9188062ca196d45cb8e41b3fde777 [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#
drh739105c2002-05-29 23:22:23 +000014# $Id: func.test,v 1.12 2002/05/29 23:22:23 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
drh297ecf12001-04-05 15:57:13 +000097# Only do the following tests if TCL has UTF-8 capabilities and
98# the UTF-8 encoding is turned on in the SQLite library.
99#
drhfbc3eab2001-04-06 16:13:42 +0000100if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
drh297ecf12001-04-05 15:57:13 +0000101
102# Put some UTF-8 characters in the database
103#
104do_test func-3.0 {
105 execsql {DELETE FROM tbl1}
106 foreach word "contains UTF-8 characters hi\u1234ho" {
107 execsql "INSERT INTO tbl1 VALUES('$word')"
108 }
109 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
110} "characters contains hi\u1234ho UTF-8"
111do_test func-3.1 {
112 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
113} {10 8 5 5}
114do_test func-3.2 {
115 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
116} {ch co hi UT}
117do_test func-3.3 {
118 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
119} "cha con hi\u1234 UTF"
120do_test func-3.4 {
121 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
122} "ha on i\u1234 TF"
123do_test func-3.5 {
124 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
125} "har ont i\u1234h TF-"
126do_test func-3.6 {
127 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
128} "ar nt \u1234h F-"
129do_test func-3.7 {
130 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
131} "ra ta ho -8"
132do_test func-3.8 {
133 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
134} "s s o 8"
135do_test func-3.9 {
136 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
137} "er in \u1234h F-"
138do_test func-3.10 {
139 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
140} "ter ain i\u1234h TF-"
drh832508b2002-03-02 17:04:07 +0000141do_test func-3.99 {
142 execsql {DELETE FROM tbl1}
143 foreach word {this program is free software} {
144 execsql "INSERT INTO tbl1 VALUES('$word')"
145 }
146 execsql {SELECT t1 FROM tbl1}
147} {this program is free software}
drh297ecf12001-04-05 15:57:13 +0000148
drhfbc3eab2001-04-06 16:13:42 +0000149} ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
drh297ecf12001-04-05 15:57:13 +0000150
drhbf4133c2001-10-13 02:59:08 +0000151# Test the abs() and round() functions.
152#
153do_test func-4.1 {
154 execsql {
155 CREATE TABLE t1(a,b,c);
156 INSERT INTO t1 VALUES(1,2,3);
157 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
158 INSERT INTO t1 VALUES(3,-2,-5);
159 }
160 catchsql {SELECT abs(a,b) FROM t1}
drh89425d52002-02-28 03:04:48 +0000161} {1 {wrong number of arguments to function abs()}}
drhbf4133c2001-10-13 02:59:08 +0000162do_test func-4.2 {
163 catchsql {SELECT abs() FROM t1}
drh89425d52002-02-28 03:04:48 +0000164} {1 {wrong number of arguments to function abs()}}
drhbf4133c2001-10-13 02:59:08 +0000165do_test func-4.3 {
166 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
167} {0 {2 1.2345678901234 2}}
168do_test func-4.4 {
169 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
drh89425d52002-02-28 03:04:48 +0000170} {0 {3 12345.67890 5}}
drh832508b2002-03-02 17:04:07 +0000171do_test func-4.4.1 {
172 execsql {SELECT abs(a) FROM t2}
173} {1 {} 345 {} 67890}
174do_test func-4.4.2 {
175 execsql {SELECT abs(t1) FROM tbl1}
176} {this program is free software}
drhbf4133c2001-10-13 02:59:08 +0000177
178do_test func-4.5 {
179 catchsql {SELECT round(a,b,c) FROM t1}
drh89425d52002-02-28 03:04:48 +0000180} {1 {wrong number of arguments to function round()}}
drhbf4133c2001-10-13 02:59:08 +0000181do_test func-4.6 {
drh8aff1012001-12-22 14:49:24 +0000182 catchsql {SELECT round(b,2) FROM t1 ORDER BY b}
183} {0 {-2.00 1.23 2.00}}
drhbf4133c2001-10-13 02:59:08 +0000184do_test func-4.7 {
185 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
186} {0 {2 1 -2}}
187do_test func-4.8 {
188 catchsql {SELECT round(c) FROM t1 ORDER BY a}
189} {0 {3 -12346 -5}}
190do_test func-4.9 {
191 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
192} {0 {3.0 -12345.68 -5.000}}
193do_test func-4.10 {
drh01a34662001-10-20 12:30:10 +0000194 catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a}
195} {0 {x3.0y x-12345.68y x-5.000y}}
196do_test func-4.11 {
drhbf4133c2001-10-13 02:59:08 +0000197 catchsql {SELECT round() FROM t1 ORDER BY a}
drh89425d52002-02-28 03:04:48 +0000198} {1 {wrong number of arguments to function round()}}
drh832508b2002-03-02 17:04:07 +0000199do_test func-4.12 {
drhbb113512002-05-27 01:04:51 +0000200 execsql {SELECT coalesce(round(a,2),'nil') FROM t2}
201} {1.00 nil 345.00 nil 67890.00}
drh832508b2002-03-02 17:04:07 +0000202do_test func-4.13 {
203 execsql {SELECT round(t1,2) FROM tbl1}
204} {0.00 0.00 0.00 0.00 0.00}
205
206# Test the upper() and lower() functions
207#
208do_test func-5.1 {
209 execsql {SELECT upper(t1) FROM tbl1}
210} {THIS PROGRAM IS FREE SOFTWARE}
211do_test func-5.2 {
212 execsql {SELECT lower(upper(t1)) FROM tbl1}
213} {this program is free software}
214do_test func-5.3 {
215 execsql {SELECT upper(a), lower(a) FROM t2}
216} {1 1 {} {} 345 345 {} {} 67890 67890}
217do_test func-5.4 {
218 catchsql {SELECT upper(a,5) FROM t2}
219} {1 {wrong number of arguments to function upper()}}
220do_test func-5.5 {
221 catchsql {SELECT upper(*) FROM t2}
222} {1 {wrong number of arguments to function upper()}}
223
224# Test the coalesce() function
225#
226do_test func-6.1 {
227 execsql {SELECT coalesce(a,'xyz') FROM t2}
228} {1 xyz 345 xyz 67890}
229do_test func-6.2 {
230 execsql {SELECT coalesce(upper(a),'nil') FROM t2}
231} {1 nil 345 nil 67890}
232
drh6ed41ad2002-04-06 14:10:47 +0000233# Test the last_insert_rowid() function
234#
235do_test func-7.1 {
236 execsql {SELECT last_insert_rowid()}
237} [db last_insert_rowid]
238
drh739105c2002-05-29 23:22:23 +0000239# Tests for aggregate functions and how they handle NULLs.
240#
241do_test func-8.1 {
242 execsql {
243 SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2;
244 }
245} {68236 3 22745.33 1 67890 5}
246
drhbf4133c2001-10-13 02:59:08 +0000247
drhff6e9112000-08-28 16:21:58 +0000248finish_test