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 | # |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame^] | 14 | # $Id: func.test,v 1.22 2004/06/17 05:36:45 danielk1977 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} |
danielk1977 | f93bbbe | 2004-05-27 10:30:52 +0000 | [diff] [blame] | 175 | } {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} |
| 182 | } {0 {-2.00 1.23 2.00}} |
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} |
| 185 | } {0 {2 1 -2}} |
| 186 | do_test func-4.8 { |
| 187 | catchsql {SELECT round(c) FROM t1 ORDER BY a} |
| 188 | } {0 {3 -12346 -5}} |
| 189 | do_test func-4.9 { |
| 190 | catchsql {SELECT round(c,a) FROM t1 ORDER BY a} |
| 191 | } {0 {3.0 -12345.68 -5.000}} |
| 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} |
| 194 | } {0 {x3.0y x-12345.68y x-5.000y}} |
| 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} |
| 200 | } {1.00 nil 345.00 nil 67890.00} |
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} |
| 203 | } {0.00 0.00 0.00 0.00 0.00} |
| 204 | |
| 205 | # Test the upper() and lower() functions |
| 206 | # |
| 207 | do_test func-5.1 { |
| 208 | execsql {SELECT upper(t1) FROM tbl1} |
| 209 | } {THIS PROGRAM IS FREE SOFTWARE} |
| 210 | do_test func-5.2 { |
| 211 | execsql {SELECT lower(upper(t1)) FROM tbl1} |
| 212 | } {this program is free software} |
| 213 | do_test func-5.3 { |
| 214 | execsql {SELECT upper(a), lower(a) FROM t2} |
| 215 | } {1 1 {} {} 345 345 {} {} 67890 67890} |
| 216 | do_test func-5.4 { |
| 217 | catchsql {SELECT upper(a,5) FROM t2} |
| 218 | } {1 {wrong number of arguments to function upper()}} |
| 219 | do_test func-5.5 { |
| 220 | catchsql {SELECT upper(*) FROM t2} |
| 221 | } {1 {wrong number of arguments to function upper()}} |
| 222 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 223 | # Test the coalesce() and nullif() functions |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 224 | # |
| 225 | do_test func-6.1 { |
| 226 | execsql {SELECT coalesce(a,'xyz') FROM t2} |
| 227 | } {1 xyz 345 xyz 67890} |
| 228 | do_test func-6.2 { |
| 229 | execsql {SELECT coalesce(upper(a),'nil') FROM t2} |
| 230 | } {1 nil 345 nil 67890} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 231 | do_test func-6.3 { |
| 232 | execsql {SELECT coalesce(nullif(1,1),'nil')} |
| 233 | } {nil} |
| 234 | do_test func-6.4 { |
| 235 | execsql {SELECT coalesce(nullif(1,2),'nil')} |
| 236 | } {1} |
| 237 | do_test func-6.5 { |
| 238 | execsql {SELECT coalesce(nullif(1,NULL),'nil')} |
| 239 | } {1} |
| 240 | |
drh | 832508b | 2002-03-02 17:04:07 +0000 | [diff] [blame] | 241 | |
drh | 6ed41ad | 2002-04-06 14:10:47 +0000 | [diff] [blame] | 242 | # Test the last_insert_rowid() function |
| 243 | # |
| 244 | do_test func-7.1 { |
| 245 | execsql {SELECT last_insert_rowid()} |
| 246 | } [db last_insert_rowid] |
| 247 | |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 248 | # Tests for aggregate functions and how they handle NULLs. |
| 249 | # |
| 250 | do_test func-8.1 { |
| 251 | execsql { |
| 252 | SELECT sum(a), count(a), round(avg(a),2), min(a), max(a), count(*) FROM t2; |
| 253 | } |
| 254 | } {68236 3 22745.33 1 67890 5} |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 255 | do_test func-8.2 { |
| 256 | execsql { |
| 257 | SELECT max('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t2; |
| 258 | } |
| 259 | } {z+67890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
| 260 | do_test func-8.3 { |
| 261 | execsql { |
| 262 | CREATE TEMP TABLE t3 AS SELECT a FROM t2 ORDER BY a DESC; |
| 263 | SELECT min('z+'||a||'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP') FROM t3; |
| 264 | } |
| 265 | } {z+1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP} |
drh | 739105c | 2002-05-29 23:22:23 +0000 | [diff] [blame] | 266 | |
drh | a9f9d1c | 2002-06-29 02:20:08 +0000 | [diff] [blame] | 267 | # How do you test the random() function in a meaningful, deterministic way? |
| 268 | # |
| 269 | do_test func-9.1 { |
| 270 | execsql { |
| 271 | SELECT random() is not null; |
| 272 | } |
| 273 | } {1} |
drh | bf4133c | 2001-10-13 02:59:08 +0000 | [diff] [blame] | 274 | |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 275 | # Use the "sqlite_register_test_function" TCL command which is part of |
| 276 | # the text fixture in order to verify correct operation of some of |
| 277 | # the user-defined SQL function APIs that are not used by the built-in |
| 278 | # functions. |
| 279 | # |
| 280 | db close |
| 281 | set ::DB [sqlite db test.db] |
| 282 | sqlite_register_test_function $::DB testfunc |
| 283 | do_test func-10.1 { |
| 284 | catchsql { |
| 285 | SELECT testfunc(NULL,NULL); |
| 286 | } |
danielk1977 | 6d88bad | 2004-05-27 14:23:36 +0000 | [diff] [blame] | 287 | } {1 {first argument should be one of: int int64 string double null value}} |
drh | 6cbe1f1 | 2002-07-01 00:31:36 +0000 | [diff] [blame] | 288 | do_test func-10.2 { |
| 289 | execsql { |
| 290 | SELECT testfunc( |
| 291 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 292 | 'int', 1234 |
| 293 | ); |
| 294 | } |
| 295 | } {1234} |
| 296 | do_test func-10.3 { |
| 297 | execsql { |
| 298 | SELECT testfunc( |
| 299 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 300 | 'string', NULL |
| 301 | ); |
| 302 | } |
| 303 | } {{}} |
| 304 | do_test func-10.4 { |
| 305 | execsql { |
| 306 | SELECT testfunc( |
| 307 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 308 | 'double', 1.234 |
| 309 | ); |
| 310 | } |
| 311 | } {1.234} |
| 312 | do_test func-10.5 { |
| 313 | execsql { |
| 314 | SELECT testfunc( |
| 315 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 316 | 'int', 1234, |
| 317 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 318 | 'string', NULL, |
| 319 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 320 | 'double', 1.234, |
| 321 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 322 | 'int', 1234, |
| 323 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 324 | 'string', NULL, |
| 325 | 'string', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', |
| 326 | 'double', 1.234 |
| 327 | ); |
| 328 | } |
| 329 | } {1.234} |
| 330 | |
drh | 647cb0e | 2002-11-04 19:32:25 +0000 | [diff] [blame] | 331 | # Test the built-in sqlite_version(*) SQL function. |
| 332 | # |
| 333 | do_test func-11.1 { |
| 334 | execsql { |
| 335 | SELECT sqlite_version(*); |
| 336 | } |
| 337 | } [sqlite -version] |
| 338 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 339 | # Test that destructors passed to sqlite by calls to sqlite3_result_text() |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame^] | 340 | # etc. are called. These tests use two special user-defined functions |
| 341 | # (implemented in func.c) only available in test builds. |
| 342 | # |
| 343 | # Function test_destructor() takes one argument and returns a copy of the |
| 344 | # text form of that argument. A destructor is associated with the return |
| 345 | # value. Function test_destructor_count() returns the number of outstanding |
| 346 | # destructor calls for values returned by test_destructor(). |
| 347 | # |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 348 | do_test func-12.1 { |
| 349 | execsql { |
| 350 | SELECT test_destructor('hello world'), test_destructor_count(); |
| 351 | } |
| 352 | } {{hello world} 1} |
| 353 | do_test func-12.2 { |
| 354 | execsql { |
| 355 | SELECT test_destructor_count(); |
| 356 | } |
| 357 | } {0} |
| 358 | do_test func-12.3 { |
| 359 | execsql { |
| 360 | SELECT test_destructor('hello')||' world', test_destructor_count(); |
| 361 | } |
| 362 | } {{hello world} 0} |
| 363 | do_test func-12.4 { |
| 364 | execsql { |
| 365 | SELECT test_destructor_count(); |
| 366 | } |
| 367 | } {0} |
| 368 | do_test func-12.5 { |
| 369 | execsql { |
| 370 | CREATE TABLE t4(x); |
| 371 | INSERT INTO t4 VALUES(test_destructor('hello')); |
| 372 | INSERT INTO t4 VALUES(test_destructor('world')); |
| 373 | SELECT min(test_destructor(x)), max(test_destructor(x)) FROM t4; |
| 374 | } |
| 375 | } {hello world} |
| 376 | do_test func-12.6 { |
| 377 | execsql { |
| 378 | SELECT test_destructor_count(); |
| 379 | } |
| 380 | } {0} |
danielk1977 | 3f6b087 | 2004-06-17 05:36:44 +0000 | [diff] [blame^] | 381 | do_test func-12.7 { |
| 382 | execsql { |
| 383 | DROP TABLE t4; |
| 384 | } |
| 385 | } {} |
| 386 | |
| 387 | # Test that the auxdata API for scalar functions works. This test uses |
| 388 | # a special user-defined function only available in test builds, |
| 389 | # test_auxdata(). Function test_auxdata() takes any number of arguments. |
| 390 | do_test func-13.1 { |
| 391 | execsql { |
| 392 | SELECT test_auxdata('hello world'); |
| 393 | } |
| 394 | } {0} |
| 395 | do_test func-13.2 { |
| 396 | execsql { |
| 397 | CREATE TABLE t4(a, b); |
| 398 | INSERT INTO t4 VALUES('abc', 'def'); |
| 399 | INSERT INTO t4 VALUES('ghi', 'jkl'); |
| 400 | } |
| 401 | } {} |
| 402 | do_test func-13.3 { |
| 403 | execsql { |
| 404 | SELECT test_auxdata('hello world') FROM t4; |
| 405 | } |
| 406 | } {0 1} |
| 407 | do_test func-13.4 { |
| 408 | execsql { |
| 409 | SELECT test_auxdata('hello world', 123) FROM t4; |
| 410 | } |
| 411 | } {{0 0} {1 1}} |
| 412 | do_test func-13.5 { |
| 413 | execsql { |
| 414 | SELECT test_auxdata('hello world', a) FROM t4; |
| 415 | } |
| 416 | } {{0 0} {1 0}} |
| 417 | do_test func-13.6 { |
| 418 | execsql { |
| 419 | SELECT test_auxdata('hello'||'world', a) FROM t4; |
| 420 | } |
| 421 | } {{0 0} {1 0}} |
| 422 | |
| 423 | # Test that auxilary data is preserved between calls for SQL variables. |
| 424 | do_test func-13.7 { |
| 425 | db close |
| 426 | set DB [sqlite db test.db] |
| 427 | set sql "SELECT test_auxdata( ? , a ) FROM t4;" |
| 428 | set STMT [sqlite3_prepare $DB $sql -1 TAIL] |
| 429 | sqlite3_bind_text $STMT 1 hello -1 |
| 430 | set res [list] |
| 431 | while { "SQLITE_ROW"==[sqlite3_step $STMT] } { |
| 432 | lappend res [sqlite3_column_text $STMT 0] |
| 433 | } |
| 434 | lappend res [sqlite3_finalize $STMT] |
| 435 | } {{0 0} {1 0} SQLITE_OK} |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 436 | |
| 437 | |
drh | ff6e911 | 2000-08-28 16:21:58 +0000 | [diff] [blame] | 438 | finish_test |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 439 | |
| 440 | |
| 441 | |