drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 6 | # 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. |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 9 | # |
| 10 | #*********************************************************************** |
| 11 | # This file implements regression tests for SQLite library. The |
| 12 | # focus of this file is testing built-in functions. |
| 13 | # |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame^] | 14 | # $Id: func.test,v 1.5 2001/10/13 02:59:09 drh Exp $ |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # Create a table to work with. |
| 20 | # |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 21 | do_test func-0.0 { |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 22 | execsql {CREATE TABLE tbl1(t1 text)} |
| 23 | foreach word {this program is free software} { |
| 24 | execsql "INSERT INTO tbl1 VALUES('$word')" |
| 25 | } |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 26 | execsql {SELECT t1 FROM tbl1 ORDER BY t1} |
| 27 | } {free is program software this} |
| 28 | |
| 29 | # Check out the length() function |
| 30 | # |
| 31 | do_test func-1.0 { |
| 32 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
| 33 | } {4 2 7 8 4} |
| 34 | do_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()}} |
| 38 | do_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()}} |
| 42 | do_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 | # |
| 49 | do_test func-2.0 { |
| 50 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
| 51 | } {fr is pr so th} |
| 52 | do_test func-2.1 { |
| 53 | execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1} |
| 54 | } {r s r o h} |
| 55 | do_test func-2.2 { |
| 56 | execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1} |
| 57 | } {ee {} ogr ftw is} |
| 58 | do_test func-2.3 { |
| 59 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
| 60 | } {e s m e s} |
| 61 | do_test func-2.4 { |
| 62 | execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1} |
| 63 | } {e s m e s} |
| 64 | do_test func-2.5 { |
| 65 | execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1} |
| 66 | } {e i a r i} |
| 67 | do_test func-2.6 { |
| 68 | execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1} |
| 69 | } {ee is am re is} |
| 70 | do_test func-2.7 { |
| 71 | execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1} |
| 72 | } {fr {} gr wa th} |
| 73 | do_test func-2.8 { |
| 74 | execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)} |
| 75 | } {this software free program is} |
| 76 | |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 77 | # 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 | # |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 80 | if {[sqlite -encoding]=="UTF-8" && "\u1234"!="u1234"} { |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 81 | |
| 82 | # Put some UTF-8 characters in the database |
| 83 | # |
| 84 | do_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" |
| 91 | do_test func-3.1 { |
| 92 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
| 93 | } {10 8 5 5} |
| 94 | do_test func-3.2 { |
| 95 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
| 96 | } {ch co hi UT} |
| 97 | do_test func-3.3 { |
| 98 | execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1} |
| 99 | } "cha con hi\u1234 UTF" |
| 100 | do_test func-3.4 { |
| 101 | execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1} |
| 102 | } "ha on i\u1234 TF" |
| 103 | do_test func-3.5 { |
| 104 | execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1} |
| 105 | } "har ont i\u1234h TF-" |
| 106 | do_test func-3.6 { |
| 107 | execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1} |
| 108 | } "ar nt \u1234h F-" |
| 109 | do_test func-3.7 { |
| 110 | execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1} |
| 111 | } "ra ta ho -8" |
| 112 | do_test func-3.8 { |
| 113 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
| 114 | } "s s o 8" |
| 115 | do_test func-3.9 { |
| 116 | execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1} |
| 117 | } "er in \u1234h F-" |
| 118 | do_test func-3.10 { |
| 119 | execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1} |
| 120 | } "ter ain i\u1234h TF-" |
| 121 | |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 122 | } ;# End [sqlite -encoding]==UTF-8 and \u1234!=u1234 |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 123 | |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame^] | 124 | # Test the abs() and round() functions. |
| 125 | # |
| 126 | do_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()}} |
| 135 | do_test func-4.2 { |
| 136 | catchsql {SELECT abs() FROM t1} |
| 137 | } {1 {too few arguments to function abs()}} |
| 138 | do_test func-4.3 { |
| 139 | catchsql {SELECT abs(b) FROM t1 ORDER BY a} |
| 140 | } {0 {2 1.2345678901234 2}} |
| 141 | do_test func-4.4 { |
| 142 | catchsql {SELECT abs(c) FROM t1 ORDER BY a} |
| 143 | } {0 {3 12345.6789 5}} |
| 144 | |
| 145 | do_test func-4.5 { |
| 146 | catchsql {SELECT round(a,b,c) FROM t1} |
| 147 | } {1 {too many arguments to function round()}} |
| 148 | do_test func-4.6 { |
| 149 | catchsql {SELECT round(b,2) FROM t1} |
| 150 | } {0 {2.00 1.23 -2.00}} |
| 151 | do_test func-4.7 { |
| 152 | catchsql {SELECT round(b,0) FROM t1 ORDER BY a} |
| 153 | } {0 {2 1 -2}} |
| 154 | do_test func-4.8 { |
| 155 | catchsql {SELECT round(c) FROM t1 ORDER BY a} |
| 156 | } {0 {3 -12346 -5}} |
| 157 | do_test func-4.9 { |
| 158 | catchsql {SELECT round(c,a) FROM t1 ORDER BY a} |
| 159 | } {0 {3.0 -12345.68 -5.000}} |
| 160 | do_test func-4.10 { |
| 161 | catchsql {SELECT round() FROM t1 ORDER BY a} |
| 162 | } {1 {too few arguments to function round()}} |
| 163 | |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 164 | finish_test |