sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 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 | |
| 18 | set mxpow 35 |
| 19 | expr srand(1) |
| 20 | for {set i 1} {$i<20000} {incr i} { |
| 21 | set pow [expr {int((rand()-0.5)*$mxpow)}] |
| 22 | set x [expr {pow((rand()-0.5)*2*rand(),$pow)}] |
| 23 | set xf [format %.32e $x] |
| 24 | |
| 25 | # Verify that text->real conversions get exactly same ieee754 floating- |
| 26 | # point value in SQLite as they do in TCL. |
| 27 | # |
| 28 | do_test atof1-1.$i.1 { |
| 29 | set y [db eval "SELECT $xf=\$x"] |
| 30 | if {!$y} { |
| 31 | puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175 |
| 32 | db eval "SELECT $xf+0.0 AS a, \$x AS b" { |
| 33 | puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b] |
| 34 | } |
| 35 | } |
| 36 | set y |
| 37 | } {1} |
| 38 | |
| 39 | # Verify that round-trip real->text->real conversions using the quote() |
| 40 | # function preserve the bits of the numeric value exactly. |
| 41 | # |
| 42 | do_test atof1-1.$i.2 { |
| 43 | set y [db eval {SELECT $x=CAST(quote($x) AS real)}] |
| 44 | if {!$y} { |
| 45 | db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {} |
| 46 | puts "" |
| 47 | if {$x<0} { |
| 48 | puts "[format {!SCALE: %17s 1 23456789 123456789 123456789} {}]" |
| 49 | } else { |
| 50 | puts "[format {!SCALE: %16s 1 23456789 123456789 123456789} {}]" |
| 51 | } |
| 52 | puts "!IN: $a $xf" |
| 53 | puts [format {!QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]] |
| 54 | db eval {SELECT CAST(quote($x) AS real) c} {} |
| 55 | puts "!OUT: $b [format %.32e $c]" |
| 56 | } |
| 57 | set y |
| 58 | } {1} |
| 59 | } |
| 60 | |
| 61 | # 2020-01-08 ticket 9eda2697f5cc1aba |
| 62 | # When running sqlite3AtoF() on a blob with an odd number of bytes using |
| 63 | # UTF16, ignore the last byte so that the string has an integer number of |
| 64 | # UTF16 code points. |
| 65 | # |
| 66 | reset_db |
| 67 | do_execsql_test atof1-2.10 { |
| 68 | PRAGMA encoding = 'UTF16be'; |
| 69 | CREATE TABLE t1(a, b); |
| 70 | INSERT INTO t1(rowid,a) VALUES (1,x'00'),(2,3); |
| 71 | SELECT substr(a,',') is true FROM t1 ORDER BY rowid; |
| 72 | } {0 1} |
| 73 | do_execsql_test atof1-2.20 { |
| 74 | SELECT substr(a,',') is true FROM t1 ORDER BY rowid DESC; |
| 75 | } {1 0} |
| 76 | do_execsql_test atof1-2.30 { |
| 77 | CREATE INDEX i1 ON t1(a); |
| 78 | SELECT count(*) FROM t1 WHERE substr(a,','); |
| 79 | } {1} |
| 80 | # 2020-08-27 OSSFuzz find related to the above. |
| 81 | do_execsql_test atof1-2.40 { |
| 82 | SELECT randomblob(0) - 1; |
| 83 | } {-1} |
| 84 | |
| 85 | # 2024-12-07 https://sqlite.org/forum/forumpost/569a7209179a7f5e |
| 86 | # Incorrect conversion of floating point or integer literals that |
| 87 | # have significant digits that begin with 1844674407370955 followed |
| 88 | # by more digits in the range 0592 throgh 1609. |
| 89 | # |
| 90 | do_execsql_test atof-3.1 { |
| 91 | WITH RECURSIVE bigval(i,vtxt) AS ( |
| 92 | SELECT 0, '18446744073709550000' |
| 93 | UNION ALL |
| 94 | SELECT i+1, format('1844674407370955%04d',i+1) FROM bigval |
| 95 | WHERE i+1<=9999 |
| 96 | ) |
| 97 | SELECT vtxt, CAST(vtxt AS REAL) FROM bigval |
| 98 | WHERE CAST(vtxt AS REAL) NOT GLOB '1.8446744073709[56]*'; |
| 99 | } {} |
| 100 | do_execsql_test atof-3.2 { |
| 101 | WITH RECURSIVE bigval(i,vtxt) AS ( |
| 102 | SELECT 0, '18.446744073709550000' |
| 103 | UNION ALL |
| 104 | SELECT i+1, format('18.44674407370955%04d',i+1) FROM bigval |
| 105 | WHERE i+1<=9999 |
| 106 | ) |
| 107 | SELECT vtxt, CAST(vtxt AS REAL) FROM bigval |
| 108 | WHERE CAST(vtxt AS REAL) NOT GLOB '18.446744073709*'; |
| 109 | } {} |
| 110 | do_execsql_test atof-3.3 { |
| 111 | WITH RECURSIVE exp(n,v1,v2) AS ( |
| 112 | SELECT -200, '1.8446744073709550592e-200', '1.8446744073709551609e-200' |
| 113 | UNION ALL |
| 114 | SELECT n+1, ('1.8446744073709550592e'||n),('1.8446744073709551609e'||n) |
| 115 | FROM exp WHERE n<200 |
| 116 | ) |
| 117 | SELECT n, v1, v2 |
| 118 | FROM exp |
| 119 | WHERE format('%.10e',CAST(v1 AS REAL)) NOT GLOB '1.8446*' |
| 120 | OR format('%.10e',CAST(v2 AS REAL)) NOT GLOB '1.8446*'; |
| 121 | } {} |
| 122 | |
| 123 | |
| 124 | finish_test |