1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
discard """
targets: "c cpp 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"
|