Skip to content

Commit

Permalink
Add test for undefined behavior of unsafe_trunc
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Apr 27, 2024
1 parent 37e83e7 commit 0460bc7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,26 @@ function test_rand(TX::Type)
@test size(a) == (3, 5)
end
end

# 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
@inline _to_normed(::Val, x) = x % N0f8
@inline _to_normed(::Val{:N0f16}, x) = x % N0f16
function _issue288(::Type{Fixed})
buf = IOBuffer()
for sym in (:Q0f7, :Q0f15)
show(buf, _to_fixed(Val(sym), -1.0))
print(buf, " ")
end
return String(take!(buf))
end
function _issue288(::Type{Normed})
buf = IOBuffer()
for sym in (:N0f8, :N0f16)
show(buf, _to_normed(Val(sym), 1.0))
print(buf, " ")
end
return String(take!(buf))
end
8 changes: 8 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ end

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

# issue #288
if _issue288(Fixed) == "-1.0Q0f7 -1.0Q0f15 " # just leave it in the report
@test _issue288(Fixed) == "-1.0Q0f7 -1.0Q0f15 "
else
@test_broken _issue288(Fixed) == "-1.0Q0f7 -1.0Q0f15 "
@warn "broken result: " * repr(_issue288(Fixed))
end
end

@testset "neg" begin
Expand Down
8 changes: 8 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,14 @@ end
# issue #211
@test big"1.2" % N0f8 === 0.196N0f8
@test reinterpret(BigFloat(0x0_01234567_89abcdef) % N63f1) === 0x01234567_89abcdef

# issue #288
if _issue288(Normed) == "1.0N0f8 1.0N0f16 " # just leave it in the report
@test _issue288(Normed) == "1.0N0f8 1.0N0f16 "
else
@test_broken _issue288(Normed) == "1.0N0f8 1.0N0f16 "
@warn "broken result: " * repr(_issue288(Normed))
end
end

@testset "arithmetic" begin
Expand Down

0 comments on commit 0460bc7

Please sign in to comment.