diff options
Diffstat (limited to 'tests/float')
-rw-r--r-- | tests/float/tfloat1.nim | 3 | ||||
-rw-r--r-- | tests/float/tfloat2.nim | 3 | ||||
-rw-r--r-- | tests/float/tfloat3.nim | 15 | ||||
-rw-r--r-- | tests/float/tfloat4.nim | 3 | ||||
-rw-r--r-- | tests/float/tfloat5.nim | 7 | ||||
-rw-r--r-- | tests/float/tfloat6.nim | 7 | ||||
-rw-r--r-- | tests/float/tfloat7.nim | 7 | ||||
-rw-r--r-- | tests/float/tfloatmod.nim | 130 | ||||
-rw-r--r-- | tests/float/tfloatnan.nim | 4 | ||||
-rw-r--r-- | tests/float/tissue5821.nim | 8 |
10 files changed, 156 insertions, 31 deletions
diff --git a/tests/float/tfloat1.nim b/tests/float/tfloat1.nim index ed99260ea..54d49a7c1 100644 --- a/tests/float/tfloat1.nim +++ b/tests/float/tfloat1.nim @@ -1,5 +1,4 @@ discard """ - file: "tfloat1.nim" outputsub: "Error: unhandled exception: FPU operation caused an overflow [FloatOverflowError]" exitcode: "1" """ @@ -11,5 +10,3 @@ var x = 0.8 var y = 0.0 echo x / y #OUT Error: unhandled exception: FPU operation caused an overflow - - diff --git a/tests/float/tfloat2.nim b/tests/float/tfloat2.nim index b84120fba..130c71087 100644 --- a/tests/float/tfloat2.nim +++ b/tests/float/tfloat2.nim @@ -1,5 +1,4 @@ discard """ - file: "tfloat2.nim" outputsub: "Error: unhandled exception: FPU operation caused a NaN result [FloatInvalidOpError]" exitcode: "1" """ @@ -11,5 +10,3 @@ var x = 0.0 var y = 0.0 echo x / y #OUT Error: unhandled exception: FPU operation caused a NaN result - - diff --git a/tests/float/tfloat3.nim b/tests/float/tfloat3.nim index a14c6c396..215470cfc 100644 --- a/tests/float/tfloat3.nim +++ b/tests/float/tfloat3.nim @@ -1,15 +1,15 @@ discard """ - file: "tfloat3.nim" - output: "Nim 3.4368930843, 0.3299290698 C double: 3.4368930843, 0.3299290698" + output: ''' +Nim 3.4368930843, 0.3299290698 +C double: 3.4368930843, 0.3299290698''' """ import math, strutils {.emit: """ void printFloats(void) { - double y = 1.234567890123456789; - - printf("C double: %.10f, %.10f ", exp(y), cos(y)); + double y = 1.234567890123456789; + printf("C double: %.10f, %.10f\n", exp(y), cos(y)); } """.} @@ -17,8 +17,5 @@ proc c_printf(frmt: cstring) {.importc: "printf", header: "<stdio.h>", varargs.} proc printFloats {.importc, nodecl.} var x: float = 1.234567890123456789 -c_printf("Nim %.10f, %.10f ", exp(x), cos(x)) +c_printf("Nim %.10f, %.10f\n", exp(x), cos(x)) printFloats() - - - diff --git a/tests/float/tfloat4.nim b/tests/float/tfloat4.nim index 68df56be8..56280111c 100644 --- a/tests/float/tfloat4.nim +++ b/tests/float/tfloat4.nim @@ -1,8 +1,7 @@ discard """ - file: "tfloat4.nim" output: "passed all tests." - exitcode: 0 """ + import math, strutils proc c_sprintf(buf, fmt: cstring) {.importc:"sprintf", header: "<stdio.h>", varargs.} diff --git a/tests/float/tfloat5.nim b/tests/float/tfloat5.nim index aa7dc6c53..0708838fc 100644 --- a/tests/float/tfloat5.nim +++ b/tests/float/tfloat5.nim @@ -1,9 +1,10 @@ discard """ - file: "tfloat5.nim" - output: '''0 : 0.0 +output: ''' 0 : 0.0 0 : 0.0 -0 : 0.0''' +0 : 0.0 +0 : 0.0 +''' """ import parseutils diff --git a/tests/float/tfloat6.nim b/tests/float/tfloat6.nim index 8e043a658..c4cd6e932 100644 --- a/tests/float/tfloat6.nim +++ b/tests/float/tfloat6.nim @@ -1,12 +1,13 @@ discard """ - file: "tfloat6.nim" - output: '''1e-06 : 1e-06 + output: ''' +1e-06 : 1e-06 1e-06 : 1e-06 0.001 : 0.001 1e-06 : 1e-06 1e-06 : 1e-06 10.000001 : 10.000001 -100.000001 : 100.000001''' +100.000001 : 100.000001 +''' disabled: "windows" """ diff --git a/tests/float/tfloat7.nim b/tests/float/tfloat7.nim index 5fd0d43d9..a6d7af10b 100644 --- a/tests/float/tfloat7.nim +++ b/tests/float/tfloat7.nim @@ -1,12 +1,13 @@ discard """ - file: "tfloat6.nim" - output: '''passed. +output: ''' passed. passed. passed. passed. passed. -passed.''' +passed. +passed. +''' """ import strutils diff --git a/tests/float/tfloatmod.nim b/tests/float/tfloatmod.nim new file mode 100644 index 000000000..0b84ec7e0 --- /dev/null +++ b/tests/float/tfloatmod.nim @@ -0,0 +1,130 @@ +discard """ + targets: "c c++ js" + output: "ok" + exitcode: "0" +""" + +# Test `mod` on float64 both at compiletime and at runtime +import math + +# Testdata from golang +const testValues: array[10, tuple[f64, expected: float64]] = [ + (4.9790119248836735e+00, 4.197615023265299782906368e-02), + (7.7388724745781045e+00, 2.261127525421895434476482e+00), + (-2.7688005719200159e-01, 3.231794108794261433104108e-02), + (-5.0106036182710749e+00, 4.989396381728925078391512e+00), + (9.6362937071984173e+00, 3.637062928015826201999516e-01), + (2.9263772392439646e+00, 1.220868282268106064236690e+00), + (5.2290834314593066e+00, 4.770916568540693347699744e+00), + (2.7279399104360102e+00, 1.816180268691969246219742e+00), + (1.8253080916808550e+00, 8.734595415957246977711748e-01), + (-8.6859247685756013e+00, 1.314075231424398637614104e+00)] + +const simpleTestData = [ + (5.0, 3.0, 2.0), + (5.0, -3.0, 2.0), + (-5.0, 3.0, -2.0), + (-5.0, -3.0, -2.0), + (10.0, 1.0, 0.0), + (10.0, 0.5, 0.0), + (10.0, 1.5, 1.0), + (-10.0, 1.0, -0.0), + (-10.0, 0.5, -0.0), + (-10.0, 1.5, -1.0), + (1.5, 1.0, 0.5), + (1.25, 1.0, 0.25), + (1.125, 1.0, 0.125) + ] + +const specialCases = [ + (-Inf, -Inf, Nan), + (-Inf, -Pi, Nan), + (-Inf, 0.0, Nan), + (-Inf, Pi, Nan), + (-Inf, Inf, Nan), + (-Inf, Nan, Nan), + (-PI, -Inf, -PI), + (-PI, 0.0, Nan), + (-PI, Inf, -PI), + (-PI, Nan, Nan), + (-0.0, -Inf, -0.0), + (-0.0, 0.0, Nan), + (-0.0, Inf, -0.0), + (-0.0, Nan, Nan), + (0.0, -Inf, 0.0), + (0.0, 0.0, Nan), + (0.0, Inf, 0.0), + (0.0, Nan, Nan), + (PI, -Inf, PI), + (PI, 0.0, Nan), + (PI, Inf, PI), + (PI, Nan, Nan), + (Inf, -Inf, Nan), + (Inf, -PI, Nan), + (Inf, 0.0, Nan), + (Inf, PI, Nan), + (Inf, Inf, Nan), + (Inf, Nan, Nan), + (Nan, -Inf, Nan), + (Nan, -PI, Nan), + (Nan, 0.0, Nan), + (Nan, PI, Nan), + (Nan, Inf, Nan), + (Nan, Nan, Nan)] + +const extremeValues = [ + (5.9790119248836734e+200, 1.1258465975523544, 0.6447968302508578), + (1.0e-100, 1.0e100, 1.0e-100)] + +proc errmsg(x, y, r, expected: float64): string = + $x & " mod " & $y & " == " & $r & " but expected " & $expected + +proc golangtest() = + let x = 10.0 + for tpl in testValues: + let (y, expected) = tpl + let r = x mod y + doAssert(r == expected, errmsg(x, y, r, expected)) + +proc simpletest() = + for tpl in simpleTestData: + let(x, y, expected) = tpl + let r = x mod y + doAssert(r == expected, errmsg(x, y, r, expected)) + +proc testSpecialCases() = + proc isnan(f: float64): bool = + case classify(f) + of fcNan: + result = true + else: + result = false + + for tpl in specialCases: + let(x, y, expected) = tpl + let r = x mod y + doAssert((r == expected) or (r.isnan and expected.isnan), + errmsg(x, y, r, expected)) + +proc testExtremeValues() = + for tpl in extremeValues: + let (x, y, expected) = tpl + let r = x mod y + doAssert(r == expected, errmsg(x, y, r, expected)) + +static: + # compiletime evaluation + golangtest() + simpletest() + testSpecialCases() + testExtremeValues() + +proc main() = + # runtime evaluation + golangtest() + simpletest() + testSpecialCases() + testExtremeValues() + +main() +echo "ok" diff --git a/tests/float/tfloatnan.nim b/tests/float/tfloatnan.nim index aa288d342..29937a862 100644 --- a/tests/float/tfloatnan.nim +++ b/tests/float/tfloatnan.nim @@ -1,6 +1,6 @@ discard """ - file: "tfloatnan.nim" - output: '''Nim: nan +output: ''' +Nim: nan Nim: nan (float) Nim: nan (double) ''' diff --git a/tests/float/tissue5821.nim b/tests/float/tissue5821.nim index e8aa4a1d9..c4f561f09 100644 --- a/tests/float/tissue5821.nim +++ b/tests/float/tissue5821.nim @@ -1,7 +1,7 @@ discard """ - file: "tissue5821.nim" - output: '''''' +output: "ok" """ + proc main(): void = let a: float32 = 47.11'f32 doAssert a == 47.11'f32 @@ -10,4 +10,6 @@ proc main(): void = doAssert b != 10.123402823e+38'f64 doAssert b == 10.234402823e+38'f64 -main() \ No newline at end of file + echo "ok" + +main() |