Skip to content

Commit 2ec4d19

Browse files
committed
Improve type-stability of tests
1 parent 06b6372 commit 2ec4d19

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

test/fixed.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
using Base.Test
22
using FixedPointNumbers
33

4+
function test_op{F,T}(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol)
5+
# Make sure that the result is representable
6+
(typemin(T) <= fun(fxf, fyf) <= typemax(T)) || return nothing
7+
@test abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
8+
@test abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
9+
end
10+
411
function test_fixed{T}(::Type{T}, f)
512
values = [-10:0.01:10; -180:.01:-160; 160:.01:180]
613
tol = 2.0^-f
@@ -33,15 +40,10 @@ function test_fixed{T}(::Type{T}, f)
3340
@test fx<fy || x>=y
3441
@test fx<=fy || x>y
3542

36-
for fun in (+, -, *, /)
37-
# Make sure that the result is representable
38-
if !(typemin(T) <= fun(fxf, fyf) <= typemax(T))
39-
continue
40-
elseif (fun == /) && fy != 0
41-
@test abs(fun(fx, fy) - convert(T, fun(fxf, fyf))) <= tol
42-
@test abs(convert(Float64, fun(fx, fy)) - fun(fxf, fyf)) <= tol
43-
end
44-
end
43+
test_op(+, T, fx, fy, fxf, fyf, tol)
44+
test_op(-, T, fx, fy, fxf, fyf, tol)
45+
test_op(*, T, fx, fy, fxf, fyf, tol)
46+
fy != 0 && test_op(/, T, fx, fy, fxf, fyf, tol)
4547

4648
@test isequal(fx,fy) == isequal(hash(fx),hash(fy))
4749
end

0 commit comments

Comments
 (0)