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 | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame^] | 14 | # $Id: func.test,v 1.55 2006/09/16 21:45:14 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} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 28 | do_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} |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 39 | |
| 40 | # Check out the length() function |
| 41 | # |
| 42 | do_test func-1.0 { |
| 43 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
| 44 | } {4 2 7 8 4} |
| 45 | do_test func-1.1 { |
| 46 | set r [catch {execsql {SELECT length(*) FROM tbl1 ORDER BY t1}} msg] |
| 47 | lappend r $msg |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 48 | } {1 {wrong number of arguments to function length()}} |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 49 | do_test func-1.2 { |
| 50 | set r [catch {execsql {SELECT length(t1,5) FROM tbl1 ORDER BY t1}} msg] |
| 51 | lappend r $msg |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 52 | } {1 {wrong number of arguments to function length()}} |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 53 | do_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} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 57 | do_test func-1.4 { |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 58 | execsql {SELECT coalesce(length(a),-1) FROM t2} |
| 59 | } {1 -1 3 -1 5} |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 60 | |
| 61 | # Check out the substr() function |
| 62 | # |
| 63 | do_test func-2.0 { |
| 64 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
| 65 | } {fr is pr so th} |
| 66 | do_test func-2.1 { |
| 67 | execsql {SELECT substr(t1,2,1) FROM tbl1 ORDER BY t1} |
| 68 | } {r s r o h} |
| 69 | do_test func-2.2 { |
| 70 | execsql {SELECT substr(t1,3,3) FROM tbl1 ORDER BY t1} |
| 71 | } {ee {} ogr ftw is} |
| 72 | do_test func-2.3 { |
| 73 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
| 74 | } {e s m e s} |
| 75 | do_test func-2.4 { |
| 76 | execsql {SELECT substr(t1,-1,2) FROM tbl1 ORDER BY t1} |
| 77 | } {e s m e s} |
| 78 | do_test func-2.5 { |
| 79 | execsql {SELECT substr(t1,-2,1) FROM tbl1 ORDER BY t1} |
| 80 | } {e i a r i} |
| 81 | do_test func-2.6 { |
| 82 | execsql {SELECT substr(t1,-2,2) FROM tbl1 ORDER BY t1} |
| 83 | } {ee is am re is} |
| 84 | do_test func-2.7 { |
| 85 | execsql {SELECT substr(t1,-4,2) FROM tbl1 ORDER BY t1} |
| 86 | } {fr {} gr wa th} |
| 87 | do_test func-2.8 { |
| 88 | execsql {SELECT t1 FROM tbl1 ORDER BY substr(t1,2,20)} |
| 89 | } {this software free program is} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 90 | do_test func-2.9 { |
| 91 | execsql {SELECT substr(a,1,1) FROM t2} |
| 92 | } {1 {} 3 {} 6} |
| 93 | do_test func-2.10 { |
| 94 | execsql {SELECT substr(a,2,2) FROM t2} |
| 95 | } {{} {} 45 {} 78} |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 96 | |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 97 | # Only do the following tests if TCL has UTF-8 capabilities |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 98 | # |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 99 | if {"\u1234"!="u1234"} { |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 100 | |
| 101 | # Put some UTF-8 characters in the database |
| 102 | # |
| 103 | do_test func-3.0 { |
| 104 | execsql {DELETE FROM tbl1} |
| 105 | foreach word "contains UTF-8 characters hi\u1234ho" { |
| 106 | execsql "INSERT INTO tbl1 VALUES('$word')" |
| 107 | } |
| 108 | execsql {SELECT t1 FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 109 | } "UTF-8 characters contains hi\u1234ho" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 110 | do_test func-3.1 { |
| 111 | execsql {SELECT length(t1) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 112 | } {5 10 8 5} |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 113 | do_test func-3.2 { |
| 114 | execsql {SELECT substr(t1,1,2) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 115 | } {UT ch co hi} |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 116 | do_test func-3.3 { |
| 117 | execsql {SELECT substr(t1,1,3) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 118 | } "UTF cha con hi\u1234" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 119 | do_test func-3.4 { |
| 120 | execsql {SELECT substr(t1,2,2) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 121 | } "TF ha on i\u1234" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 122 | do_test func-3.5 { |
| 123 | execsql {SELECT substr(t1,2,3) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 124 | } "TF- har ont i\u1234h" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 125 | do_test func-3.6 { |
| 126 | execsql {SELECT substr(t1,3,2) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 127 | } "F- ar nt \u1234h" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 128 | do_test func-3.7 { |
| 129 | execsql {SELECT substr(t1,4,2) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 130 | } "-8 ra ta ho" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 131 | do_test func-3.8 { |
| 132 | execsql {SELECT substr(t1,-1,1) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 133 | } "8 s s o" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 134 | do_test func-3.9 { |
| 135 | execsql {SELECT substr(t1,-3,2) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 136 | } "F- er in \u1234h" |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 137 | do_test func-3.10 { |
| 138 | execsql {SELECT substr(t1,-4,3) FROM tbl1 ORDER BY t1} |
drh | a9e99ae | 2002-08-13 23:02:57 +0000 | [diff] [blame] | 139 | } "TF- ter ain i\u1234h" |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 140 | do_test func-3.99 { |
| 141 | execsql {DELETE FROM tbl1} |
| 142 | foreach word {this program is free software} { |
| 143 | execsql "INSERT INTO tbl1 VALUES('$word')" |
| 144 | } |
| 145 | execsql {SELECT t1 FROM tbl1} |
| 146 | } {this program is free software} |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 147 | |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 148 | } ;# End \u1234!=u1234 |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 149 | |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 150 | # Test the abs() and round() functions. |
| 151 | # |
| 152 | do_test func-4.1 { |
| 153 | execsql { |
| 154 | CREATE TABLE t1(a,b,c); |
| 155 | INSERT INTO t1 VALUES(1,2,3); |
| 156 | INSERT INTO t1 VALUES(2,1.2345678901234,-12345.67890); |
| 157 | INSERT INTO t1 VALUES(3,-2,-5); |
| 158 | } |
| 159 | catchsql {SELECT abs(a,b) FROM t1} |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 160 | } {1 {wrong number of arguments to function abs()}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 161 | do_test func-4.2 { |
| 162 | catchsql {SELECT abs() FROM t1} |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 163 | } {1 {wrong number of arguments to function abs()}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 164 | do_test func-4.3 { |
| 165 | catchsql {SELECT abs(b) FROM t1 ORDER BY a} |
| 166 | } {0 {2 1.2345678901234 2}} |
| 167 | do_test func-4.4 { |
| 168 | catchsql {SELECT abs(c) FROM t1 ORDER BY a} |
danielk1977 | 3d1bfea | 2004-05-14 11:00:53 +0000 | [diff] [blame] | 169 | } {0 {3 12345.6789 5}} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 170 | do_test func-4.4.1 { |
| 171 | execsql {SELECT abs(a) FROM t2} |
| 172 | } {1 {} 345 {} 67890} |
| 173 | do_test func-4.4.2 { |
| 174 | execsql {SELECT abs(t1) FROM tbl1} |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame] | 175 | } {0.0 0.0 0.0 0.0 0.0} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 176 | |
| 177 | do_test func-4.5 { |
| 178 | catchsql {SELECT round(a,b,c) FROM t1} |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 179 | } {1 {wrong number of arguments to function round()}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 180 | do_test func-4.6 { |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 181 | catchsql {SELECT round(b,2) FROM t1 ORDER BY b} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 182 | } {0 {-2.0 1.23 2.0}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 183 | do_test func-4.7 { |
| 184 | catchsql {SELECT round(b,0) FROM t1 ORDER BY a} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 185 | } {0 {2.0 1.0 -2.0}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 186 | do_test func-4.8 { |
| 187 | catchsql {SELECT round(c) FROM t1 ORDER BY a} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 188 | } {0 {3.0 -12346.0 -5.0}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 189 | do_test func-4.9 { |
| 190 | catchsql {SELECT round(c,a) FROM t1 ORDER BY a} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 191 | } {0 {3.0 -12345.68 -5.0}} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 192 | do_test func-4.10 { |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 193 | catchsql {SELECT 'x' || round(c,a) || 'y' FROM t1 ORDER BY a} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 194 | } {0 {x3.0y x-12345.68y x-5.0y}} |
drh | 01a3466 | 2001-10-20 12:30:10 +0000 | [diff] [blame] | 195 | do_test func-4.11 { |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 196 | catchsql {SELECT round() FROM t1 ORDER BY a} |
drh | 89425d5 | 2002-02-28 03:04:48 +0000 | [diff] [blame] | 197 | } {1 {wrong number of arguments to function round()}} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 198 | do_test func-4.12 { |
drh | bb11351 | 2002-05-27 01:04:51 +0000 | [diff] [blame] | 199 | execsql {SELECT coalesce(round(a,2),'nil') FROM t2} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 200 | } {1.0 nil 345.0 nil 67890.0} |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 201 | do_test func-4.13 { |
| 202 | execsql {SELECT round(t1,2) FROM tbl1} |
drh | d589a92 | 2006-03-02 03:02:48 +0000 | [diff] [blame] | 203 | } {0.0 0.0 0.0 0.0 0.0} |
| 204 | do_test func-4.14 { |
| 205 | execsql {SELECT typeof(round(5.1,1));} |
| 206 | } {real} |
| 207 | do_test func-4.15 { |
| 208 | execsql {SELECT typeof(round(5.1));} |
| 209 | } {real} |
| 210 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 211 | |
| 212 | # Test the upper() and lower() functions |
| 213 | # |
| 214 | do_test func-5.1 { |
| 215 | execsql {SELECT upper(t1) FROM tbl1} |
| 216 | } {THIS PROGRAM IS FREE SOFTWARE} |
| 217 | do_test func-5.2 { |
| 218 | execsql {SELECT lower(upper(t1)) FROM tbl1} |
| 219 | } {this program is free software} |
| 220 | do_test func-5.3 { |
| 221 | execsql {SELECT upper(a), lower(a) FROM t2} |
| 222 | } {1 1 {} {} 345 345 {} {} 67890 67890} |
| 223 | do_test func-5.4 { |
| 224 | catchsql {SELECT upper(a,5) FROM t2} |
| 225 | } {1 {wrong number of arguments to function upper()}} |
| 226 | do_test func-5.5 { |
| 227 | catchsql {SELECT upper(*) FROM t2} |
| 228 | } {1 {wrong number of arguments to function upper()}} |
| 229 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 230 | # Test the coalesce() and nullif() functions |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 231 | # |
| 232 | do_test func-6.1 { |
| 233 | execsql {SELECT coalesce(a,'xyz') FROM t2} |
| 234 | } {1 xyz 345 xyz 67890} |
| 235 | do_test func-6.2 { |
| 236 | execsql {SELECT coalesce(upper(a),'nil') FROM t2} |
| 237 | } {1 nil 345 nil 67890} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 238 | do_test func-6.3 { |
| 239 | execsql {SELECT coalesce(nullif(1,1),'nil')} |
| 240 | } {nil} |
| 241 | do_test func-6.4 { |
| 242 | execsql {SELECT coalesce(nullif(1,2),'nil')} |
| 243 | } {1} |
| 244 | do_test func-6.5 { |
| 245 | execsql {SELECT coalesce(nullif(1,NULL),'nil')} |
| 246 | } {1} |
| 247 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 248 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 249 | # Test the last_insert_rowid() function |
| 250 | # |
| 251 | do_test func-7.1 { |
| 252 | execsql {SELECT last_insert_rowid()} |
| 253 | } [db last_insert_rowid] |
| 254 | |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 255 | # Tests for aggregate functions and how they handle NULLs. |
| 256 | # |
| 257 | do_test func-8.1 { |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame] | 258 | ifcapable explain { |
| 259 | execsql {EXPLAIN SELECT sum(a) FROM t2;} |
| 260 | } |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 261 | execsql { |
| 262 | SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2; |
| 263 | } |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 264 | } {68236 3 22745.33 1 67890 5} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 265 | do_test func-8.2 { |
| 266 | execsql { |
| 267 | SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2; |
| 268 | } |
| 269 | } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 270 | |
| 271 | ifcapable tempdb { |
| 272 | do_test func-8.3 { |
| 273 | execsql { |
| 274 | CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; |
| 275 | SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 276 | } |
| 277 | } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
| 278 | } else { |
| 279 | do_test func-8.3 { |
| 280 | execsql { |
| 281 | CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; |
| 282 | SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 283 | } |
| 284 | } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
| 285 | } |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 286 | do_test func-8.4 { |
| 287 | execsql { |
| 288 | SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 289 | } |
| 290 | } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 291 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 292 | # How do you test the random() function in a meaningful, deterministic way? |
| 293 | # |
| 294 | do_test func-9.1 { |
| 295 | execsql { |
| 296 | SELECT random() is not null; |
| 297 | } |
| 298 | } {1} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 299 | |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 300 | # Use the "sqlite_register_test_function" TCL command which is part of |
| 301 | # the text fixture in order to verify correct operation of some of |
| 302 | # the user-defined SQL function APIs that are not used by the built-in |
| 303 | # functions. |
| 304 | # |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 305 | set ::DB [sqlite3_connection_pointer db] |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 306 | sqlite_register_test_function $::DB testfunc |
| 307 | do_test func-10.1 { |
| 308 | catchsql { |
| 309 | SELECT testfunc(NULL,NULL); |
| 310 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 311 | } {1 {first argument should be one of: int int64 string double null value}} |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 312 | do_test func-10.2 { |
| 313 | execsql { |
| 314 | SELECT testfunc( |
| 315 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 316 | 'int', 1234 |
| 317 | ); |
| 318 | } |
| 319 | } {1234} |
| 320 | do_test func-10.3 { |
| 321 | execsql { |
| 322 | SELECT testfunc( |
| 323 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 324 | 'string', NULL |
| 325 | ); |
| 326 | } |
| 327 | } {{}} |
| 328 | do_test func-10.4 { |
| 329 | execsql { |
| 330 | SELECT testfunc( |
| 331 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 332 | 'double', 1.234 |
| 333 | ); |
| 334 | } |
| 335 | } {1.234} |
| 336 | do_test func-10.5 { |
| 337 | execsql { |
| 338 | SELECT testfunc( |
| 339 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 340 | 'int', 1234, |
| 341 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 342 | 'string', NULL, |
| 343 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 344 | 'double', 1.234, |
| 345 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 346 | 'int', 1234, |
| 347 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 348 | 'string', NULL, |
| 349 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 350 | 'double', 1.234 |
| 351 | ); |
| 352 | } |
| 353 | } {1.234} |
| 354 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 355 | # Test the built-in sqlite_version(*) SQL function. |
| 356 | # |
| 357 | do_test func-11.1 { |
| 358 | execsql { |
| 359 | SELECT sqlite_version(*); |
| 360 | } |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 361 | } [sqlite3 -version] |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 362 | |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 363 | # Test that destructors passed to sqlite3 by calls to sqlite3_result_text() |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 364 | # etc. are called. These tests use two special user-defined functions |
| 365 | # (implemented in func.c) only available in test builds. |
| 366 | # |
| 367 | # Function test_destructor() takes one argument and returns a copy of the |
| 368 | # text form of that argument. A destructor is associated with the return |
| 369 | # value. Function test_destructor_count() returns the number of outstanding |
| 370 | # destructor calls for values returned by test_destructor(). |
| 371 | # |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 372 | do_test func-12.1 { |
| 373 | execsql { |
| 374 | SELECT test_destructor('hello world'), test_destructor_count(); |
| 375 | } |
| 376 | } {{hello world} 1} |
| 377 | do_test func-12.2 { |
| 378 | execsql { |
| 379 | SELECT test_destructor_count(); |
| 380 | } |
| 381 | } {0} |
| 382 | do_test func-12.3 { |
| 383 | execsql { |
| 384 | SELECT test_destructor('hello')||' world', test_destructor_count(); |
| 385 | } |
| 386 | } {{hello world} 0} |
| 387 | do_test func-12.4 { |
| 388 | execsql { |
| 389 | SELECT test_destructor_count(); |
| 390 | } |
| 391 | } {0} |
| 392 | do_test func-12.5 { |
| 393 | execsql { |
| 394 | CREATE TABLE t4(x); |
| 395 | INSERT INTO t4 VALUES(test_destructor('hello')); |
| 396 | INSERT INTO t4 VALUES(test_destructor('world')); |
| 397 | SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4; |
| 398 | } |
| 399 | } {hello world} |
| 400 | do_test func-12.6 { |
| 401 | execsql { |
| 402 | SELECT test_destructor_count(); |
| 403 | } |
| 404 | } {0} |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 405 | do_test func-12.7 { |
| 406 | execsql { |
| 407 | DROP TABLE t4; |
| 408 | } |
| 409 | } {} |
| 410 | |
| 411 | # Test that the auxdata API for scalar functions works. This test uses |
| 412 | # a special user-defined function only available in test builds, |
| 413 | # test_auxdata(). Function test_auxdata() takes any number of arguments. |
drh | 9237825 | 2006-03-26 01:21:22 +0000 | [diff] [blame] | 414 | btree_breakpoint |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 415 | do_test func-13.1 { |
| 416 | execsql { |
| 417 | SELECT test_auxdata('hello world'); |
| 418 | } |
| 419 | } {0} |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 420 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 421 | do_test func-13.2 { |
| 422 | execsql { |
| 423 | CREATE TABLE t4(a, b); |
| 424 | INSERT INTO t4 VALUES('abc', 'def'); |
| 425 | INSERT INTO t4 VALUES('ghi', 'jkl'); |
| 426 | } |
| 427 | } {} |
| 428 | do_test func-13.3 { |
| 429 | execsql { |
| 430 | SELECT test_auxdata('hello world') FROM t4; |
| 431 | } |
| 432 | } {0 1} |
| 433 | do_test func-13.4 { |
| 434 | execsql { |
| 435 | SELECT test_auxdata('hello world', 123) FROM t4; |
| 436 | } |
| 437 | } {{0 0} {1 1}} |
| 438 | do_test func-13.5 { |
| 439 | execsql { |
| 440 | SELECT test_auxdata('hello world', a) FROM t4; |
| 441 | } |
| 442 | } {{0 0} {1 0}} |
| 443 | do_test func-13.6 { |
| 444 | execsql { |
| 445 | SELECT test_auxdata('hello'||'world', a) FROM t4; |
| 446 | } |
| 447 | } {{0 0} {1 0}} |
| 448 | |
| 449 | # Test that auxilary data is preserved between calls for SQL variables. |
| 450 | do_test func-13.7 { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 451 | set DB [sqlite3_connection_pointer db] |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 452 | set sql "SELECT test_auxdata( ? , a ) FROM t4;" |
| 453 | set STMT [sqlite3_prepare $DB $sql -1 TAIL] |
| 454 | sqlite3_bind_text $STMT 1 hello -1 |
| 455 | set res [list] |
| 456 | while { "SQLITE_ROW"==[sqlite3_step $STMT] } { |
| 457 | lappend res [sqlite3_column_text $STMT 0] |
| 458 | } |
| 459 | lappend res [sqlite3_finalize $STMT] |
| 460 | } {{0 0} {1 0} SQLITE_OK} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 461 | |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 462 | # Make sure that a function with a very long name is rejected |
| 463 | do_test func-14.1 { |
| 464 | catch { |
| 465 | db function [string repeat X 254] {return "hello"} |
| 466 | } |
| 467 | } {0} |
| 468 | do_test func-14.2 { |
| 469 | catch { |
| 470 | db function [string repeat X 256] {return "hello"} |
| 471 | } |
| 472 | } {1} |
| 473 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 474 | do_test func-15.1 { |
| 475 | catchsql { |
| 476 | select test_error(NULL); |
| 477 | } |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 478 | } {1 {}} |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 479 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 480 | # Test the quote function for BLOB and NULL values. |
| 481 | do_test func-16.1 { |
| 482 | execsql { |
| 483 | CREATE TABLE tbl2(a, b); |
| 484 | } |
| 485 | set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL] |
| 486 | sqlite3_bind_blob $::STMT 1 abc 3 |
| 487 | sqlite3_step $::STMT |
| 488 | sqlite3_finalize $::STMT |
| 489 | execsql { |
| 490 | SELECT quote(a), quote(b) FROM tbl2; |
| 491 | } |
| 492 | } {X'616263' NULL} |
| 493 | |
drh | 2501eb1 | 2005-08-12 23:20:53 +0000 | [diff] [blame] | 494 | # Correctly handle function error messages that include %. Ticket #1354 |
| 495 | # |
| 496 | do_test func-17.1 { |
| 497 | proc testfunc1 args {error "Error %d with %s percents %p"} |
| 498 | db function testfunc1 ::testfunc1 |
| 499 | catchsql { |
| 500 | SELECT testfunc1(1,2,3); |
| 501 | } |
| 502 | } {1 {Error %d with %s percents %p}} |
| 503 | |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 504 | # The SUM function should return integer results when all inputs are integer. |
| 505 | # |
| 506 | do_test func-18.1 { |
| 507 | execsql { |
| 508 | CREATE TABLE t5(x); |
| 509 | INSERT INTO t5 VALUES(1); |
| 510 | INSERT INTO t5 VALUES(-99); |
| 511 | INSERT INTO t5 VALUES(10000); |
| 512 | SELECT sum(x) FROM t5; |
| 513 | } |
| 514 | } {9902} |
| 515 | do_test func-18.2 { |
| 516 | execsql { |
| 517 | INSERT INTO t5 VALUES(0.0); |
| 518 | SELECT sum(x) FROM t5; |
| 519 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 520 | } {9902.0} |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 521 | |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 522 | # The sum of nothing is NULL. But the sum of all NULLs is NULL. |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 523 | # |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 524 | # The TOTAL of nothing is 0.0. |
| 525 | # |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 526 | do_test func-18.3 { |
| 527 | execsql { |
| 528 | DELETE FROM t5; |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 529 | SELECT sum(x), total(x) FROM t5; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 530 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 531 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 532 | do_test func-18.4 { |
| 533 | execsql { |
| 534 | INSERT INTO t5 VALUES(NULL); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 535 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 536 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 537 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 538 | do_test func-18.5 { |
| 539 | execsql { |
| 540 | INSERT INTO t5 VALUES(NULL); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 541 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 542 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 543 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 544 | do_test func-18.6 { |
| 545 | execsql { |
| 546 | INSERT INTO t5 VALUES(123); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 547 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 548 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 549 | } {123 123.0} |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 550 | |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 551 | # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes |
| 552 | # an error. The non-standard TOTAL() function continues to give a helpful |
| 553 | # result. |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 554 | # |
| 555 | do_test func-18.10 { |
| 556 | execsql { |
| 557 | CREATE TABLE t6(x INTEGER); |
| 558 | INSERT INTO t6 VALUES(1); |
| 559 | INSERT INTO t6 VALUES(1<<62); |
| 560 | SELECT sum(x) - ((1<<62)+1) from t6; |
| 561 | } |
| 562 | } 0 |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 563 | do_test func-18.11 { |
| 564 | execsql { |
| 565 | SELECT typeof(sum(x)) FROM t6 |
| 566 | } |
| 567 | } integer |
| 568 | do_test func-18.12 { |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 569 | catchsql { |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 570 | INSERT INTO t6 VALUES(1<<62); |
| 571 | SELECT sum(x) - ((1<<62)*2.0+1) from t6; |
| 572 | } |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 573 | } {1 {integer overflow}} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 574 | do_test func-18.13 { |
| 575 | execsql { |
| 576 | SELECT total(x) - ((1<<62)*2.0+1) FROM t6 |
| 577 | } |
| 578 | } 0.0 |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 579 | do_test func-18.14 { |
| 580 | execsql { |
| 581 | SELECT sum(-9223372036854775805); |
| 582 | } |
| 583 | } -9223372036854775805 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 584 | |
| 585 | ifcapable compound&&subquery { |
| 586 | |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 587 | do_test func-18.15 { |
| 588 | catchsql { |
| 589 | SELECT sum(x) FROM |
| 590 | (SELECT 9223372036854775807 AS x UNION ALL |
| 591 | SELECT 10 AS x); |
| 592 | } |
| 593 | } {1 {integer overflow}} |
| 594 | do_test func-18.16 { |
| 595 | catchsql { |
| 596 | SELECT sum(x) FROM |
| 597 | (SELECT 9223372036854775807 AS x UNION ALL |
| 598 | SELECT -10 AS x); |
| 599 | } |
| 600 | } {0 9223372036854775797} |
| 601 | do_test func-18.17 { |
| 602 | catchsql { |
| 603 | SELECT sum(x) FROM |
| 604 | (SELECT -9223372036854775807 AS x UNION ALL |
| 605 | SELECT 10 AS x); |
| 606 | } |
| 607 | } {0 -9223372036854775797} |
| 608 | do_test func-18.18 { |
| 609 | catchsql { |
| 610 | SELECT sum(x) FROM |
| 611 | (SELECT -9223372036854775807 AS x UNION ALL |
| 612 | SELECT -10 AS x); |
| 613 | } |
| 614 | } {1 {integer overflow}} |
| 615 | do_test func-18.19 { |
| 616 | catchsql { |
| 617 | SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x); |
| 618 | } |
| 619 | } {0 -1} |
| 620 | do_test func-18.20 { |
| 621 | catchsql { |
| 622 | SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x); |
| 623 | } |
| 624 | } {0 1} |
| 625 | do_test func-18.21 { |
| 626 | catchsql { |
| 627 | SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x); |
| 628 | } |
| 629 | } {0 -1} |
| 630 | do_test func-18.22 { |
| 631 | catchsql { |
| 632 | SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x); |
| 633 | } |
| 634 | } {0 1} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 635 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 636 | } ;# ifcapable compound&&subquery |
| 637 | |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 638 | # Integer overflow on abs() |
| 639 | # |
| 640 | do_test func-18.31 { |
| 641 | catchsql { |
| 642 | SELECT abs(-9223372036854775807); |
| 643 | } |
| 644 | } {0 9223372036854775807} |
| 645 | do_test func-18.32 { |
| 646 | catchsql { |
| 647 | SELECT abs(-9223372036854775807-1); |
| 648 | } |
| 649 | } {1 {integer overflow}} |
| 650 | |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 651 | # The MATCH function exists but is only a stub and always throws an error. |
| 652 | # |
| 653 | do_test func-19.1 { |
| 654 | execsql { |
| 655 | SELECT match(a,b) FROM t1 WHERE 0; |
| 656 | } |
| 657 | } {} |
| 658 | do_test func-19.2 { |
| 659 | catchsql { |
| 660 | SELECT 'abc' MATCH 'xyz'; |
| 661 | } |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame^] | 662 | } {1 {unable to use function MATCH in the requested context}} |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 663 | do_test func-19.3 { |
| 664 | catchsql { |
| 665 | SELECT 'abc' NOT MATCH 'xyz'; |
| 666 | } |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame^] | 667 | } {1 {unable to use function MATCH in the requested context}} |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 668 | do_test func-19.4 { |
| 669 | catchsql { |
| 670 | SELECT match(1,2,3); |
| 671 | } |
| 672 | } {1 {wrong number of arguments to function match()}} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 673 | |
drh | bdf67e0 | 2006-08-19 11:34:01 +0000 | [diff] [blame] | 674 | # Soundex tests. |
| 675 | # |
| 676 | if {![catch {db eval {SELECT soundex('hello')}}]} { |
| 677 | set i 0 |
| 678 | foreach {name sdx} { |
| 679 | euler E460 |
| 680 | EULER E460 |
| 681 | Euler E460 |
| 682 | ellery E460 |
| 683 | gauss G200 |
| 684 | ghosh G200 |
| 685 | hilbert H416 |
| 686 | Heilbronn H416 |
| 687 | knuth K530 |
| 688 | kant K530 |
| 689 | Lloyd L300 |
| 690 | LADD L300 |
| 691 | Lukasiewicz L222 |
| 692 | Lissajous L222 |
| 693 | A A000 |
| 694 | 12345 ?000 |
| 695 | } { |
| 696 | incr i |
| 697 | do_test func-20.$i { |
| 698 | execsql {SELECT soundex($name)} |
| 699 | } $sdx |
| 700 | } |
| 701 | } |
| 702 | |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 703 | finish_test |