Skip to content

Commit

Permalink
Add test for unsafe_trunc returning arbitrary values
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Apr 28, 2024
1 parent 37e83e7 commit 307b750
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
36 changes: 36 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ function symbol_to_inttype(::Type{Fixed}, s::Symbol)
d[s]
end

# issue #288
# The following needs to be outside of `@testset` to reproduce the issue.
# issue #288
# The following needs to be outside of `@testset` to reproduce the issue.
@inline _to_fixed(::Val, x) = x % Q0f7
@inline _to_fixed(::Val{:Q0f15}, x) = x % Q0f15
function _issue288(::Type{Fixed}, x)
buf = IOBuffer()
for sym in (:Q0f7, :Q0f15)
show(buf, _to_fixed(Val(sym), x))
print(buf, " ")
end
for sym in (:Q0f15, :Q0f7)
show(buf, _to_fixed(Val(sym), x))
print(buf, " ")
end
return String(take!(buf))
end

function test_op(fun::Fun, fx::F, fy::F, fxf, fyf, tol) where {Fun, F}
# Make sure that the result is representable
zf = fun(fxf, fyf)
Expand Down Expand Up @@ -282,6 +301,23 @@ end

@test -1 % Q0f7 === Q0f7(-1)
@test -2 % Q0f7 === Q0f7(0)

# issue #288
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if _issue288(Fixed, -1.0) == expected_issue288 # just leave it in the report
@test _issue288(Fixed, -1.0) == expected_issue288
else
@test_broken _issue288(Fixed, -1.0) == expected_issue288
@warn """broken: "$(_issue288(Fixed, -1.0))"\nexpected: "$expected_issue288" """
end
# out of range
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if _issue288(Fixed, 1.0) == expected_issue288 # just leave it in the report
@test _issue288(Fixed, 1.0) == expected_issue288
else
@test_broken _issue288(Fixed, 1.0) == expected_issue288
@warn """broken: "$(_issue288(Fixed, 1.0))"\nexpected: "$expected_issue288" """
end
end

@testset "neg" begin
Expand Down
36 changes: 36 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ function symbol_to_inttype(::Type{Normed}, s::Symbol)
d[s]
end

# issue #288
# The following needs to be outside of `@testset` to reproduce the issue.
@inline _to_normed(::Val, x) = x % N0f8
@inline _to_normed(::Val{:N0f16}, x) = x % N0f16
function _issue288(::Type{Normed}, x)
buf = IOBuffer()
for sym in (:N0f8, :N0f16)
print(buf, _to_normed(Val(sym), -1.0))
print(buf, " ")
end
#=
for sym in (:N0f16, :N0f8)
show(buf, _to_normed(Val(sym), -1.0))
print(buf, " ")
end
=#
return String(take!(buf))
end

@testset "domain of f" begin
@test_throws DomainError zero(Normed{UInt8,-1})
@test_throws DomainError zero(Normed{UInt8,0})
Expand Down Expand Up @@ -279,6 +298,23 @@ end
# issue #211
@test big"1.2" % N0f8 === 0.196N0f8
@test reinterpret(BigFloat(0x0_01234567_89abcdef) % N63f1) === 0x01234567_89abcdef

# issue #288
expected_issue288 = "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
if _issue288(Normed, 1.0) == expected_issue288 # just leave it in the report
@test _issue288(Normed, 1.0) == expected_issue288
else
@test_broken _issue288(Normed, 1.0) == expected_issue288
@warn """broken: "$(_issue288(Normed, 1.0))"\nexpected: "$expected_issue288" """
end
# out of range
expected_issue288 = "0.502N0f8 0.50001N0f16 0.50001N0f16 0.502N0f8 "
if _issue288(Normed, -0.5) == expected_issue288 # just leave it in the report
@test _issue288(Normed, -0.5) == expected_issue288
else
@test_broken _issue288(Normed, -0.5) == expected_issue288
@warn """broken: "$(_issue288(Normed, -0.5))"\nexpected: "$expected_issue288" """
end
end

@testset "arithmetic" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using FixedPointNumbers, Test, Aqua

@info Sys.ARCH
Aqua.test_all(FixedPointNumbers)

if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
Expand Down

0 comments on commit 307b750

Please sign in to comment.