1
1
using Base. Test
2
2
using FixedPointNumbers
3
3
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
+ @assert abs (fun (fx, fy) - convert (T, fun (fxf, fyf))) <= tol
8
+ @assert abs (convert (Float64, fun (fx, fy)) - fun (fxf, fyf)) <= tol
9
+ end
10
+
4
11
function test_fixed {T} (:: Type{T} , f)
5
12
values = [- 10 : 0.01 : 10 ; - 180 : .01 : - 160 ; 160 : .01 : 180 ]
6
13
tol = 2.0 ^- f
@@ -29,21 +36,16 @@ function test_fixed{T}(::Type{T}, f)
29
36
fy = convert (T,y)
30
37
fyf = convert (Float64, fy)
31
38
32
- @test fx== fy || x!= y
33
- @test fx< fy || x>= y
34
- @test fx<= fy || x> y
39
+ @assert fx== fy || x!= y
40
+ @assert fx< fy || x>= y
41
+ @assert fx<= fy || x> y
35
42
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)
45
47
46
- @test isequal (fx,fy) == isequal (hash (fx),hash (fy))
48
+ @assert isequal (fx,fy) == isequal (hash (fx),hash (fy))
47
49
end
48
50
end
49
51
end
@@ -66,3 +68,9 @@ a = F6[1.2, 1.4]
66
68
acmp = Float64 (a[1 ])* Float64 (a[2 ])
67
69
@test prod (a) == acmp
68
70
@test prod (a, 1 ) == [acmp]
71
+
72
+ x = Fixed {Int8,8} (0.3 )
73
+ for T in (Float16, Float32, Float64, BigFloat)
74
+ y = convert (T, x)
75
+ @test isa (y, T)
76
+ end
0 commit comments