Skip to content

Commit 408033a

Browse files
committed
Fix all v0.7 deprecations and errors, and convert tests to testsets
1 parent cf9ebc4 commit 408033a

7 files changed

Lines changed: 394 additions & 342 deletions

File tree

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
julia 0.6
2-
Compat 0.35
1+
julia 0.7-

src/FixedPointNumbers.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function show(io::IO, x::FixedPoint{T,f}) where {T,f}
111111
showtype(io, typeof(x))
112112
end
113113
const _log2_10 = 3.321928094887362
114-
showcompact(io::IO, x::FixedPoint{T,f}) where {T,f} = show(io, round(convert(Float64,x), ceil(Int,f/_log2_10)))
114+
showcompact(io::IO, x::FixedPoint{T,f}) where {T,f} = show(io, round(convert(Float64,x), digits=ceil(Int,f/_log2_10)))
115115

116116
if VERSION >= v"0.7.0-DEV.1790"
117117
function Base.showarg(io::IO, a::Array{T}, toplevel) where {T<:FixedPoint}
@@ -147,12 +147,12 @@ if isdefined(Base, :r_promote)
147147
Base.reducedim_initarray(A, region, oneunit(Treduce))
148148
else
149149
# Julia v0.7
150-
Base.add_sum(x::FixedPoint, y::FixedPoint) = Treduce(x) + Treduce(y)
150+
Base.add_sum(x::FixedPoint, y::FixedPoint) = convert(Treduce, x) + convert(Treduce, y)
151151
Base.reduce_empty(::typeof(Base.add_sum), ::Type{F}) where {F<:FixedPoint} = zero(Treduce)
152-
Base.reduce_first(::typeof(Base.add_sum), x::FixedPoint) = Treduce(x)
153-
Base.mul_prod(x::FixedPoint, y::FixedPoint) = Treduce(x) * Treduce(y)
152+
Base.reduce_first(::typeof(Base.add_sum), x::FixedPoint) = convert(Treduce, x)
153+
Base.mul_prod(x::FixedPoint, y::FixedPoint) = convert(Treduce, x) * convert(Treduce, y)
154154
Base.reduce_empty(::typeof(Base.mul_prod), ::Type{F}) where {F<:FixedPoint} = one(Treduce)
155-
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = Treduce(x)
155+
Base.reduce_first(::typeof(Base.mul_prod), x::FixedPoint) = convert(Treduce, x)
156156
end
157157

158158

src/fixed.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Fixed{T <: Signed,f} <: FixedPoint{T, f}
77
Fixed{T, f}(i::Integer, _) where {T,f} = new{T, f}(i % T)
88
Fixed{T, f}(x) where {T,f} = convert(Fixed{T,f}, x)
99
Fixed{T, f}(x::Fixed{T,f}) where {T,f} = x
10-
Fixed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Fixed cannot be constructed from a Char"))
10+
Fixed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Fixed cannot be constructed from a Char"))
1111
Fixed{T, f}(x::Complex) where {T,f} = Fixed{T, f}(convert(real(typeof(x)), x))
1212
Fixed{T, f}(x::Base.TwicePrecision) where {T,f} = Fixed{T, f}(convert(Float64, x))
1313
end

src/normed.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}
77
Normed{T, f}(i::Integer,_) where {T,f} = new{T, f}(i%T) # for setting by raw representation
88
Normed{T, f}(x) where {T,f} = convert(Normed{T,f}, x)
99
Normed{T, f}(x::Normed{T,f}) where {T,f} = x
10-
Normed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
10+
Normed{T, f}(x::AbstractChar) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
1111
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(convert(real(typeof(x)), x))
1212
Normed{T, f}(x::Base.TwicePrecision) where {T,f} = Normed{T, f}(convert(Float64, x))
1313
end
@@ -61,7 +61,7 @@ _convert(::Type{U}, ::Type{UInt128}, x::Float16) where {U <: Normed} =
6161
_convert(U, UInt128, Float32(x))
6262
function _convert(::Type{U}, ::Type{UInt128}, x) where {U <: Normed}
6363
y = round(rawone(U)*x) # for UInt128, we can't widen
64-
(0 <= y) & (y <= typemax(UInt128)) & (x <= Float64(typemax(U))) || throw_converterror(U, x)
64+
(0 <= y) & (y <= typemax(UInt128)) & (x <= convert(Float64, typemax(U))) || throw_converterror(U, x)
6565
U(_unsafe_trunc(UInt128, y), 0)
6666
end
6767

test/fixed.jl

Lines changed: 104 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -49,102 +49,121 @@ function test_fixed(::Type{T}, f) where {T}
4949
end
5050
end
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)
6460
end
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)
69117
end
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)
76123
end
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
83132
end
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
103142
end
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)
128147
end
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
150169
end

0 commit comments

Comments
 (0)