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 | # |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame^] | 14 | # $Id: func.test,v 1.85 2008/07/31 01:47:11 shane 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} |
danielk1977 | 7de68a0 | 2007-05-07 16:58:02 +0000 | [diff] [blame] | 223 | ifcapable !icu { |
| 224 | do_test func-5.4 { |
| 225 | catchsql {SELECT upper(a,5) FROM t2} |
| 226 | } {1 {wrong number of arguments to function upper()}} |
| 227 | } |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 228 | do_test func-5.5 { |
| 229 | catchsql {SELECT upper(*) FROM t2} |
| 230 | } {1 {wrong number of arguments to function upper()}} |
| 231 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 232 | # Test the coalesce() and nullif() functions |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 233 | # |
| 234 | do_test func-6.1 { |
| 235 | execsql {SELECT coalesce(a,'xyz') FROM t2} |
| 236 | } {1 xyz 345 xyz 67890} |
| 237 | do_test func-6.2 { |
| 238 | execsql {SELECT coalesce(upper(a),'nil') FROM t2} |
| 239 | } {1 nil 345 nil 67890} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 240 | do_test func-6.3 { |
| 241 | execsql {SELECT coalesce(nullif(1,1),'nil')} |
| 242 | } {nil} |
| 243 | do_test func-6.4 { |
| 244 | execsql {SELECT coalesce(nullif(1,2),'nil')} |
| 245 | } {1} |
| 246 | do_test func-6.5 { |
| 247 | execsql {SELECT coalesce(nullif(1,NULL),'nil')} |
| 248 | } {1} |
| 249 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 250 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 251 | # Test the last_insert_rowid() function |
| 252 | # |
| 253 | do_test func-7.1 { |
| 254 | execsql {SELECT last_insert_rowid()} |
| 255 | } [db last_insert_rowid] |
| 256 | |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 257 | # Tests for aggregate functions and how they handle NULLs. |
| 258 | # |
| 259 | do_test func-8.1 { |
danielk1977 | 4489f9b | 2005-01-20 02:17:01 +0000 | [diff] [blame] | 260 | ifcapable explain { |
| 261 | execsql {EXPLAIN SELECT sum(a) FROM t2;} |
| 262 | } |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 263 | execsql { |
| 264 | SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2; |
| 265 | } |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 266 | } {68236 3 22745.33 1 67890 5} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 267 | do_test func-8.2 { |
| 268 | execsql { |
| 269 | SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2; |
| 270 | } |
| 271 | } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
danielk1977 | 53c0f74 | 2005-03-29 03:10:59 +0000 | [diff] [blame] | 272 | |
| 273 | ifcapable tempdb { |
| 274 | do_test func-8.3 { |
| 275 | execsql { |
| 276 | CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; |
| 277 | SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 278 | } |
| 279 | } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
| 280 | } else { |
| 281 | do_test func-8.3 { |
| 282 | execsql { |
| 283 | CREATE TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; |
| 284 | SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 285 | } |
| 286 | } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
| 287 | } |
danielk1977 | 3aeab9e | 2004-06-24 00:20:04 +0000 | [diff] [blame] | 288 | do_test func-8.4 { |
| 289 | execsql { |
| 290 | SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 291 | } |
| 292 | } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
drh | 85e9e22 | 2008-07-15 00:27:34 +0000 | [diff] [blame] | 293 | do_test func-8.5 { |
| 294 | execsql { |
| 295 | SELECT sum(x) FROM (SELECT '9223372036' || '854775807' AS x |
| 296 | UNION ALL SELECT -9223372036854775807) |
| 297 | } |
| 298 | } {0} |
| 299 | do_test func-8.6 { |
| 300 | execsql { |
drh | 06a0a81 | 2008-07-16 18:20:09 +0000 | [diff] [blame] | 301 | SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775807' AS x |
| 302 | UNION ALL SELECT -9223372036854775807) |
| 303 | } |
| 304 | } {integer} |
| 305 | do_test func-8.7 { |
| 306 | execsql { |
drh | 01859b0 | 2008-07-16 18:04:36 +0000 | [diff] [blame] | 307 | SELECT typeof(sum(x)) FROM (SELECT '9223372036' || '854775808' AS x |
drh | 85e9e22 | 2008-07-15 00:27:34 +0000 | [diff] [blame] | 308 | UNION ALL SELECT -9223372036854775807) |
| 309 | } |
drh | 01859b0 | 2008-07-16 18:04:36 +0000 | [diff] [blame] | 310 | } {real} |
drh | 06a0a81 | 2008-07-16 18:20:09 +0000 | [diff] [blame] | 311 | do_test func-8.8 { |
drh | 01859b0 | 2008-07-16 18:04:36 +0000 | [diff] [blame] | 312 | execsql { |
drh | 06a0a81 | 2008-07-16 18:20:09 +0000 | [diff] [blame] | 313 | SELECT sum(x)>0.0 FROM (SELECT '9223372036' || '854775808' AS x |
drh | 01859b0 | 2008-07-16 18:04:36 +0000 | [diff] [blame] | 314 | UNION ALL SELECT -9223372036850000000) |
| 315 | } |
drh | 06a0a81 | 2008-07-16 18:20:09 +0000 | [diff] [blame] | 316 | } {1} |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 317 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 318 | # How do you test the random() function in a meaningful, deterministic way? |
| 319 | # |
| 320 | do_test func-9.1 { |
| 321 | execsql { |
| 322 | SELECT random() is not null; |
| 323 | } |
| 324 | } {1} |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 325 | do_test func-9.2 { |
| 326 | execsql { |
| 327 | SELECT typeof(random()); |
| 328 | } |
| 329 | } {integer} |
| 330 | do_test func-9.3 { |
| 331 | execsql { |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 332 | SELECT randomblob(32) is not null; |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 333 | } |
| 334 | } {1} |
| 335 | do_test func-9.4 { |
| 336 | execsql { |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 337 | SELECT typeof(randomblob(32)); |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 338 | } |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 339 | } {blob} |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 340 | do_test func-9.5 { |
| 341 | execsql { |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 342 | SELECT length(randomblob(32)), length(randomblob(-5)), |
| 343 | length(randomblob(2000)) |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 344 | } |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 345 | } {32 1 2000} |
drh | 63cf66f | 2007-01-29 15:50:05 +0000 | [diff] [blame] | 346 | |
drh | 137c728 | 2007-01-29 17:58:28 +0000 | [diff] [blame] | 347 | # The "hex()" function was added in order to be able to render blobs |
| 348 | # generated by randomblob(). So this seems like a good place to test |
| 349 | # hex(). |
| 350 | # |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 351 | ifcapable bloblit { |
| 352 | do_test func-9.10 { |
| 353 | execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')} |
| 354 | } {00112233445566778899AABBCCDDEEFF} |
| 355 | } |
drh | 056c8f7 | 2007-05-15 18:35:21 +0000 | [diff] [blame] | 356 | set encoding [db one {PRAGMA encoding}] |
| 357 | if {$encoding=="UTF-16le"} { |
| 358 | do_test func-9.11-utf16le { |
| 359 | execsql {SELECT hex(replace('abcdefg','ef','12'))} |
| 360 | } {6100620063006400310032006700} |
| 361 | do_test func-9.12-utf16le { |
| 362 | execsql {SELECT hex(replace('abcdefg','','12'))} |
| 363 | } {{}} |
drh | 056c8f7 | 2007-05-15 18:35:21 +0000 | [diff] [blame] | 364 | do_test func-9.13-utf16le { |
| 365 | execsql {SELECT hex(replace('aabcdefg','a','aaa'))} |
| 366 | } {610061006100610061006100620063006400650066006700} |
| 367 | } elseif {$encoding=="UTF-8"} { |
| 368 | do_test func-9.11-utf8 { |
| 369 | execsql {SELECT hex(replace('abcdefg','ef','12'))} |
| 370 | } {61626364313267} |
| 371 | do_test func-9.12-utf8 { |
| 372 | execsql {SELECT hex(replace('abcdefg','','12'))} |
| 373 | } {{}} |
drh | 056c8f7 | 2007-05-15 18:35:21 +0000 | [diff] [blame] | 374 | do_test func-9.13-utf8 { |
| 375 | execsql {SELECT hex(replace('aabcdefg','a','aaa'))} |
| 376 | } {616161616161626364656667} |
| 377 | } |
| 378 | |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 379 | # Use the "sqlite_register_test_function" TCL command which is part of |
| 380 | # the text fixture in order to verify correct operation of some of |
| 381 | # the user-defined SQL function APIs that are not used by the built-in |
| 382 | # functions. |
| 383 | # |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 384 | set ::DB [sqlite3_connection_pointer db] |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 385 | sqlite_register_test_function $::DB testfunc |
| 386 | do_test func-10.1 { |
| 387 | catchsql { |
| 388 | SELECT testfunc(NULL,NULL); |
| 389 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 390 | } {1 {first argument should be one of: int int64 string double null value}} |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 391 | do_test func-10.2 { |
| 392 | execsql { |
| 393 | SELECT testfunc( |
| 394 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 395 | 'int', 1234 |
| 396 | ); |
| 397 | } |
| 398 | } {1234} |
| 399 | do_test func-10.3 { |
| 400 | execsql { |
| 401 | SELECT testfunc( |
| 402 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 403 | 'string', NULL |
| 404 | ); |
| 405 | } |
| 406 | } {{}} |
| 407 | do_test func-10.4 { |
| 408 | execsql { |
| 409 | SELECT testfunc( |
| 410 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 411 | 'double', 1.234 |
| 412 | ); |
| 413 | } |
| 414 | } {1.234} |
| 415 | do_test func-10.5 { |
| 416 | execsql { |
| 417 | SELECT testfunc( |
| 418 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 419 | 'int', 1234, |
| 420 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 421 | 'string', NULL, |
| 422 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 423 | 'double', 1.234, |
| 424 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 425 | 'int', 1234, |
| 426 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 427 | 'string', NULL, |
| 428 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 429 | 'double', 1.234 |
| 430 | ); |
| 431 | } |
| 432 | } {1.234} |
| 433 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 434 | # Test the built-in sqlite_version(*) SQL function. |
| 435 | # |
| 436 | do_test func-11.1 { |
| 437 | execsql { |
| 438 | SELECT sqlite_version(*); |
| 439 | } |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 440 | } [sqlite3 -version] |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 441 | |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 442 | # Test that destructors passed to sqlite3 by calls to sqlite3_result_text() |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 443 | # etc. are called. These tests use two special user-defined functions |
| 444 | # (implemented in func.c) only available in test builds. |
| 445 | # |
| 446 | # Function test_destructor() takes one argument and returns a copy of the |
| 447 | # text form of that argument. A destructor is associated with the return |
| 448 | # value. Function test_destructor_count() returns the number of outstanding |
| 449 | # destructor calls for values returned by test_destructor(). |
| 450 | # |
drh | da84ca8 | 2008-03-19 16:35:24 +0000 | [diff] [blame] | 451 | if {[db eval {PRAGMA encoding}]=="UTF-8"} { |
| 452 | do_test func-12.1-utf8 { |
| 453 | execsql { |
| 454 | SELECT test_destructor('hello world'), test_destructor_count(); |
| 455 | } |
| 456 | } {{hello world} 1} |
| 457 | } else { |
shane | 2a5fc4d | 2008-07-31 01:47:11 +0000 | [diff] [blame^] | 458 | ifcapable {utf16} { |
| 459 | do_test func-12.1-utf16 { |
| 460 | execsql { |
| 461 | SELECT test_destructor16('hello world'), test_destructor_count(); |
| 462 | } |
| 463 | } {{hello world} 1} |
drh | da84ca8 | 2008-03-19 16:35:24 +0000 | [diff] [blame] | 464 | } |
drh | da84ca8 | 2008-03-19 16:35:24 +0000 | [diff] [blame] | 465 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 466 | do_test func-12.2 { |
| 467 | execsql { |
| 468 | SELECT test_destructor_count(); |
| 469 | } |
| 470 | } {0} |
| 471 | do_test func-12.3 { |
| 472 | execsql { |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 473 | SELECT test_destructor('hello')||' world' |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 474 | } |
drh | 2dcef11 | 2008-01-12 19:03:48 +0000 | [diff] [blame] | 475 | } {{hello world}} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 476 | do_test func-12.4 { |
| 477 | execsql { |
| 478 | SELECT test_destructor_count(); |
| 479 | } |
| 480 | } {0} |
| 481 | do_test func-12.5 { |
| 482 | execsql { |
| 483 | CREATE TABLE t4(x); |
| 484 | INSERT INTO t4 VALUES(test_destructor('hello')); |
| 485 | INSERT INTO t4 VALUES(test_destructor('world')); |
| 486 | SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4; |
| 487 | } |
| 488 | } {hello world} |
| 489 | do_test func-12.6 { |
| 490 | execsql { |
| 491 | SELECT test_destructor_count(); |
| 492 | } |
| 493 | } {0} |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 494 | do_test func-12.7 { |
| 495 | execsql { |
| 496 | DROP TABLE t4; |
| 497 | } |
| 498 | } {} |
| 499 | |
drh | a4e5d58 | 2007-10-20 15:41:57 +0000 | [diff] [blame] | 500 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 501 | # Test that the auxdata API for scalar functions works. This test uses |
| 502 | # a special user-defined function only available in test builds, |
| 503 | # test_auxdata(). Function test_auxdata() takes any number of arguments. |
| 504 | do_test func-13.1 { |
| 505 | execsql { |
| 506 | SELECT test_auxdata('hello world'); |
| 507 | } |
| 508 | } {0} |
danielk1977 | ece80f1 | 2004-06-23 01:05:26 +0000 | [diff] [blame] | 509 | |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 510 | do_test func-13.2 { |
| 511 | execsql { |
| 512 | CREATE TABLE t4(a, b); |
| 513 | INSERT INTO t4 VALUES('abc', 'def'); |
| 514 | INSERT INTO t4 VALUES('ghi', 'jkl'); |
| 515 | } |
| 516 | } {} |
| 517 | do_test func-13.3 { |
| 518 | execsql { |
| 519 | SELECT test_auxdata('hello world') FROM t4; |
| 520 | } |
| 521 | } {0 1} |
| 522 | do_test func-13.4 { |
| 523 | execsql { |
| 524 | SELECT test_auxdata('hello world', 123) FROM t4; |
| 525 | } |
| 526 | } {{0 0} {1 1}} |
| 527 | do_test func-13.5 { |
| 528 | execsql { |
| 529 | SELECT test_auxdata('hello world', a) FROM t4; |
| 530 | } |
| 531 | } {{0 0} {1 0}} |
| 532 | do_test func-13.6 { |
| 533 | execsql { |
| 534 | SELECT test_auxdata('hello'||'world', a) FROM t4; |
| 535 | } |
| 536 | } {{0 0} {1 0}} |
| 537 | |
| 538 | # Test that auxilary data is preserved between calls for SQL variables. |
| 539 | do_test func-13.7 { |
drh | dddca28 | 2006-01-03 00:33:50 +0000 | [diff] [blame] | 540 | set DB [sqlite3_connection_pointer db] |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 541 | set sql "SELECT test_auxdata( ? , a ) FROM t4;" |
| 542 | set STMT [sqlite3_prepare $DB $sql -1 TAIL] |
drh | 10dfbbb | 2008-04-16 12:58:53 +0000 | [diff] [blame] | 543 | sqlite3_bind_text $STMT 1 hello\000 -1 |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame] | 544 | set res [list] |
| 545 | while { "SQLITE_ROW"==[sqlite3_step $STMT] } { |
| 546 | lappend res [sqlite3_column_text $STMT 0] |
| 547 | } |
| 548 | lappend res [sqlite3_finalize $STMT] |
| 549 | } {{0 0} {1 0} SQLITE_OK} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 550 | |
danielk1977 | 312d6b3 | 2004-06-29 13:18:23 +0000 | [diff] [blame] | 551 | # Make sure that a function with a very long name is rejected |
| 552 | do_test func-14.1 { |
| 553 | catch { |
| 554 | db function [string repeat X 254] {return "hello"} |
| 555 | } |
| 556 | } {0} |
| 557 | do_test func-14.2 { |
| 558 | catch { |
| 559 | db function [string repeat X 256] {return "hello"} |
| 560 | } |
| 561 | } {1} |
| 562 | |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 563 | do_test func-15.1 { |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 564 | catchsql {select test_error(NULL)} |
drh | 90669c1 | 2006-01-20 15:45:36 +0000 | [diff] [blame] | 565 | } {1 {}} |
drh | 00e087b | 2008-04-10 17:14:07 +0000 | [diff] [blame] | 566 | do_test func-15.2 { |
| 567 | catchsql {select test_error('this is the error message')} |
| 568 | } {1 {this is the error message}} |
| 569 | do_test func-15.3 { |
| 570 | catchsql {select test_error('this is the error message',12)} |
| 571 | } {1 {this is the error message}} |
| 572 | do_test func-15.4 { |
| 573 | db errorcode |
| 574 | } {12} |
danielk1977 | 01427a6 | 2005-01-11 13:02:33 +0000 | [diff] [blame] | 575 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 576 | # Test the quote function for BLOB and NULL values. |
| 577 | do_test func-16.1 { |
| 578 | execsql { |
| 579 | CREATE TABLE tbl2(a, b); |
| 580 | } |
| 581 | set STMT [sqlite3_prepare $::DB "INSERT INTO tbl2 VALUES(?, ?)" -1 TAIL] |
| 582 | sqlite3_bind_blob $::STMT 1 abc 3 |
| 583 | sqlite3_step $::STMT |
| 584 | sqlite3_finalize $::STMT |
| 585 | execsql { |
| 586 | SELECT quote(a), quote(b) FROM tbl2; |
| 587 | } |
| 588 | } {X'616263' NULL} |
| 589 | |
drh | 2501eb1 | 2005-08-12 23:20:53 +0000 | [diff] [blame] | 590 | # Correctly handle function error messages that include %. Ticket #1354 |
| 591 | # |
| 592 | do_test func-17.1 { |
| 593 | proc testfunc1 args {error "Error %d with %s percents %p"} |
| 594 | db function testfunc1 ::testfunc1 |
| 595 | catchsql { |
| 596 | SELECT testfunc1(1,2,3); |
| 597 | } |
| 598 | } {1 {Error %d with %s percents %p}} |
| 599 | |
drh | 3d1d95e | 2005-09-08 10:37:01 +0000 | [diff] [blame] | 600 | # The SUM function should return integer results when all inputs are integer. |
| 601 | # |
| 602 | do_test func-18.1 { |
| 603 | execsql { |
| 604 | CREATE TABLE t5(x); |
| 605 | INSERT INTO t5 VALUES(1); |
| 606 | INSERT INTO t5 VALUES(-99); |
| 607 | INSERT INTO t5 VALUES(10000); |
| 608 | SELECT sum(x) FROM t5; |
| 609 | } |
| 610 | } {9902} |
| 611 | do_test func-18.2 { |
| 612 | execsql { |
| 613 | INSERT INTO t5 VALUES(0.0); |
| 614 | SELECT sum(x) FROM t5; |
| 615 | } |
drh | 8a51256 | 2005-11-14 22:29:05 +0000 | [diff] [blame] | 616 | } {9902.0} |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 617 | |
drh | c2bd913 | 2005-09-08 20:37:43 +0000 | [diff] [blame] | 618 | # 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] | 619 | # |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 620 | # The TOTAL of nothing is 0.0. |
| 621 | # |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 622 | do_test func-18.3 { |
| 623 | execsql { |
| 624 | DELETE FROM t5; |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 625 | SELECT sum(x), total(x) FROM t5; |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 626 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 627 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 628 | do_test func-18.4 { |
| 629 | execsql { |
| 630 | INSERT INTO t5 VALUES(NULL); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 631 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 632 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 633 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 634 | do_test func-18.5 { |
| 635 | execsql { |
| 636 | INSERT INTO t5 VALUES(NULL); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 637 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 638 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 639 | } {{} 0.0} |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 640 | do_test func-18.6 { |
| 641 | execsql { |
| 642 | INSERT INTO t5 VALUES(123); |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 643 | SELECT sum(x), total(x) FROM t5 |
drh | 3f219f4 | 2005-09-08 19:45:57 +0000 | [diff] [blame] | 644 | } |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 645 | } {123 123.0} |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 646 | |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 647 | # Ticket #1664, #1669, #1670, #1674: An integer overflow on SUM causes |
| 648 | # an error. The non-standard TOTAL() function continues to give a helpful |
| 649 | # result. |
drh | fc6ad39 | 2006-02-09 13:38:19 +0000 | [diff] [blame] | 650 | # |
| 651 | do_test func-18.10 { |
| 652 | execsql { |
| 653 | CREATE TABLE t6(x INTEGER); |
| 654 | INSERT INTO t6 VALUES(1); |
| 655 | INSERT INTO t6 VALUES(1<<62); |
| 656 | SELECT sum(x) - ((1<<62)+1) from t6; |
| 657 | } |
| 658 | } 0 |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 659 | do_test func-18.11 { |
| 660 | execsql { |
| 661 | SELECT typeof(sum(x)) FROM t6 |
| 662 | } |
| 663 | } integer |
| 664 | do_test func-18.12 { |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 665 | catchsql { |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 666 | INSERT INTO t6 VALUES(1<<62); |
| 667 | SELECT sum(x) - ((1<<62)*2.0+1) from t6; |
| 668 | } |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 669 | } {1 {integer overflow}} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 670 | do_test func-18.13 { |
| 671 | execsql { |
| 672 | SELECT total(x) - ((1<<62)*2.0+1) FROM t6 |
| 673 | } |
| 674 | } 0.0 |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 675 | do_test func-18.14 { |
| 676 | execsql { |
| 677 | SELECT sum(-9223372036854775805); |
| 678 | } |
| 679 | } -9223372036854775805 |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 680 | |
| 681 | ifcapable compound&&subquery { |
| 682 | |
drh | 8c08e86 | 2006-02-11 17:34:00 +0000 | [diff] [blame] | 683 | do_test func-18.15 { |
| 684 | catchsql { |
| 685 | SELECT sum(x) FROM |
| 686 | (SELECT 9223372036854775807 AS x UNION ALL |
| 687 | SELECT 10 AS x); |
| 688 | } |
| 689 | } {1 {integer overflow}} |
| 690 | do_test func-18.16 { |
| 691 | catchsql { |
| 692 | SELECT sum(x) FROM |
| 693 | (SELECT 9223372036854775807 AS x UNION ALL |
| 694 | SELECT -10 AS x); |
| 695 | } |
| 696 | } {0 9223372036854775797} |
| 697 | do_test func-18.17 { |
| 698 | catchsql { |
| 699 | SELECT sum(x) FROM |
| 700 | (SELECT -9223372036854775807 AS x UNION ALL |
| 701 | SELECT 10 AS x); |
| 702 | } |
| 703 | } {0 -9223372036854775797} |
| 704 | do_test func-18.18 { |
| 705 | catchsql { |
| 706 | SELECT sum(x) FROM |
| 707 | (SELECT -9223372036854775807 AS x UNION ALL |
| 708 | SELECT -10 AS x); |
| 709 | } |
| 710 | } {1 {integer overflow}} |
| 711 | do_test func-18.19 { |
| 712 | catchsql { |
| 713 | SELECT sum(x) FROM (SELECT 9 AS x UNION ALL SELECT -10 AS x); |
| 714 | } |
| 715 | } {0 -1} |
| 716 | do_test func-18.20 { |
| 717 | catchsql { |
| 718 | SELECT sum(x) FROM (SELECT -9 AS x UNION ALL SELECT 10 AS x); |
| 719 | } |
| 720 | } {0 1} |
| 721 | do_test func-18.21 { |
| 722 | catchsql { |
| 723 | SELECT sum(x) FROM (SELECT -10 AS x UNION ALL SELECT 9 AS x); |
| 724 | } |
| 725 | } {0 -1} |
| 726 | do_test func-18.22 { |
| 727 | catchsql { |
| 728 | SELECT sum(x) FROM (SELECT 10 AS x UNION ALL SELECT -9 AS x); |
| 729 | } |
| 730 | } {0 1} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 731 | |
danielk1977 | 4b2688a | 2006-06-20 11:01:07 +0000 | [diff] [blame] | 732 | } ;# ifcapable compound&&subquery |
| 733 | |
drh | 52fc849 | 2006-02-23 21:43:55 +0000 | [diff] [blame] | 734 | # Integer overflow on abs() |
| 735 | # |
| 736 | do_test func-18.31 { |
| 737 | catchsql { |
| 738 | SELECT abs(-9223372036854775807); |
| 739 | } |
| 740 | } {0 9223372036854775807} |
| 741 | do_test func-18.32 { |
| 742 | catchsql { |
| 743 | SELECT abs(-9223372036854775807-1); |
| 744 | } |
| 745 | } {1 {integer overflow}} |
| 746 | |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 747 | # The MATCH function exists but is only a stub and always throws an error. |
| 748 | # |
| 749 | do_test func-19.1 { |
| 750 | execsql { |
| 751 | SELECT match(a,b) FROM t1 WHERE 0; |
| 752 | } |
| 753 | } {} |
| 754 | do_test func-19.2 { |
| 755 | catchsql { |
| 756 | SELECT 'abc' MATCH 'xyz'; |
| 757 | } |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 758 | } {1 {unable to use function MATCH in the requested context}} |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 759 | do_test func-19.3 { |
| 760 | catchsql { |
| 761 | SELECT 'abc' NOT MATCH 'xyz'; |
| 762 | } |
drh | b7481e7 | 2006-09-16 21:45:14 +0000 | [diff] [blame] | 763 | } {1 {unable to use function MATCH in the requested context}} |
drh | 7f37590 | 2006-06-13 17:38:59 +0000 | [diff] [blame] | 764 | do_test func-19.4 { |
| 765 | catchsql { |
| 766 | SELECT match(1,2,3); |
| 767 | } |
| 768 | } {1 {wrong number of arguments to function match()}} |
drh | 76c730c | 2006-02-09 17:47:42 +0000 | [diff] [blame] | 769 | |
drh | bdf67e0 | 2006-08-19 11:34:01 +0000 | [diff] [blame] | 770 | # Soundex tests. |
| 771 | # |
| 772 | if {![catch {db eval {SELECT soundex('hello')}}]} { |
| 773 | set i 0 |
| 774 | foreach {name sdx} { |
| 775 | euler E460 |
| 776 | EULER E460 |
| 777 | Euler E460 |
| 778 | ellery E460 |
| 779 | gauss G200 |
| 780 | ghosh G200 |
| 781 | hilbert H416 |
| 782 | Heilbronn H416 |
| 783 | knuth K530 |
| 784 | kant K530 |
| 785 | Lloyd L300 |
| 786 | LADD L300 |
| 787 | Lukasiewicz L222 |
| 788 | Lissajous L222 |
| 789 | A A000 |
| 790 | 12345 ?000 |
| 791 | } { |
| 792 | incr i |
| 793 | do_test func-20.$i { |
| 794 | execsql {SELECT soundex($name)} |
| 795 | } $sdx |
| 796 | } |
| 797 | } |
| 798 | |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 799 | # Tests of the REPLACE function. |
| 800 | # |
| 801 | do_test func-21.1 { |
| 802 | catchsql { |
| 803 | SELECT replace(1,2); |
| 804 | } |
| 805 | } {1 {wrong number of arguments to function replace()}} |
| 806 | do_test func-21.2 { |
| 807 | catchsql { |
| 808 | SELECT replace(1,2,3,4); |
| 809 | } |
| 810 | } {1 {wrong number of arguments to function replace()}} |
| 811 | do_test func-21.3 { |
| 812 | execsql { |
| 813 | SELECT typeof(replace("This is the main test string", NULL, "ALT")); |
| 814 | } |
| 815 | } {null} |
| 816 | do_test func-21.4 { |
| 817 | execsql { |
| 818 | SELECT typeof(replace(NULL, "main", "ALT")); |
| 819 | } |
| 820 | } {null} |
| 821 | do_test func-21.5 { |
| 822 | execsql { |
| 823 | SELECT typeof(replace("This is the main test string", "main", NULL)); |
| 824 | } |
| 825 | } {null} |
| 826 | do_test func-21.6 { |
| 827 | execsql { |
| 828 | SELECT replace("This is the main test string", "main", "ALT"); |
| 829 | } |
| 830 | } {{This is the ALT test string}} |
| 831 | do_test func-21.7 { |
| 832 | execsql { |
| 833 | SELECT replace("This is the main test string", "main", "larger-main"); |
| 834 | } |
| 835 | } {{This is the larger-main test string}} |
| 836 | do_test func-21.8 { |
| 837 | execsql { |
| 838 | SELECT replace("aaaaaaa", "a", "0123456789"); |
| 839 | } |
| 840 | } {0123456789012345678901234567890123456789012345678901234567890123456789} |
| 841 | |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 842 | ifcapable tclvar { |
| 843 | do_test func-21.9 { |
| 844 | # Attempt to exploit a buffer-overflow that at one time existed |
| 845 | # in the REPLACE function. |
| 846 | set ::str "[string repeat A 29998]CC[string repeat A 35537]" |
| 847 | set ::rep [string repeat B 65536] |
| 848 | execsql { |
| 849 | SELECT LENGTH(REPLACE($::str, 'C', $::rep)); |
| 850 | } |
| 851 | } [expr 29998 + 2*65536 + 35537] |
| 852 | } |
danielk1977 | 17374e8 | 2007-05-08 14:39:04 +0000 | [diff] [blame] | 853 | |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 854 | # Tests for the TRIM, LTRIM and RTRIM functions. |
| 855 | # |
| 856 | do_test func-22.1 { |
| 857 | catchsql {SELECT trim(1,2,3)} |
| 858 | } {1 {wrong number of arguments to function trim()}} |
| 859 | do_test func-22.2 { |
| 860 | catchsql {SELECT ltrim(1,2,3)} |
| 861 | } {1 {wrong number of arguments to function ltrim()}} |
| 862 | do_test func-22.3 { |
| 863 | catchsql {SELECT rtrim(1,2,3)} |
| 864 | } {1 {wrong number of arguments to function rtrim()}} |
| 865 | do_test func-22.4 { |
| 866 | execsql {SELECT trim(' hi ');} |
| 867 | } {hi} |
| 868 | do_test func-22.5 { |
| 869 | execsql {SELECT ltrim(' hi ');} |
| 870 | } {{hi }} |
| 871 | do_test func-22.6 { |
| 872 | execsql {SELECT rtrim(' hi ');} |
| 873 | } {{ hi}} |
| 874 | do_test func-22.7 { |
| 875 | execsql {SELECT trim(' hi ','xyz');} |
| 876 | } {{ hi }} |
| 877 | do_test func-22.8 { |
| 878 | execsql {SELECT ltrim(' hi ','xyz');} |
| 879 | } {{ hi }} |
| 880 | do_test func-22.9 { |
| 881 | execsql {SELECT rtrim(' hi ','xyz');} |
| 882 | } {{ hi }} |
| 883 | do_test func-22.10 { |
| 884 | execsql {SELECT trim('xyxzy hi zzzy','xyz');} |
| 885 | } {{ hi }} |
| 886 | do_test func-22.11 { |
| 887 | execsql {SELECT ltrim('xyxzy hi zzzy','xyz');} |
| 888 | } {{ hi zzzy}} |
| 889 | do_test func-22.12 { |
| 890 | execsql {SELECT rtrim('xyxzy hi zzzy','xyz');} |
| 891 | } {{xyxzy hi }} |
| 892 | do_test func-22.13 { |
| 893 | execsql {SELECT trim(' hi ','');} |
| 894 | } {{ hi }} |
drh | 4e05c83 | 2007-05-11 01:44:50 +0000 | [diff] [blame] | 895 | if {[db one {PRAGMA encoding}]=="UTF-8"} { |
| 896 | do_test func-22.14 { |
| 897 | execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))} |
| 898 | } {F48FBFBF6869} |
| 899 | do_test func-22.15 { |
| 900 | execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61', |
| 901 | x'6162e1bfbfc280f48fbfbf'))} |
| 902 | } {6869} |
| 903 | do_test func-22.16 { |
| 904 | execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));} |
| 905 | } {CEB2CEB3} |
| 906 | } |
drh | 309b338 | 2007-03-17 17:52:42 +0000 | [diff] [blame] | 907 | do_test func-22.20 { |
| 908 | execsql {SELECT typeof(trim(NULL));} |
| 909 | } {null} |
| 910 | do_test func-22.21 { |
| 911 | execsql {SELECT typeof(trim(NULL,'xyz'));} |
| 912 | } {null} |
| 913 | do_test func-22.22 { |
| 914 | execsql {SELECT typeof(trim('hello',NULL));} |
| 915 | } {null} |
drh | 26b6d90 | 2007-03-17 13:27:54 +0000 | [diff] [blame] | 916 | |
danielk1977 | fa18bec | 2007-09-03 11:04:22 +0000 | [diff] [blame] | 917 | # This is to test the deprecated sqlite3_aggregate_count() API. |
| 918 | # |
| 919 | do_test func-23.1 { |
| 920 | sqlite3_create_aggregate db |
| 921 | execsql { |
| 922 | SELECT legacy_count() FROM t6; |
| 923 | } |
| 924 | } {3} |
| 925 | |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 926 | # The group_concat() function. |
| 927 | # |
| 928 | do_test func-24.1 { |
| 929 | execsql { |
| 930 | SELECT group_concat(t1) FROM tbl1 |
| 931 | } |
| 932 | } {this,program,is,free,software} |
| 933 | do_test func-24.2 { |
| 934 | execsql { |
| 935 | SELECT group_concat(t1,' ') FROM tbl1 |
| 936 | } |
| 937 | } {{this program is free software}} |
| 938 | do_test func-24.3 { |
| 939 | execsql { |
| 940 | SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1 |
| 941 | } |
| 942 | } {{this 2 program 3 is 4 free 5 software}} |
| 943 | do_test func-24.4 { |
| 944 | execsql { |
| 945 | SELECT group_concat(NULL,t1) FROM tbl1 |
| 946 | } |
| 947 | } {{}} |
| 948 | do_test func-24.5 { |
| 949 | execsql { |
| 950 | SELECT group_concat(t1,NULL) FROM tbl1 |
| 951 | } |
| 952 | } {thisprogramisfreesoftware} |
drh | 2dca868 | 2008-03-21 17:13:13 +0000 | [diff] [blame] | 953 | do_test func-24.6 { |
| 954 | execsql { |
| 955 | SELECT 'BEGIN-'||group_concat(t1) FROM tbl1 |
| 956 | } |
| 957 | } {BEGIN-this,program,is,free,software} |
drh | 3780b5d | 2008-06-19 18:39:11 +0000 | [diff] [blame] | 958 | unset -nocomplain midargs |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 959 | set midargs {} |
drh | 3780b5d | 2008-06-19 18:39:11 +0000 | [diff] [blame] | 960 | unset -nocomplain midres |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 961 | set midres {} |
drh | 3780b5d | 2008-06-19 18:39:11 +0000 | [diff] [blame] | 962 | unset -nocomplain result |
drh | a2baf3a | 2008-06-18 15:34:09 +0000 | [diff] [blame] | 963 | for {set i 1} {$i<[sqlite3_limit db SQLITE_LIMIT_FUNCTION_ARG -1]-1} {incr i} { |
| 964 | append midargs ,'/$i' |
| 965 | append midres /$i |
| 966 | set result \ |
| 967 | "this$midres:program$midres:is$midres:free$midres:software$midres" |
| 968 | set sql "SELECT group_concat(t1$midargs,':') FROM tbl1" |
| 969 | do_test func-24.7.$i { |
| 970 | db eval $::sql |
| 971 | } $result |
| 972 | } |
drh | ade8648 | 2007-11-28 22:36:40 +0000 | [diff] [blame] | 973 | |
drh | 191b54c | 2008-04-15 12:14:21 +0000 | [diff] [blame] | 974 | # Use the test_isolation function to make sure that type conversions |
| 975 | # on function arguments do not effect subsequent arguments. |
| 976 | # |
| 977 | do_test func-25.1 { |
| 978 | execsql {SELECT test_isolation(t1,t1) FROM tbl1} |
| 979 | } {this program is free software} |
| 980 | |
drh | 24b58dd | 2008-07-07 14:50:14 +0000 | [diff] [blame] | 981 | # Try to misuse the sqlite3_create_function() interface. Verify that |
| 982 | # errors are returned. |
| 983 | # |
| 984 | do_test func-26.1 { |
| 985 | abuse_create_function db |
| 986 | } {} |
| 987 | |
| 988 | # The previous test (func-26.1) registered a function with a very long |
| 989 | # function name that takes many arguments and always returns NULL. Verify |
| 990 | # that this function works correctly. |
| 991 | # |
| 992 | do_test func-26.2 { |
| 993 | set a {} |
| 994 | for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG} {incr i} { |
| 995 | lappend a $i |
| 996 | } |
| 997 | db eval " |
| 998 | SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]); |
| 999 | " |
| 1000 | } {{}} |
| 1001 | do_test func-26.3 { |
| 1002 | set a {} |
| 1003 | for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG+1} {incr i} { |
| 1004 | lappend a $i |
| 1005 | } |
| 1006 | catchsql " |
| 1007 | SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]); |
| 1008 | " |
| 1009 | } {1 {too many arguments on function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789}} |
| 1010 | do_test func-26.4 { |
| 1011 | set a {} |
| 1012 | for {set i 1} {$i<=$::SQLITE_MAX_FUNCTION_ARG-1} {incr i} { |
| 1013 | lappend a $i |
| 1014 | } |
| 1015 | catchsql " |
| 1016 | SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789([join $a ,]); |
| 1017 | " |
| 1018 | } {1 {wrong number of arguments to function nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789()}} |
| 1019 | do_test func-26.5 { |
| 1020 | catchsql " |
| 1021 | SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a(0); |
| 1022 | " |
| 1023 | } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678a}} |
| 1024 | do_test func-26.6 { |
| 1025 | catchsql " |
| 1026 | SELECT nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a(0); |
| 1027 | " |
| 1028 | } {1 {no such function: nullx_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789a}} |
| 1029 | |
drh | 5708d2d | 2005-06-22 10:53:59 +0000 | [diff] [blame] | 1030 | finish_test |