xref: /sqlite-3.40.0/test/atof1.test (revision c722a2c2)
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
18*c722a2c2Sdrhif {![info exists __GNUC__]} {
19*c722a2c2Sdrh  finish_test
20*c722a2c2Sdrh  return
21*c722a2c2Sdrh}
22*c722a2c2Sdrh
2389f15088Sdrhexpr srand(1)
244ef94130Sdrhfor {set i 1} {$i<20000} {incr i} {
2589f15088Sdrh  set pow [expr {int((rand()-0.5)*100)}]
2689f15088Sdrh  set x [expr {pow((rand()-0.5)*2*rand(),$pow)}]
2772b3fbc7Sdrh  set xf [format %.32e $x]
2872b3fbc7Sdrh
2972b3fbc7Sdrh  # Verify that text->real conversions get exactly same ieee754 floating-
3072b3fbc7Sdrh  # point value in SQLite as they do in TCL.
3172b3fbc7Sdrh  #
32*c722a2c2Sdrh  do_test atof1-1.$i.1 {
3389f15088Sdrh    set y [db eval "SELECT $xf=\$x"]
3489f15088Sdrh    if {!$y} {
3589f15088Sdrh      puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175
3689f15088Sdrh      db eval "SELECT $xf+0.0 AS a, \$x AS b" {
3789f15088Sdrh        puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b]
3889f15088Sdrh      }
3989f15088Sdrh    }
4089f15088Sdrh    set y
4189f15088Sdrh  } {1}
4272b3fbc7Sdrh
4372b3fbc7Sdrh  # Verify that round-trip real->text->real conversions using the quote()
4472b3fbc7Sdrh  # function preserve the bits of the numeric value exactly.
4572b3fbc7Sdrh  #
46*c722a2c2Sdrh  do_test atof1-1.$i.2 {
4772b3fbc7Sdrh    set y [db eval {SELECT $x=CAST(quote($x) AS real)}]
4872b3fbc7Sdrh    if {!$y} {
4972b3fbc7Sdrh      db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {}
5072b3fbc7Sdrh      puts "\nIN:    $a $xf"
5172b3fbc7Sdrh      puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]]
5272b3fbc7Sdrh      db eval {SELECT CAST(quote($x) AS real) c} {}
5372b3fbc7Sdrh      puts "OUT:   $b [format %.32e $c]"
5472b3fbc7Sdrh    }
5572b3fbc7Sdrh    set y
5672b3fbc7Sdrh  } {1}
5789f15088Sdrh}
5889f15088Sdrh
5989f15088Sdrh
6089f15088Sdrhfinish_test
61