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