xref: /sqlite-3.40.0/test/atof1.test (revision 4ef94130)
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
1889f15088Sdrhexpr srand(1)
19*4ef94130Sdrhfor {set i 1} {$i<20000} {incr i} {
2089f15088Sdrh  set pow [expr {int((rand()-0.5)*100)}]
2189f15088Sdrh  set x [expr {pow((rand()-0.5)*2*rand(),$pow)}]
2272b3fbc7Sdrh  set xf [format %.32e $x]
2372b3fbc7Sdrh
2472b3fbc7Sdrh  # Verify that text->real conversions get exactly same ieee754 floating-
2572b3fbc7Sdrh  # point value in SQLite as they do in TCL.
2672b3fbc7Sdrh  #
2772b3fbc7Sdrh  do_test 1.$i.1 {
2889f15088Sdrh    set y [db eval "SELECT $xf=\$x"]
2989f15088Sdrh    if {!$y} {
3089f15088Sdrh      puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175
3189f15088Sdrh      db eval "SELECT $xf+0.0 AS a, \$x AS b" {
3289f15088Sdrh        puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b]
3389f15088Sdrh      }
3489f15088Sdrh    }
3589f15088Sdrh    set y
3689f15088Sdrh  } {1}
3772b3fbc7Sdrh
3872b3fbc7Sdrh  # Verify that round-trip real->text->real conversions using the quote()
3972b3fbc7Sdrh  # function preserve the bits of the numeric value exactly.
4072b3fbc7Sdrh  #
4172b3fbc7Sdrh  do_test 1.$i.2 {
4272b3fbc7Sdrh    set y [db eval {SELECT $x=CAST(quote($x) AS real)}]
4372b3fbc7Sdrh    if {!$y} {
4472b3fbc7Sdrh      db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {}
4572b3fbc7Sdrh      puts "\nIN:    $a $xf"
4672b3fbc7Sdrh      puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]]
4772b3fbc7Sdrh      db eval {SELECT CAST(quote($x) AS real) c} {}
4872b3fbc7Sdrh      puts "OUT:   $b [format %.32e $c]"
4972b3fbc7Sdrh    }
5072b3fbc7Sdrh    set y
5172b3fbc7Sdrh  } {1}
5289f15088Sdrh}
5389f15088Sdrh
5489f15088Sdrh
5589f15088Sdrhfinish_test
56