xref: /sqlite-3.40.0/test/round1.test (revision cf2ad7ae)
1a0ed86bcSdrh# 2019-05-24
2a0ed86bcSdrh#
3a0ed86bcSdrh# The author disclaims copyright to this source code.  In place of
4a0ed86bcSdrh# a legal notice, here is a blessing:
5a0ed86bcSdrh#
6a0ed86bcSdrh#    May you do good and not evil.
7a0ed86bcSdrh#    May you find forgiveness for yourself and forgive others.
8a0ed86bcSdrh#    May you share freely, never taking more than you give.
9a0ed86bcSdrh#
10a0ed86bcSdrh#***********************************************************************
11a0ed86bcSdrh# Test cases for rounding behavior of floating point values.
12a0ed86bcSdrh#
13*cf2ad7aeSdan# TESTRUNNER: slow
14a0ed86bcSdrh
15a0ed86bcSdrhset testdir [file dirname $argv0]
16a0ed86bcSdrhsource $testdir/tester.tcl
17a0ed86bcSdrhset testprefix round1
18a0ed86bcSdrh
19a0ed86bcSdrhexpr srand(0)
20a0ed86bcSdrhunset -nocomplain iTest
21ef7d5187Sdrhfor {set iTest 1} {$iTest<=50000} {incr iTest} {
22a0ed86bcSdrh   set x1 [expr int(rand()*100000)]
23a0ed86bcSdrh   set x2 [expr int(rand()*100000)+1000*int(rand()*10000)]
24a0ed86bcSdrh   set n [expr int(rand()*8)+1]
25a0ed86bcSdrh   set x3 [string range [format %09d $x2] [expr {9-$n}] end]
26a0ed86bcSdrh   set r $x1.$x3
27a0ed86bcSdrh   set ans [string trimright $r 0]
28a0ed86bcSdrh   if {[string match *. $ans]} {set ans ${ans}0}
29a0ed86bcSdrh   do_test $iTest/$n/${r}4=>$ans {
30a0ed86bcSdrh     set x [db one "SELECT round(${r}4,$n)"]
31a0ed86bcSdrh   } $ans
32a0ed86bcSdrh   set x4 [string range [format %09d [expr {$x2+1}]] [expr {9-$n}] end]
33a0ed86bcSdrh   if {[string trim $x3 9]==""} {incr x1}
34a0ed86bcSdrh   set r2 $x1.$x4
35a0ed86bcSdrh   set ans [string trimright $r2 0]
36a0ed86bcSdrh   if {[string match *. $ans]} {set ans ${ans}0}
37a0ed86bcSdrh   do_test $iTest/$n/${r}5=>$ans {
38a0ed86bcSdrh     set x [db one "SELECT round(${r}5,$n)"]
39a0ed86bcSdrh   } $ans
40a0ed86bcSdrh}
41a0ed86bcSdrh
42a0ed86bcSdrhfinish_test
43