drh | 89f1508 | 2012-06-19 00:45:16 +0000 | [diff] [blame] | 1 | # 2012 June 18 |
| 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 | # Tests of the sqlite3AtoF() function. |
| 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | |
drh | 2ab410a | 2015-11-06 14:59:07 +0000 | [diff] [blame] | 18 | if {$::longdouble_size<=8} { |
drh | c722a2c | 2012-06-21 15:02:26 +0000 | [diff] [blame] | 19 | finish_test |
| 20 | return |
| 21 | } |
| 22 | |
drh | 89f1508 | 2012-06-19 00:45:16 +0000 | [diff] [blame] | 23 | expr srand(1) |
drh | 4ef9413 | 2012-06-19 03:35:05 +0000 | [diff] [blame] | 24 | for {set i 1} {$i<20000} {incr i} { |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 25 | set pow [expr {int((rand()-0.5)*100)}] |
| 26 | set x [expr {pow((rand()-0.5)*2*rand(),$pow)}] |
| 27 | set xf [format %.32e $x] |
| 28 | |
| 29 | # Verify that text->real conversions get exactly same ieee754 floating- |
| 30 | # point value in SQLite as they do in TCL. |
| 31 | # |
drh | c722a2c | 2012-06-21 15:02:26 +0000 | [diff] [blame] | 32 | do_test atof1-1.$i.1 { |
drh | 89f1508 | 2012-06-19 00:45:16 +0000 | [diff] [blame] | 33 | set y [db eval "SELECT $xf=\$x"] |
| 34 | if {!$y} { |
| 35 | puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175 |
| 36 | db eval "SELECT $xf+0.0 AS a, \$x AS b" { |
| 37 | puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b] |
| 38 | } |
| 39 | } |
| 40 | set y |
| 41 | } {1} |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 42 | |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 43 | # Verify that round-trip real->text->real conversions using the quote() |
| 44 | # function preserve the bits of the numeric value exactly. |
| 45 | # |
drh | c722a2c | 2012-06-21 15:02:26 +0000 | [diff] [blame] | 46 | do_test atof1-1.$i.2 { |
drh | 72b3fbc | 2012-06-19 03:11:25 +0000 | [diff] [blame] | 47 | set y [db eval {SELECT $x=CAST(quote($x) AS real)}] |
| 48 | if {!$y} { |
| 49 | db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {} |
| 50 | puts "\nIN: $a $xf" |
| 51 | puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]] |
| 52 | db eval {SELECT CAST(quote($x) AS real) c} {} |
| 53 | puts "OUT: $b [format %.32e $c]" |
| 54 | } |
| 55 | set y |
| 56 | } {1} |
drh | 89f1508 | 2012-06-19 00:45:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
drh | 87969b2 | 2020-01-08 12:17:46 +0000 | [diff] [blame] | 59 | # 2020-01-08 ticket 9eda2697f5cc1aba |
| 60 | # When running sqlite3AtoF() on a blob with an odd number of bytes using |
| 61 | # UTF16, ignore the last byte so that the string has an integer number of |
| 62 | # UTF16 code points. |
| 63 | # |
| 64 | reset_db |
| 65 | do_execsql_test atof1-2.10 { |
| 66 | PRAGMA encoding = 'UTF16be'; |
| 67 | CREATE TABLE t1(a, b); |
| 68 | INSERT INTO t1(rowid,a) VALUES (1,x'00'),(2,3); |
| 69 | SELECT substr(a,',') is true FROM t1 ORDER BY rowid; |
| 70 | } {0 1} |
| 71 | do_execsql_test atof1-2.20 { |
| 72 | SELECT substr(a,',') is true FROM t1 ORDER BY rowid DESC; |
| 73 | } {1 0} |
| 74 | do_execsql_test atof1-2.30 { |
| 75 | CREATE INDEX i1 ON t1(a); |
| 76 | SELECT count(*) FROM t1 WHERE substr(a,','); |
| 77 | } {1} |
drh | 359941b | 2020-08-27 16:28:30 +0000 | [diff] [blame] | 78 | # 2020-08-27 OSSFuzz find related to the above. |
| 79 | do_execsql_test atof1-2.40 { |
| 80 | SELECT randomblob(0) - 1; |
| 81 | } {-1} |
drh | 87969b2 | 2020-01-08 12:17:46 +0000 | [diff] [blame] | 82 | |
drh | 89f1508 | 2012-06-19 00:45:16 +0000 | [diff] [blame] | 83 | |
| 84 | finish_test |