Skip to content

Commit 9adacd5

Browse files
committed
Improve test_fixed
1 parent 2a4a61b commit 9adacd5

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

test/fixed.jl

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,45 @@ function symbol_to_inttype(::Type{Fixed}, s::Symbol)
1313
d[s]
1414
end
1515

16-
function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
16+
function test_op(fun::Fun, fx::F, fy::F, fxf, fyf, tol) where {Fun, F}
1717
# Make sure that the result is representable
18-
(typemin(T) <= fun(fxf, fyf) <= typemax(T)) || return nothing
19-
@assert abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
20-
@assert abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
21-
end
22-
23-
function test_fixed(::Type{T}) where {T}
24-
values = [-10:0.01:10; -180:.01:-160; 160:.01:180]
25-
tol = Float64(eps(T))
18+
zf = fun(fxf, fyf)
19+
typemin(F) <= zf <= typemax(F) || return nothing
20+
z = fun(fx, fy)
21+
@assert abs(z - convert(F, zf)) <= tol
22+
@assert abs(convert(Float64, z) - zf) <= tol
23+
end
24+
25+
function test_fixed(::Type{F}) where {F}
26+
tol = Float64(eps(F))
27+
v = [-10:0.01:10; -180:.01:-160; 160:.01:180]
28+
# Ignore values outside the representable range
29+
values = filter(x -> typemin(F) < x <= typemax(F), v)
2630
for x in values
27-
# Ignore values outside the representable range
28-
# typemin <, otherwise for -(-0.5) > typemax
29-
if !(typemin(T) < x <= typemax(T))
30-
continue
31-
end
32-
fx = convert(T,x)
33-
@test convert(T,convert(Float64, fx)) == fx
34-
@test convert(T,convert(Float64, -fx)) == -fx
35-
@test convert(Float64, -fx) == -convert(Float64, fx)
36-
31+
fx = convert(F, x)
3732
fxf = convert(Float64, fx)
3833

39-
rx = convert(Rational{BigInt},fx)
40-
@assert isequal(fx,rx) == isequal(hash(fx),hash(rx))
34+
@test convert(F, convert(Float64, fx)) === fx
35+
@test convert(F, convert(Float64, -fx)) === -fx
36+
@test convert(Float64, -fx) == -convert(Float64, fx)
4137

42-
for y in values
43-
if !(typemin(T) < y <= typemax(T))
44-
continue
45-
end
38+
rx = convert(Rational{BigInt}, fx)
39+
@assert isequal(fx, rx) == isequal(hash(fx), hash(rx))
4640

47-
fy = convert(T,y)
41+
for y in values
42+
fy = convert(F, y)
4843
fyf = convert(Float64, fy)
4944

5045
@assert fx==fy || x!=y
5146
@assert fx<fy || (x + tol)>=y
5247
@assert fx<=fy || x>y
5348

54-
test_op(+, T, fx, fy, fxf, fyf, tol)
55-
test_op(-, T, fx, fy, fxf, fyf, tol)
56-
test_op(*, T, fx, fy, fxf, fyf, tol)
57-
fy != 0 && test_op(/, T, fx, fy, fxf, fyf, tol)
49+
test_op(+, fx, fy, fxf, fyf, tol)
50+
test_op(-, fx, fy, fxf, fyf, tol)
51+
test_op(*, fx, fy, fxf, fyf, tol)
52+
fy != 0 && test_op(/, fx, fy, fxf, fyf, tol)
5853

59-
@assert isequal(fx,fy) == isequal(hash(fx),hash(fy))
54+
@assert isequal(fx, fy) === isequal(hash(fx), hash(fy))
6055
end
6156
end
6257
end

0 commit comments

Comments
 (0)