@@ -49,102 +49,121 @@ function test_fixed(::Type{T}, f) where {T}
4949 end
5050end
5151
52- @test isapprox (convert (Fixed{Int8,7 }, 0.8 ), 0.797 , atol= 0.001 )
53- @test isapprox (convert (Fixed{Int8,7 }, 0.9 ), 0.898 , atol= 0.001 )
54- @test_throws InexactError convert (Fixed{Int8, 7 }, 0.999 )
55- @test_throws InexactError convert (Fixed{Int8, 7 }, 1.0 )
56- @test_throws InexactError convert (Fixed{Int8, 7 }, 1 )
57- @test_throws InexactError convert (Fixed{Int8, 7 }, 2 )
58- @test_throws InexactError convert (Fixed{Int8, 7 }, 128 )
59-
60- for (TI, f) in [(Int8, 8 ), (Int16, 8 ), (Int16, 10 ), (Int32, 16 )]
61- T = Fixed{TI,f}
62- println (" Testing $T " )
63- test_fixed (T, f)
52+ @testset " conversion" begin
53+ @test isapprox (convert (Fixed{Int8,7 }, 0.8 ), 0.797 , atol= 0.001 )
54+ @test isapprox (convert (Fixed{Int8,7 }, 0.9 ), 0.898 , atol= 0.001 )
55+ @test_throws InexactError convert (Fixed{Int8, 7 }, 0.999 )
56+ @test_throws InexactError convert (Fixed{Int8, 7 }, 1.0 )
57+ @test_throws InexactError convert (Fixed{Int8, 7 }, 1 )
58+ @test_throws InexactError convert (Fixed{Int8, 7 }, 2 )
59+ @test_throws InexactError convert (Fixed{Int8, 7 }, 128 )
6460end
6561
66- T = Fixed{Int8,7 }
67- for i = - 1.0 : 0.1 : typemax (T)
68- @test i % T === T (i)
62+ @testset " test_fixed" begin
63+ for (TI, f) in [(Int8, 8 ), (Int16, 8 ), (Int16, 10 ), (Int32, 16 )]
64+ T = Fixed{TI,f}
65+ println (" Testing $T " )
66+ test_fixed (T, f)
67+ end
68+ end
69+
70+ @testset " modulus" begin
71+ T = Fixed{Int8,7 }
72+ for i = - 1.0 : 0.1 : typemax (T)
73+ @test i % T === T (i)
74+ end
75+ @test ( 1.5 % T). i == round (Int, 1.5 * 128 ) % Int8
76+ @test (- 0.3 % T). i == round (Int, - 0.3 * 128 ) % Int8
77+
78+ T = Fixed{Int16,9 }
79+ for i = - 64.0 : 0.1 : typemax (T)
80+ @test i % T === T (i)
81+ end
82+ @test ( 65.2 % T). i == round (Int, 65.2 * 512 ) % Int16
83+ @test (- 67.2 % T). i == round (Int, - 67.2 * 512 ) % Int16
84+ end
85+
86+ @testset " testapprox" begin
87+ for T in [Fixed{Int8,7 }, Fixed{Int16,8 }, Fixed{Int16,10 }]
88+ testapprox (T) # defined in ufixed.jl
89+ end
90+ end
91+
92+ @testset " reductions" begin
93+ F8 = Fixed{Int8,8 }
94+ a = F8[0.498 , 0.1 ]
95+ acmp = convert (Float64, a[1 ]) + convert (Float64, a[2 ])
96+ @test sum (a) == acmp
97+ @test sum (a, dims= 1 ) == [acmp]
98+
99+ F6 = Fixed{Int8,6 }
100+ a = F6[1.2 , 1.4 ]
101+ acmp = convert (Float64, a[1 ])* convert (Float64, a[2 ])
102+ @test prod (a) == acmp
103+ @test prod (a, dims= 1 ) == [acmp]
104+ end
105+
106+ @testset " convert result type" begin
107+ x = Fixed {Int8,8} (0.3 )
108+ for T in (Float16, Float32, Float64, BigFloat)
109+ y = convert (T, x)
110+ @test isa (y, T)
111+ end
112+ end
113+
114+ @testset " Integer conversions" begin
115+ @test convert (Int, Q1f6 (1 )) === 1
116+ @test convert (Integer, Q1f6 (1 )) === Int8 (1 )
69117end
70- @test ( 1.5 % T). i == round (Int, 1.5 * 128 ) % Int8
71- @test (- 0.3 % T). i == round (Int, - 0.3 * 128 ) % Int8
72118
73- T = Fixed{Int16,9 }
74- for i = - 64.0 : 0.1 : typemax (T)
75- @test i % T === T (i)
119+ @testset " Floating-point conversions" begin
120+ @test isa (float (one (Fixed{Int8,6 })), Float32)
121+ @test isa (float (one (Fixed{Int32,18 })), Float64)
122+ @test isa (float (one (Fixed{Int32,25 })), Float64)
76123end
77- @test ( 65.2 % T). i == round (Int, 65.2 * 512 ) % Int16
78- @test (- 67.2 % T). i == round (Int, - 67.2 * 512 ) % Int16
79124
80- for T in [Fixed{Int8,7 }, Fixed{Int16,8 }, Fixed{Int16,10 }]
81- local T
82- testapprox (T) # defined in ufixed.jl
125+ @testset " Show" begin
126+ x = Fixed {Int32,5} (0.25 )
127+ iob = IOBuffer ()
128+ show (iob, x)
129+ str = String (take! (iob))
130+ @test str == " 0.25Q26f5"
131+ @test eval (Meta. parse (str)) == x
83132end
84133
85- # reductions
86- F8 = Fixed{Int8,8 }
87- a = F8[0.498 , 0.1 ]
88- acmp = Float64 (a[1 ]) + Float64 (a[2 ])
89- @test sum (a) == acmp
90- @test sum (a, 1 ) == [acmp]
91-
92- F6 = Fixed{Int8,6 }
93- a = F6[1.2 , 1.4 ]
94- acmp = Float64 (a[1 ])* Float64 (a[2 ])
95- @test prod (a) == acmp
96- @test prod (a, 1 ) == [acmp]
97-
98- x = Fixed {Int8,8} (0.3 )
99- for T in (Float16, Float32, Float64, BigFloat)
100- local T
101- y = convert (T, x)
102- @test isa (y, T)
134+ @testset " rand" begin
135+ for T in (Fixed{Int8,8 }, Fixed{Int16,8 }, Fixed{Int16,10 }, Fixed{Int32,16 })
136+ a = rand (T)
137+ @test isa (a, T)
138+ a = rand (T, (3 , 5 ))
139+ @test ndims (a) == 2 && eltype (a) == T
140+ @test size (a) == (3 ,5 )
141+ end
103142end
104143
105- @test convert (Int, Q1f6 (1 )) === 1
106- @test convert (Integer, Q1f6 (1 )) === Int8 (1 )
107-
108- # Floating-point conversions
109- @test isa (float (one (Fixed{Int8,6 })), Float32)
110- @test isa (float (one (Fixed{Int32,18 })), Float64)
111- @test isa (float (one (Fixed{Int32,25 })), Float64)
112-
113- # Show
114- x = Fixed {Int32,5} (0.25 )
115- iob = IOBuffer ()
116- show (iob, x)
117- str = String (take! (iob))
118- @test str == " 0.25Q26f5"
119- @test eval (parse (str)) == x
120-
121- for T in (Fixed{Int8,8 }, Fixed{Int16,8 }, Fixed{Int16,10 }, Fixed{Int32,16 })
122- local T
123- a = rand (T)
124- @test isa (a, T)
125- a = rand (T, (3 , 5 ))
126- @test ndims (a) == 2 && eltype (a) == T
127- @test size (a) == (3 ,5 )
144+ @testset " realmin" begin
145+ # issue #79
146+ @test realmin (Q11f4) == Q11f4 (0.06 )
128147end
129148
130- # issue #79
131- @test realmin (Q11f4) == Q11f4 ( 0.06 )
132-
133- # Test disambiguation constructors
134- @test_throws ArgumentError Fixed {Int32,16} (' a ' )
135- @test_throws InexactError Fixed {Int32,16} ( complex ( 1.0 , 1.0 ))
136- @test Fixed {Int32,16} ( complex ( 1.0 , 0.0 )) == 1
137- @test Fixed {Int32,16} (Base . TwicePrecision ( 1.0 , 0.0 )) == 1
138-
139- # test all-fractional fixed-point numbers (issue #104)
140- for (T, f) in ((Int8, 7 ),
141- (Int16, 15 ),
142- (Int32, 31 ),
143- (Int64, 63 ) )
144- tmax = typemax (Fixed{T, f})
145- @test tmax == BigInt ( typemax (T )) / BigInt ( 2 ) ^ f
146- tol = (tmax + BigFloat ( 1.0 )) / ( sizeof (T) * 8 )
147- for x in linspace ( - 1 , BigFloat (tmax) - tol, 50 )
148- @test abs ( Fixed {T, f} (x) - x) <= tol
149+ @testset " Disambiguation constructors " begin
150+ @test_throws ArgumentError Fixed {Int32,16} ( ' a ' )
151+ @test_throws InexactError Fixed {Int32,16} ( complex ( 1.0 , 1.0 ))
152+ @test Fixed {Int32,16} ( complex ( 1.0 , 0.0 )) == 1
153+ @test Fixed {Int32,16} (Base . TwicePrecision ( 1.0 , 0.0 )) == 1
154+ end
155+
156+ @testset " fractional fixed-point numbers " begin
157+ # test all-fractional fixed-point numbers (issue #104)
158+ for (T, f) in ((Int8, 7 ),
159+ (Int16, 15 ),
160+ (Int32, 31 ),
161+ (Int64, 63 ))
162+ tmax = typemax (Fixed{T, f} )
163+ @test tmax == BigInt ( typemax (T)) / BigInt ( 2 ) ^ f
164+ tol = (tmax + BigFloat ( 1.0 )) / ( sizeof (T) * 8 )
165+ for x in range ( - 1 , stop = convert (BigFloat, tmax) - tol, length = 50 )
166+ @test abs ( Fixed {T, f} (x) - x) <= tol
167+ end
149168 end
150169end
0 commit comments