From 0460bc78ef4c1887d2e016dc85c598803dbcc51f Mon Sep 17 00:00:00 2001 From: kimikage Date: Sat, 27 Apr 2024 10:57:33 +0900 Subject: [PATCH] Add test for undefined behavior of `unsafe_trunc` --- test/common.jl | 23 +++++++++++++++++++++++ test/fixed.jl | 8 ++++++++ test/normed.jl | 8 ++++++++ 3 files changed, 39 insertions(+) diff --git a/test/common.jl b/test/common.jl index fdd4cf7b..0ec76b1e 100644 --- a/test/common.jl +++ b/test/common.jl @@ -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 diff --git a/test/fixed.jl b/test/fixed.jl index 8fbb1bf8..9a6866a1 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -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 diff --git a/test/normed.jl b/test/normed.jl index 4678182c..354ce205 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -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