189f15088Sdrh# 2012 June 18 289f15088Sdrh# 389f15088Sdrh# The author disclaims copyright to this source code. In place of 489f15088Sdrh# a legal notice, here is a blessing: 589f15088Sdrh# 689f15088Sdrh# May you do good and not evil. 789f15088Sdrh# May you find forgiveness for yourself and forgive others. 889f15088Sdrh# May you share freely, never taking more than you give. 989f15088Sdrh# 1089f15088Sdrh#*********************************************************************** 1189f15088Sdrh# 1289f15088Sdrh# Tests of the sqlite3AtoF() function. 1389f15088Sdrh# 1489f15088Sdrh 1589f15088Sdrhset testdir [file dirname $argv0] 1689f15088Sdrhsource $testdir/tester.tcl 1789f15088Sdrh 182ab410aaSdrhif {$::longdouble_size<=8} { 19c722a2c2Sdrh finish_test 20c722a2c2Sdrh return 21c722a2c2Sdrh} 22*7931f0a3Sdrhif {$::tcl_platform(machine)!="x86_64"} { 23*7931f0a3Sdrh finish_test 24*7931f0a3Sdrh return 25*7931f0a3Sdrh} 26c722a2c2Sdrh 2789f15088Sdrhexpr srand(1) 284ef94130Sdrhfor {set i 1} {$i<20000} {incr i} { 2989f15088Sdrh set pow [expr {int((rand()-0.5)*100)}] 3089f15088Sdrh set x [expr {pow((rand()-0.5)*2*rand(),$pow)}] 3172b3fbc7Sdrh set xf [format %.32e $x] 3272b3fbc7Sdrh 3372b3fbc7Sdrh # Verify that text->real conversions get exactly same ieee754 floating- 3472b3fbc7Sdrh # point value in SQLite as they do in TCL. 3572b3fbc7Sdrh # 36c722a2c2Sdrh do_test atof1-1.$i.1 { 3789f15088Sdrh set y [db eval "SELECT $xf=\$x"] 3889f15088Sdrh if {!$y} { 3989f15088Sdrh puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175 4089f15088Sdrh db eval "SELECT $xf+0.0 AS a, \$x AS b" { 4189f15088Sdrh puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b] 4289f15088Sdrh } 4389f15088Sdrh } 4489f15088Sdrh set y 4589f15088Sdrh } {1} 4672b3fbc7Sdrh 4772b3fbc7Sdrh # Verify that round-trip real->text->real conversions using the quote() 4872b3fbc7Sdrh # function preserve the bits of the numeric value exactly. 4972b3fbc7Sdrh # 50c722a2c2Sdrh do_test atof1-1.$i.2 { 5172b3fbc7Sdrh set y [db eval {SELECT $x=CAST(quote($x) AS real)}] 5272b3fbc7Sdrh if {!$y} { 5372b3fbc7Sdrh db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {} 5472b3fbc7Sdrh puts "\nIN: $a $xf" 5572b3fbc7Sdrh puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]] 5672b3fbc7Sdrh db eval {SELECT CAST(quote($x) AS real) c} {} 5772b3fbc7Sdrh puts "OUT: $b [format %.32e $c]" 5872b3fbc7Sdrh } 5972b3fbc7Sdrh set y 6072b3fbc7Sdrh } {1} 6189f15088Sdrh} 6289f15088Sdrh 6387969b2aSdrh# 2020-01-08 ticket 9eda2697f5cc1aba 6487969b2aSdrh# When running sqlite3AtoF() on a blob with an odd number of bytes using 6587969b2aSdrh# UTF16, ignore the last byte so that the string has an integer number of 6687969b2aSdrh# UTF16 code points. 6787969b2aSdrh# 6887969b2aSdrhreset_db 6987969b2aSdrhdo_execsql_test atof1-2.10 { 7087969b2aSdrh PRAGMA encoding = 'UTF16be'; 7187969b2aSdrh CREATE TABLE t1(a, b); 7287969b2aSdrh INSERT INTO t1(rowid,a) VALUES (1,x'00'),(2,3); 7387969b2aSdrh SELECT substr(a,',') is true FROM t1 ORDER BY rowid; 7487969b2aSdrh} {0 1} 7587969b2aSdrhdo_execsql_test atof1-2.20 { 7687969b2aSdrh SELECT substr(a,',') is true FROM t1 ORDER BY rowid DESC; 7787969b2aSdrh} {1 0} 7887969b2aSdrhdo_execsql_test atof1-2.30 { 7987969b2aSdrh CREATE INDEX i1 ON t1(a); 8087969b2aSdrh SELECT count(*) FROM t1 WHERE substr(a,','); 8187969b2aSdrh} {1} 82359941bdSdrh# 2020-08-27 OSSFuzz find related to the above. 83359941bdSdrhdo_execsql_test atof1-2.40 { 84359941bdSdrh SELECT randomblob(0) - 1; 85359941bdSdrh} {-1} 8687969b2aSdrh 8789f15088Sdrh 8889f15088Sdrhfinish_test 89