Skip to content

Commit 4a53703

Browse files
committed
Add extensive tests for multiplication correctness, to cover the new change
Tests all FD{(U)Int16} values. Tests most corner cases for FD{(U)Int128} values.
1 parent 0019bb0 commit 4a53703

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

test/fldmod-by-const_tests.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,68 @@ end
5656
end
5757
end
5858
end
59+
60+
@testset "fixed decimal multiplication - exhaustive 16-bit" begin
61+
@testset for P in (0,1,2,3,4)
62+
@testset for T in (Int16, UInt16)
63+
FD = FixedDecimal{T,P}
64+
65+
function test_multiplies_correctly(fd, x)
66+
big = FixedDecimal{BigInt, P}(fd)
67+
big_mul = big * x
68+
# This might overflow: ...
69+
mul = fd * x
70+
# ... so we truncate big to the same size
71+
@test big_mul.i % T == mul.i % T
72+
end
73+
@testset for v in typemin(FD) : eps(FD) : typemax(FD)
74+
test_multiplies_correctly(v, typemin(T))
75+
test_multiplies_correctly(v, -1)
76+
test_multiplies_correctly(v, -eps(FD))
77+
test_multiplies_correctly(v, 0)
78+
test_multiplies_correctly(v, eps(FD))
79+
test_multiplies_correctly(v, 1)
80+
test_multiplies_correctly(v, 2)
81+
test_multiplies_correctly(v, 3)
82+
test_multiplies_correctly(v, typemax(T))
83+
end
84+
end
85+
end
86+
end
87+
88+
@testset "fixed decimal multiplication - 128-bit" begin
89+
@testset for P in 0:37
90+
@testset for T in (Int128, UInt128)
91+
FD = FixedDecimal{T,P}
92+
93+
function test_multiplies_correctly(fd, x)
94+
big = FixedDecimal{BigInt, P}(fd)
95+
big_mul = big * x
96+
# This might overflow: ...
97+
mul = fd * x
98+
# ... so we truncate big to the same size
99+
@test big_mul.i % T == mul.i % T
100+
end
101+
vals = FD[
102+
typemin(FD), typemax(FD),
103+
typemin(FD) + eps(FD), typemax(FD) - eps(FD),
104+
0.0, eps(FD), 0.1, 1.0, 2.0,
105+
typemax(FD) ÷ 2,
106+
]
107+
if T <: Signed
108+
append!(vals, vals.*-1)
109+
end
110+
@testset for v in vals
111+
test_multiplies_correctly(v, typemin(T))
112+
test_multiplies_correctly(v, -1)
113+
test_multiplies_correctly(v, -eps(FD))
114+
test_multiplies_correctly(v, 0)
115+
test_multiplies_correctly(v, eps(FD))
116+
test_multiplies_correctly(v, 1)
117+
test_multiplies_correctly(v, 2)
118+
test_multiplies_correctly(v, 3)
119+
test_multiplies_correctly(v, typemax(T))
120+
end
121+
end
122+
end
123+
end

0 commit comments

Comments
 (0)