Skip to content

Commit

Permalink
Fix complex constructors
Browse files Browse the repository at this point in the history
Add some tests
  • Loading branch information
andreasnoack committed Feb 1, 2018
1 parent fe45399 commit 91b2f53
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct Fixed{T <: Signed,f} <: FixedPoint{T, f}
Fixed{T, f}(x) where {T,f} = convert(Fixed{T,f}, x)
Fixed{T, f}(x::Fixed{T,f}) where {T,f} = x
Fixed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Fixed cannot be constructed from a Char"))
Fixed{T, f}(x::Complex) where {T,f} = Fixed{T, f}(real(x))
Fixed{T, f}(x::Base.TwicePrecision) where {T,f,S} = Fixed{T, f}(convert(Float64, x))
Fixed{T, f}(x::Complex) where {T,f} = Fixed{T, f}(convert(real(typeof(x)), x))
Fixed{T, f}(x::Base.TwicePrecision) where {T,f} = Fixed{T, f}(convert(Float64, x))
end

reinterpret(::Type{Fixed{T,f}}, x::T) where {T <: Signed,f} = Fixed{T,f}(x, 0)
Expand Down
4 changes: 2 additions & 2 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct Normed{T<:Unsigned,f} <: FixedPoint{T,f}
Normed{T, f}(x) where {T,f} = convert(Normed{T,f}, x)
Normed{T, f}(x::Normed{T,f}) where {T,f} = x
Normed{T, f}(x::Char) where {T,f} = throw(ArgumentError("Normed cannot be constructed from a Char"))
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(real(x))
Normed{T, f}(x::Base.TwicePrecision) where {T,f,S} = Normed{T, f}(convert(Float64, x))
Normed{T, f}(x::Complex) where {T,f} = Normed{T, f}(convert(real(typeof(x)), x))
Normed{T, f}(x::Base.TwicePrecision) where {T,f} = Normed{T, f}(convert(Float64, x))
end

typechar(::Type{X}) where {X <: Normed} = 'N'
Expand Down
8 changes: 7 additions & 1 deletion test/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Compat, Test
using FixedPointNumbers, Compat.Test

function test_op(fun::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
# Make sure that the result is representable
Expand Down Expand Up @@ -129,3 +129,9 @@ end

# issue #79
@test realmin(Q11f4) == Q11f4(0.06)

# Test disambiguation constructors
@test_throws ArgumentError Fixed{Int32,16}('a')
@test_throws InexactError Fixed{Int32,16}(complex(1.0, 1.0))
@test Fixed{Int32,16}(complex(1.0, 0.0)) == 1
@test Fixed{Int32,16}(Base.TwicePrecision(1.0)) == 1
9 changes: 8 additions & 1 deletion test/normed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Compat, Test
using FixedPointNumbers, Compat.Test

@test reinterpret(N0f8, 0xa2).i === 0xa2
@test reinterpret(N6f10, 0x1fa2).i === 0x1fa2
Expand Down Expand Up @@ -313,3 +313,10 @@ elseif VERSION >= v"0.7.0-DEV.1790"
@test summary(a) == "2-element Array{N0f8,1} with eltype FixedPointNumbers.Normed{UInt8,8}"
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype FixedPointNumbers.Normed{UInt8,8}"
end

# Test disambiguation constructors
@test_throws ArgumentError Normed{UInt32,16}('a')
@test_throws InexactError Normed{UInt32,16}(complex(1.0, 1.0))
@test Normed{UInt32,16}(complex(1.0, 0.0)) == 1
@test Normed{UInt32,16}(Base.TwicePrecision(1.0)) == 1

2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FixedPointNumbers, Compat, Test
using FixedPointNumbers, Compat.Test

@test isempty(detect_ambiguities(FixedPointNumbers, Base, Core))

Expand Down

0 comments on commit 91b2f53

Please sign in to comment.