| OLD | NEW |
| (Empty) |
| 1 # 2008 April 28 | |
| 2 # | |
| 3 # The author disclaims copyright to this source code. In place of | |
| 4 # a legal notice, here is a blessing: | |
| 5 # | |
| 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. | |
| 9 # | |
| 10 #*********************************************************************** | |
| 11 # | |
| 12 # Ticket #3060 | |
| 13 # | |
| 14 # Make sure IEEE floating point NaN values are handled properly. | |
| 15 # SQLite should always convert NaN into NULL. | |
| 16 # | |
| 17 # Also verify that the decimal to IEEE754 binary conversion routines | |
| 18 # correctly generate 0.0, +Inf, and -Inf as appropriate for numbers | |
| 19 # out of range. | |
| 20 # | |
| 21 # $Id: nan.test,v 1.5 2008/09/18 11:30:13 danielk1977 Exp $ | |
| 22 # | |
| 23 | |
| 24 set testdir [file dirname $argv0] | |
| 25 source $testdir/tester.tcl | |
| 26 | |
| 27 do_test nan-1.1.1 { | |
| 28 db eval { | |
| 29 PRAGMA auto_vacuum=OFF; | |
| 30 PRAGMA page_size=1024; | |
| 31 CREATE TABLE t1(x FLOAT); | |
| 32 } | |
| 33 set ::STMT [sqlite3_prepare db "INSERT INTO t1 VALUES(?)" -1 TAIL] | |
| 34 sqlite3_bind_double $::STMT 1 NaN | |
| 35 sqlite3_step $::STMT | |
| 36 sqlite3_reset $::STMT | |
| 37 db eval {SELECT x, typeof(x) FROM t1} | |
| 38 } {{} null} | |
| 39 if {$tcl_platform(platform) != "symbian"} { | |
| 40 do_test nan-1.1.2 { | |
| 41 sqlite3_bind_double $::STMT 1 +Inf | |
| 42 sqlite3_step $::STMT | |
| 43 sqlite3_reset $::STMT | |
| 44 db eval {SELECT x, typeof(x) FROM t1} | |
| 45 } {{} null inf real} | |
| 46 do_test nan-1.1.3 { | |
| 47 sqlite3_bind_double $::STMT 1 -Inf | |
| 48 sqlite3_step $::STMT | |
| 49 sqlite3_reset $::STMT | |
| 50 db eval {SELECT x, typeof(x) FROM t1} | |
| 51 } {{} null inf real -inf real} | |
| 52 do_test nan-1.1.4 { | |
| 53 sqlite3_bind_double $::STMT 1 -NaN | |
| 54 sqlite3_step $::STMT | |
| 55 sqlite3_reset $::STMT | |
| 56 db eval {SELECT x, typeof(x) FROM t1} | |
| 57 } {{} null inf real -inf real {} null} | |
| 58 do_test nan-1.1.5 { | |
| 59 sqlite3_bind_double $::STMT 1 NaN0 | |
| 60 sqlite3_step $::STMT | |
| 61 sqlite3_reset $::STMT | |
| 62 db eval {SELECT x, typeof(x) FROM t1} | |
| 63 } {{} null inf real -inf real {} null {} null} | |
| 64 do_test nan-1.1.5 { | |
| 65 sqlite3_bind_double $::STMT 1 -NaN0 | |
| 66 sqlite3_step $::STMT | |
| 67 sqlite3_reset $::STMT | |
| 68 db eval {SELECT x, typeof(x) FROM t1} | |
| 69 } {{} null inf real -inf real {} null {} null {} null} | |
| 70 do_test nan-1.1.6 { | |
| 71 db eval { | |
| 72 UPDATE t1 SET x=x-x; | |
| 73 SELECT x, typeof(x) FROM t1; | |
| 74 } | |
| 75 } {{} null {} null {} null {} null {} null {} null} | |
| 76 } | |
| 77 | |
| 78 # The following block of tests, nan-1.2.*, are the same as the nan-1.1.* | |
| 79 # tests above, except that the SELECT queries used to validate data | |
| 80 # convert floating point values to text internally before returning them | |
| 81 # to Tcl. This allows the tests to be run on platforms where Tcl has | |
| 82 # problems converting "inf" and "-inf" from floating point to text format. | |
| 83 # It also tests the internal float->text conversion routines a bit. | |
| 84 # | |
| 85 do_test nan-1.2.1 { | |
| 86 db eval { | |
| 87 DELETE FROM T1; | |
| 88 } | |
| 89 sqlite3_bind_double $::STMT 1 NaN | |
| 90 sqlite3_step $::STMT | |
| 91 sqlite3_reset $::STMT | |
| 92 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 93 } {{} null} | |
| 94 do_test nan-1.2.2 { | |
| 95 sqlite3_bind_double $::STMT 1 +Inf | |
| 96 sqlite3_step $::STMT | |
| 97 sqlite3_reset $::STMT | |
| 98 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 99 } {{} null Inf real} | |
| 100 do_test nan-1.2.3 { | |
| 101 sqlite3_bind_double $::STMT 1 -Inf | |
| 102 sqlite3_step $::STMT | |
| 103 sqlite3_reset $::STMT | |
| 104 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 105 } {{} null Inf real -Inf real} | |
| 106 do_test nan-1.2.4 { | |
| 107 sqlite3_bind_double $::STMT 1 -NaN | |
| 108 sqlite3_step $::STMT | |
| 109 sqlite3_reset $::STMT | |
| 110 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 111 } {{} null Inf real -Inf real {} null} | |
| 112 do_test nan-1.2.5 { | |
| 113 sqlite3_bind_double $::STMT 1 NaN0 | |
| 114 sqlite3_step $::STMT | |
| 115 sqlite3_reset $::STMT | |
| 116 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 117 } {{} null Inf real -Inf real {} null {} null} | |
| 118 do_test nan-1.2.5 { | |
| 119 sqlite3_bind_double $::STMT 1 -NaN0 | |
| 120 sqlite3_step $::STMT | |
| 121 sqlite3_reset $::STMT | |
| 122 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 123 } {{} null Inf real -Inf real {} null {} null {} null} | |
| 124 do_test nan-1.2.6 { | |
| 125 db eval { | |
| 126 UPDATE t1 SET x=x-x; | |
| 127 SELECT CAST(x AS text), typeof(x) FROM t1; | |
| 128 } | |
| 129 } {{} null {} null {} null {} null {} null {} null} | |
| 130 | |
| 131 do_test nan-2.1 { | |
| 132 db eval { | |
| 133 DELETE FROM T1; | |
| 134 } | |
| 135 sqlite3_bind_double $::STMT 1 NaN | |
| 136 sqlite3_step $::STMT | |
| 137 sqlite3_reset $::STMT | |
| 138 db eval {SELECT x, typeof(x) FROM t1} | |
| 139 } {{} null} | |
| 140 sqlite3_finalize $::STMT | |
| 141 | |
| 142 # SQLite always converts NaN into NULL so it is not possible to write | |
| 143 # a NaN value into the database file using SQLite. The following series | |
| 144 # of tests writes a normal floating point value (0.5) into the database, | |
| 145 # then writes directly into the database file to change the 0.5 into NaN. | |
| 146 # Then it reads the value of the database to verify it is converted into | |
| 147 # NULL. | |
| 148 # | |
| 149 do_test nan-3.1 { | |
| 150 db eval { | |
| 151 DELETE FROM t1; | |
| 152 INSERT INTO t1 VALUES(0.5); | |
| 153 PRAGMA auto_vacuum=OFF; | |
| 154 PRAGMA page_size=1024; | |
| 155 VACUUM; | |
| 156 } | |
| 157 hexio_read test.db 2040 8 | |
| 158 } {3FE0000000000000} | |
| 159 do_test nan-3.2 { | |
| 160 db eval { | |
| 161 SELECT x, typeof(x) FROM t1 | |
| 162 } | |
| 163 } {0.5 real} | |
| 164 do_test nan-3.3 { | |
| 165 db close | |
| 166 hexio_write test.db 2040 FFF8000000000000 | |
| 167 sqlite3 db test.db | |
| 168 db eval {SELECT x, typeof(x) FROM t1} | |
| 169 } {{} null} | |
| 170 do_test nan-3.4 { | |
| 171 db close | |
| 172 hexio_write test.db 2040 7FF8000000000000 | |
| 173 sqlite3 db test.db | |
| 174 db eval {SELECT x, typeof(x) FROM t1} | |
| 175 } {{} null} | |
| 176 do_test nan-3.5 { | |
| 177 db close | |
| 178 hexio_write test.db 2040 FFFFFFFFFFFFFFFF | |
| 179 sqlite3 db test.db | |
| 180 db eval {SELECT x, typeof(x) FROM t1} | |
| 181 } {{} null} | |
| 182 do_test nan-3.6 { | |
| 183 db close | |
| 184 hexio_write test.db 2040 7FFFFFFFFFFFFFFF | |
| 185 sqlite3 db test.db | |
| 186 db eval {SELECT x, typeof(x) FROM t1} | |
| 187 } {{} null} | |
| 188 | |
| 189 # Verify that the sqlite3AtoF routine is able to handle extreme | |
| 190 # numbers. | |
| 191 # | |
| 192 do_test nan-4.1 { | |
| 193 db eval {DELETE FROM t1} | |
| 194 db eval "INSERT INTO t1 VALUES([string repeat 9 307].0)" | |
| 195 db eval {SELECT x, typeof(x) FROM t1} | |
| 196 } {1e+307 real} | |
| 197 do_test nan-4.2 { | |
| 198 db eval {DELETE FROM t1} | |
| 199 db eval "INSERT INTO t1 VALUES([string repeat 9 308].0)" | |
| 200 db eval {SELECT x, typeof(x) FROM t1} | |
| 201 } {1e+308 real} | |
| 202 do_test nan-4.3 { | |
| 203 db eval {DELETE FROM t1} | |
| 204 db eval "INSERT INTO t1 VALUES(-[string repeat 9 307].0)" | |
| 205 db eval {SELECT x, typeof(x) FROM t1} | |
| 206 } {-1e+307 real} | |
| 207 do_test nan-4.4 { | |
| 208 db eval {DELETE FROM t1} | |
| 209 db eval "INSERT INTO t1 VALUES(-[string repeat 9 308].0)" | |
| 210 db eval {SELECT x, typeof(x) FROM t1} | |
| 211 } {-1e+308 real} | |
| 212 do_test nan-4.5 { | |
| 213 db eval {DELETE FROM t1} | |
| 214 set big -[string repeat 0 10000][string repeat 9 308].[string repeat 0 10000] | |
| 215 db eval "INSERT INTO t1 VALUES($big)" | |
| 216 db eval {SELECT x, typeof(x) FROM t1} | |
| 217 } {-1e+308 real} | |
| 218 do_test nan-4.6 { | |
| 219 db eval {DELETE FROM t1} | |
| 220 set big [string repeat 0 10000][string repeat 9 308].[string repeat 0 10000] | |
| 221 db eval "INSERT INTO t1 VALUES($big)" | |
| 222 db eval {SELECT x, typeof(x) FROM t1} | |
| 223 } {1e+308 real} | |
| 224 | |
| 225 if {$tcl_platform(platform) != "symbian"} { | |
| 226 # Do not run these tests on Symbian, as the Tcl port doesn't like to | |
| 227 # convert from floating point value "-inf" to a string. | |
| 228 # | |
| 229 do_test nan-4.7 { | |
| 230 db eval {DELETE FROM t1} | |
| 231 db eval "INSERT INTO t1 VALUES([string repeat 9 309].0)" | |
| 232 db eval {SELECT x, typeof(x) FROM t1} | |
| 233 } {inf real} | |
| 234 do_test nan-4.8 { | |
| 235 db eval {DELETE FROM t1} | |
| 236 db eval "INSERT INTO t1 VALUES(-[string repeat 9 309].0)" | |
| 237 db eval {SELECT x, typeof(x) FROM t1} | |
| 238 } {-inf real} | |
| 239 } | |
| 240 do_test nan-4.9 { | |
| 241 db eval {DELETE FROM t1} | |
| 242 db eval "INSERT INTO t1 VALUES([string repeat 9 309].0)" | |
| 243 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 244 } {Inf real} | |
| 245 do_test nan-4.10 { | |
| 246 db eval {DELETE FROM t1} | |
| 247 db eval "INSERT INTO t1 VALUES(-[string repeat 9 309].0)" | |
| 248 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 249 } {-Inf real} | |
| 250 | |
| 251 do_test nan-4.10 { | |
| 252 db eval {DELETE FROM t1} | |
| 253 db eval "INSERT INTO t1 VALUES(1234.5[string repeat 0 10000]12345)" | |
| 254 db eval {SELECT x, typeof(x) FROM t1} | |
| 255 } {1234.5 real} | |
| 256 do_test nan-4.11 { | |
| 257 db eval {DELETE FROM t1} | |
| 258 db eval "INSERT INTO t1 VALUES(-1234.5[string repeat 0 10000]12345)" | |
| 259 db eval {SELECT x, typeof(x) FROM t1} | |
| 260 } {-1234.5 real} | |
| 261 do_test nan-4.12 { | |
| 262 db eval {DELETE FROM t1} | |
| 263 set small [string repeat 0 10000].[string repeat 0 324][string repeat 9 10000] | |
| 264 db eval "INSERT INTO t1 VALUES($small)" | |
| 265 db eval {SELECT x, typeof(x) FROM t1} | |
| 266 } {0.0 real} | |
| 267 do_test nan-4.13 { | |
| 268 db eval {DELETE FROM t1} | |
| 269 set small \ | |
| 270 -[string repeat 0 10000].[string repeat 0 324][string repeat 9 10000] | |
| 271 db eval "INSERT INTO t1 VALUES($small)" | |
| 272 db eval {SELECT x, typeof(x) FROM t1} | |
| 273 } {0.0 real} | |
| 274 | |
| 275 # These tests test some really, really small floating point numbers. | |
| 276 # | |
| 277 if {$tcl_platform(platform) != "symbian"} { | |
| 278 # These two are not run on symbian because tcl has trouble converting | |
| 279 # the very small numbers back to text form (probably due to a difference | |
| 280 # in the sprintf() implementation). | |
| 281 # | |
| 282 do_test nan-4.14 { | |
| 283 db eval {DELETE FROM t1} | |
| 284 set small \ | |
| 285 [string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] | |
| 286 db eval "INSERT INTO t1 VALUES($small)" | |
| 287 db eval {SELECT x, typeof(x) FROM t1} | |
| 288 } {9.88131291682493e-324 real} | |
| 289 do_test nan-4.15 { | |
| 290 db eval {DELETE FROM t1} | |
| 291 set small \ | |
| 292 -[string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] | |
| 293 db eval "INSERT INTO t1 VALUES($small)" | |
| 294 db eval {SELECT x, typeof(x) FROM t1} | |
| 295 } {-9.88131291682493e-324 real} | |
| 296 } | |
| 297 do_test nan-4.16 { | |
| 298 db eval {DELETE FROM t1} | |
| 299 set small [string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] | |
| 300 db eval "INSERT INTO t1 VALUES($small)" | |
| 301 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 302 } {9.88131291682493e-324 real} | |
| 303 do_test nan-4.17 { | |
| 304 db eval {DELETE FROM t1} | |
| 305 set small \ | |
| 306 -[string repeat 0 10000].[string repeat 0 323][string repeat 9 10000] | |
| 307 db eval "INSERT INTO t1 VALUES($small)" | |
| 308 db eval {SELECT CAST(x AS text), typeof(x) FROM t1} | |
| 309 } {-9.88131291682493e-324 real} | |
| 310 | |
| 311 do_test nan-4.20 { | |
| 312 db eval {DELETE FROM t1} | |
| 313 set big [string repeat 9 10000].0e-9000 | |
| 314 db eval "INSERT INTO t1 VALUES($big)" | |
| 315 db eval {SELECT x, typeof(x) FROM t1} | |
| 316 } {inf real} | |
| 317 | |
| 318 | |
| 319 | |
| 320 finish_test | |
| OLD | NEW |