Skip to content

Commit 477387b

Browse files
committed
Improve test_fixed
1 parent 443e6ac commit 477387b

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
@@ -5,50 +5,45 @@ function symbol_to_inttype(::Type{Fixed}, s::Symbol)
55
d[s]
66
end
77

8-
function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
8+
function test_op(fun::Fun, fx::F, fy::F, fxf, fyf, tol) where {Fun, F}
99
# Make sure that the result is representable
10-
(typemin(T) <= fun(fxf, fyf) <= typemax(T)) || return nothing
11-
@assert abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
12-
@assert abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
13-
end
14-
15-
function test_fixed(::Type{T}) where {T}
16-
values = [-10:0.01:10; -180:.01:-160; 160:.01:180]
17-
tol = Float64(eps(T))
10+
zf = fun(fxf, fyf)
11+
typemin(F) <= zf <= typemax(F) || return nothing
12+
z = fun(fx, fy)
13+
@assert abs(z - convert(F, zf)) <= tol
14+
@assert abs(convert(Float64, z) - zf) <= tol
15+
end
16+
17+
function test_fixed(::Type{F}) where {F}
18+
tol = Float64(eps(F))
19+
v = [-10:0.01:10; -180:.01:-160; 160:.01:180]
20+
# Ignore values outside the representable range
21+
values = filter(x -> typemin(F) < x <= typemax(F), v)
1822
for x in values
19-
# Ignore values outside the representable range
20-
# typemin <, otherwise for -(-0.5) > typemax
21-
if !(typemin(T) < x <= typemax(T))
22-
continue
23-
end
24-
fx = convert(T,x)
25-
@test convert(T,convert(Float64, fx)) == fx
26-
@test convert(T,convert(Float64, -fx)) == -fx
27-
@test convert(Float64, -fx) == -convert(Float64, fx)
28-
23+
fx = convert(F, x)
2924
fxf = convert(Float64, fx)
3025

31-
rx = convert(Rational{BigInt},fx)
32-
@assert isequal(fx,rx) == isequal(hash(fx),hash(rx))
26+
@test convert(F, convert(Float64, fx)) === fx
27+
@test convert(F, convert(Float64, -fx)) === -fx
28+
@test convert(Float64, -fx) == -convert(Float64, fx)
3329

34-
for y in values
35-
if !(typemin(T) < y <= typemax(T))
36-
continue
37-
end
30+
rx = convert(Rational{BigInt}, fx)
31+
@assert isequal(fx, rx) == isequal(hash(fx), hash(rx))
3832

39-
fy = convert(T,y)
33+
for y in values
34+
fy = convert(F, y)
4035
fyf = convert(Float64, fy)
4136

4237
@assert fx==fy || x!=y
4338
@assert fx<fy || (x + tol)>=y
4439
@assert fx<=fy || x>y
4540

46-
test_op(+, T, fx, fy, fxf, fyf, tol)
47-
test_op(-, T, fx, fy, fxf, fyf, tol)
48-
test_op(*, T, fx, fy, fxf, fyf, tol)
49-
fy != 0 && test_op(/, T, fx, fy, fxf, fyf, tol)
41+
test_op(+, fx, fy, fxf, fyf, tol)
42+
test_op(-, fx, fy, fxf, fyf, tol)
43+
test_op(*, fx, fy, fxf, fyf, tol)
44+
fy != 0 && test_op(/, fx, fy, fxf, fyf, tol)
5045

51-
@assert isequal(fx,fy) == isequal(hash(fx),hash(fy))
46+
@assert isequal(fx, fy) === isequal(hash(fx), hash(fy))
5247
end
5348
end
5449
end

0 commit comments

Comments
 (0)