Skip to content

Update tests for show/summary #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,6 @@ end
end
end

@testset "Show" begin
x = Fixed{Int32,5}(0.25)
iob = IOBuffer()
show(iob, x)
str = String(take!(iob))
@test str == "0.25Q26f5"
@test eval(Meta.parse(str)) == x
end

@testset "rand" begin
for F in (Fixed{Int8,7}, Fixed{Int16,8}, Fixed{Int16,10}, Fixed{Int32,16})
@test isa(rand(F), F)
Expand Down Expand Up @@ -433,3 +424,55 @@ end
@test promote_type(Float32,Q0f7,Int) == Float32
@test promote_type(Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7}
end

@testset "show" begin
iob = IOBuffer()
q0f7 = reinterpret(Q0f7, signed(0xaa))
show(iob, q0f7)
str = String(take!(iob))
@test str == "-0.672Q0f7"
@test eval(Meta.parse(str)) === q0f7

q15f16 = reinterpret(Q15f16, signed(0xaaaaaaaa))
show(iob, q15f16)
str = String(take!(iob))
@test str == "-21845.33334Q15f16"
@test eval(Meta.parse(str)) === q15f16

show(IOContext(iob, :compact=>true), q15f16)
@test String(take!(iob)) == "-21845.3"

show(IOContext(iob, :compact=>true, :typeinfo=>Q15f16), q15f16)
@test String(take!(iob)) == "-21845.3"

show(IOContext(iob, :compact=>true, :typeinfo=>Fixed), q15f16)
@test String(take!(iob)) == "-21845.3"

show(IOContext(iob, :typeinfo=>Q15f16), q15f16)
@test String(take!(iob)) == "-21845.33334Q15f16" # TODO: Consider removing suffix (issue #188)

show(IOContext(iob, :typeinfo=>Normed), q15f16)
@test String(take!(iob)) == "-21845.33334Q15f16"

show(iob, Fixed{Int128,64}(-1.2345e6))
@test_broken String(take!(iob)) == "Fixed{Int128,64}(-1.2345e6)" # "Q63f64" is not defined

# TODO: remove this test
show(iob, reinterpret(Fixed{Int8,8}, signed(0xaa)))
@test_broken String(take!(iob)) == "Fixed{Int8,8}(-0.336)" # "Q-1f8" is invalid
end

@testset "summary" begin
a = Q0f7[0.2, 0.4]
aa = Fixed[0.2Q0f7 0.4Q0f15]

if VERSION >= v"1.6.0-DEV.356"
@test_broken summary(a) == "2-element Vector{Q0f7}"
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{Q0f7}, 1:2) with eltype Q0f7"
@test_broken summary(aa) == "1×2 Matrix{Fixed}"
else
@test summary(a) == "2-element Array{Q0f7,1} with eltype Fixed{Int8,7}"
@test summary(view(a, 1:2)) == "2-element view(::Array{Q0f7,1}, 1:2) with eltype Fixed{Int8,7}"
@test_broken summary(aa) == "1×2 Array{Fixed,2}"
end
end
51 changes: 42 additions & 9 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,51 @@ end
end

@testset "show" begin
x = reinterpret(N0f8, 0xaa)
iob = IOBuffer()
show(iob, x)
n0f8 = reinterpret(N0f8, 0xaa)
show(iob, n0f8)
str = String(take!(iob))
@test str == "0.667N0f8"
@test eval(Meta.parse(str)) == x
@test eval(Meta.parse(str)) === n0f8

n16f16 = reinterpret(N16f16, 0xaaaaaaaa)
show(iob, n16f16)
str = String(take!(iob))
@test str == "43691.33333N16f16"
@test eval(Meta.parse(str)) === n16f16

show(IOContext(iob, :compact=>true), n16f16)
@test String(take!(iob)) == "43691.3"

show(IOContext(iob, :compact=>true, :typeinfo=>N16f16), n16f16)
@test String(take!(iob)) == "43691.3"

show(IOContext(iob, :compact=>true, :typeinfo=>Normed), n16f16)
@test String(take!(iob)) == "43691.3"

show(IOContext(iob, :typeinfo=>N16f16), n16f16)
@test String(take!(iob)) == "43691.33333N16f16" # TODO: Consider removing suffix (issue #188)

show(IOContext(iob, :typeinfo=>Normed), n16f16)
@test String(take!(iob)) == "43691.33333N16f16"

show(iob, Normed{UInt128,64}(1.2345e6))
@test_broken String(take!(iob)) == "Normed{UInt128,64}(1.2345e6)" # "N64f64" is not defined
end

@testset "summary" begin
a = N0f8[0.2, 0.4]
aa = Normed[0.2N0f8 0.4N0f16]

if VERSION >= v"1.6.0-DEV.356"
@test_broken summary(a) == "2-element Vector{N0f8}"
@test_broken summary(view(a, 1:2)) == "2-element view(::Vector{N0f8}, 1:2) with eltype N0f8"
@test_broken summary(aa) == "1×2 Matrix{Normed}"
else
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
@test_broken summary(aa) == "1×2 Array{Normed,2}"
end
end

@testset "scaledual" begin
Expand Down Expand Up @@ -478,12 +517,6 @@ end
@test Float16(1.0) % N0f16 === N0f16(1.0)
end

@testset "summary" begin
a = N0f8[0.2, 0.4]
@test summary(a) == "2-element Array{N0f8,1} with eltype Normed{UInt8,8}"
@test summary(view(a, 1:2)) == "2-element view(::Array{N0f8,1}, 1:2) with eltype Normed{UInt8,8}"
end

@testset "disambiguation constructors" begin
@test_throws ArgumentError Normed{UInt32,16}('a')
@test_throws InexactError Normed{UInt32,16}(complex(1.0, 1.0))
Expand Down