blob: 12d7e3927f824deec60c7fc9c6fbafe7912aca91 [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#
drhbf4133c2001-10-13 02:59:08 +000014# $Id: func.test,v 1.5 2001/10/13 02:59:09 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}
28
29# Check out the length() function
30#
31do_test func-1.0 {
32 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
33} {4 2 7 8 4}
34do_test func-1.1 {
35 set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
36 lappend r $msg
37} {1 {too few arguments to function length()}}
38do_test func-1.2 {
39 set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
40 lappend r $msg
41} {1 {too many arguments to function length()}}
42do_test func-1.3 {
43 execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
44 ORDER BY length(t1)}
45} {2 1 4 2 7 1 8 1}
46
47# Check out the substr() function
48#
49do_test func-2.0 {
50 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
51} {fr is pr so th}
52do_test func-2.1 {
53 execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
54} {r s r o h}
55do_test func-2.2 {
56 execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
57} {ee {} ogr ftw is}
58do_test func-2.3 {
59 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
60} {e s m e s}
61do_test func-2.4 {
62 execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
63} {e s m e s}
64do_test func-2.5 {
65 execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
66} {e i a r i}
67do_test func-2.6 {
68 execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
69} {ee is am re is}
70do_test func-2.7 {
71 execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
72} {fr {} gr wa th}
73do_test func-2.8 {
74 execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
75} {this software free program is}
76
drh297ecf12001-04-05 15:57:13 +000077# Only do the following tests if TCL has UTF-8 capabilities and
78# the UTF-8 encoding is turned on in the SQLite library.
79#
drhfbc3eab2001-04-06 16:13:42 +000080if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
drh297ecf12001-04-05 15:57:13 +000081
82# Put some UTF-8 characters in the database
83#
84do_test func-3.0 {
85 execsql {DELETE FROM tbl1}
86 foreach word "contains UTF-8 characters hi\u1234ho" {
87 execsql "INSERT INTO tbl1 VALUES('$word')"
88 }
89 execsql {SELECT t1 FROM tbl1 ORDER BY t1}
90} "characters contains hi\u1234ho UTF-8"
91do_test func-3.1 {
92 execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
93} {10 8 5 5}
94do_test func-3.2 {
95 execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
96} {ch co hi UT}
97do_test func-3.3 {
98 execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
99} "cha con hi\u1234 UTF"
100do_test func-3.4 {
101 execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
102} "ha on i\u1234 TF"
103do_test func-3.5 {
104 execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
105} "har ont i\u1234h TF-"
106do_test func-3.6 {
107 execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
108} "ar nt \u1234h F-"
109do_test func-3.7 {
110 execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
111} "ra ta ho -8"
112do_test func-3.8 {
113 execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
114} "s s o 8"
115do_test func-3.9 {
116 execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
117} "er in \u1234h F-"
118do_test func-3.10 {
119 execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
120} "ter ain i\u1234h TF-"
121
drhfbc3eab2001-04-06 16:13:42 +0000122} ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
drh297ecf12001-04-05 15:57:13 +0000123
drhbf4133c2001-10-13 02:59:08 +0000124# Test the abs() and round() functions.
125#
126do_test func-4.1 {
127 execsql {
128 CREATE TABLE t1(a,b,c);
129 INSERT INTO t1 VALUES(1,2,3);
130 INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
131 INSERT INTO t1 VALUES(3,-2,-5);
132 }
133 catchsql {SELECT abs(a,b) FROM t1}
134} {1 {too many arguments to function abs()}}
135do_test func-4.2 {
136 catchsql {SELECT abs() FROM t1}
137} {1 {too few arguments to function abs()}}
138do_test func-4.3 {
139 catchsql {SELECT abs(b) FROM t1 ORDER BY a}
140} {0 {2 1.2345678901234 2}}
141do_test func-4.4 {
142 catchsql {SELECT abs(c) FROM t1 ORDER BY a}
143} {0 {3 12345.6789 5}}
144
145do_test func-4.5 {
146 catchsql {SELECT round(a,b,c) FROM t1}
147} {1 {too many arguments to function round()}}
148do_test func-4.6 {
149 catchsql {SELECT round(b,2) FROM t1}
150} {0 {2.00 1.23 -2.00}}
151do_test func-4.7 {
152 catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
153} {0 {2 1 -2}}
154do_test func-4.8 {
155 catchsql {SELECT round(c) FROM t1 ORDER BY a}
156} {0 {3 -12346 -5}}
157do_test func-4.9 {
158 catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
159} {0 {3.0 -12345.68 -5.000}}
160do_test func-4.10 {
161 catchsql {SELECT round() FROM t1 ORDER BY a}
162} {1 {too few arguments to function round()}}
163
drhff6e9112000-08-28 16:21:58 +0000164finish_test