sqlite/test/func.test

165 lines
4.9 KiB
Plaintext

# 2001 September 15
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing built-in functions.
#
# $Id: func.test,v 1.5 2001/10/13 02:59:09 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Create a table to work with.
#
do_test func-0.0 {
execsql {CREATE TABLE tbl1(t1 text)}
foreach word {this program is free software} {
execsql "INSERT INTO tbl1 VALUES('$word')"
}
execsql {SELECT t1 FROM tbl1 ORDER BY t1}
} {free is program software this}
# Check out the length() function
#
do_test func-1.0 {
execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
} {4 2 7 8 4}
do_test func-1.1 {
set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg]
lappend r $msg
} {1 {too few arguments to function length()}}
do_test func-1.2 {
set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg]
lappend r $msg
} {1 {too many arguments to function length()}}
do_test func-1.3 {
execsql {SELECT length(t1), count(*) FROM tbl1 GROUP BY length(t1)
ORDER BY length(t1)}
} {2 1 4 2 7 1 8 1}
# Check out the substr() function
#
do_test func-2.0 {
execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
} {fr is pr so th}
do_test func-2.1 {
execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1}
} {r s r o h}
do_test func-2.2 {
execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1}
} {ee {} ogr ftw is}
do_test func-2.3 {
execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
} {e s m e s}
do_test func-2.4 {
execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1}
} {e s m e s}
do_test func-2.5 {
execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1}
} {e i a r i}
do_test func-2.6 {
execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1}
} {ee is am re is}
do_test func-2.7 {
execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1}
} {fr {} gr wa th}
do_test func-2.8 {
execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)}
} {this software free program is}
# Only do the following tests if TCL has UTF-8 capabilities and
# the UTF-8 encoding is turned on in the SQLite library.
#
if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} {
# Put some UTF-8 characters in the database
#
do_test func-3.0 {
execsql {DELETE FROM tbl1}
foreach word "contains UTF-8 characters hi\u1234ho" {
execsql "INSERT INTO tbl1 VALUES('$word')"
}
execsql {SELECT t1 FROM tbl1 ORDER BY t1}
} "characters contains hi\u1234ho UTF-8"
do_test func-3.1 {
execsql {SELECT length(t1) FROM tbl1 ORDER BY t1}
} {10 8 5 5}
do_test func-3.2 {
execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1}
} {ch co hi UT}
do_test func-3.3 {
execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1}
} "cha con hi\u1234 UTF"
do_test func-3.4 {
execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1}
} "ha on i\u1234 TF"
do_test func-3.5 {
execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1}
} "har ont i\u1234h TF-"
do_test func-3.6 {
execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1}
} "ar nt \u1234h F-"
do_test func-3.7 {
execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1}
} "ra ta ho -8"
do_test func-3.8 {
execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1}
} "s s o 8"
do_test func-3.9 {
execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1}
} "er in \u1234h F-"
do_test func-3.10 {
execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1}
} "ter ain i\u1234h TF-"
} ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234
# Test the abs() and round() functions.
#
do_test func-4.1 {
execsql {
CREATE TABLE t1(a,b,c);
INSERT INTO t1 VALUES(1,2,3);
INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890);
INSERT INTO t1 VALUES(3,-2,-5);
}
catchsql {SELECT abs(a,b) FROM t1}
} {1 {too many arguments to function abs()}}
do_test func-4.2 {
catchsql {SELECT abs() FROM t1}
} {1 {too few arguments to function abs()}}
do_test func-4.3 {
catchsql {SELECT abs(b) FROM t1 ORDER BY a}
} {0 {2 1.2345678901234 2}}
do_test func-4.4 {
catchsql {SELECT abs(c) FROM t1 ORDER BY a}
} {0 {3 12345.6789 5}}
do_test func-4.5 {
catchsql {SELECT round(a,b,c) FROM t1}
} {1 {too many arguments to function round()}}
do_test func-4.6 {
catchsql {SELECT round(b,2) FROM t1}
} {0 {2.00 1.23 -2.00}}
do_test func-4.7 {
catchsql {SELECT round(b,0) FROM t1 ORDER BY a}
} {0 {2 1 -2}}
do_test func-4.8 {
catchsql {SELECT round(c) FROM t1 ORDER BY a}
} {0 {3 -12346 -5}}
do_test func-4.9 {
catchsql {SELECT round(c,a) FROM t1 ORDER BY a}
} {0 {3.0 -12345.68 -5.000}}
do_test func-4.10 {
catchsql {SELECT round() FROM t1 ORDER BY a}
} {1 {too few arguments to function round()}}
finish_test