Skip to content

Commit

Permalink
Add test for unsafe_trunc returning arbitrary values (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Apr 29, 2024
1 parent 4b797a0 commit 2604b5a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@ 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.
_to_fixed(::Val, x) = x % Q0f7
_to_fixed(::Val{:Q0f7}, x) = x % Q0f7
_to_fixed(::Val{:Q0f15}, x) = x % Q0f15
buf = IOBuffer()
# in range
for vs in ((:Q0f7, :Q0f15), (:Q0f15, :Q0f7))
for v in vs
show(buf, _to_fixed(Val(v), -1.0))
print(buf, " ")
end
end
issue288_in = String(take!(buf))
# out of range
for vs in ((:Q0f7, :Q0f15), (:Q0f15, :Q0f7))
for v in vs
show(buf, _to_fixed(Val(v), 1.0))
print(buf, " ")
end
end
issue288_out = String(take!(buf))

@testset "issue288" begin
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if issue288_in == expected_issue288 # just leave it in the report
@test issue288_in == expected_issue288
else
@test_broken issue288_in == expected_issue288
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
end
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
if issue288_out == expected_issue288 # just leave it in the report
@test issue288_out == expected_issue288
else
@test_broken issue288_out == expected_issue288
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
end
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
40 changes: 40 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,46 @@ 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.
_to_normed(::Val, x) = x % N0f8
_to_normed(::Val{:N0f8}, x) = x % N0f8
_to_normed(::Val{:N0f16}, x) = x % N0f16
buf = IOBuffer()
# in range
for vs in ((:N0f8, :N0f16), (:N0f16, :N0f8))
for v in vs
show(buf, _to_normed(Val(v), 1.0))
print(buf, " ")
end
end
issue288_in = String(take!(buf))
# out of range
for vs in ((:N0f8, :N0f16), (:N0f16, :N0f8))
for v in vs
show(buf, _to_normed(Val(v), -1.0))
print(buf, " ")
end
end
issue288_out = String(take!(buf))

@testset "issue288" begin
expected_issue288 = "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
if issue288_in == expected_issue288 # just leave it in the report
@test issue288_in == expected_issue288
else
@test_broken issue288_in == expected_issue288
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
end
expected_issue288 = "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
if issue288_out == expected_issue288 # just leave it in the report
@test issue288_out == expected_issue288
else
@test_broken issue288_out == expected_issue288
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
end
end

@testset "domain of f" begin
@test_throws DomainError zero(Normed{UInt8,-1})
@test_throws DomainError zero(Normed{UInt8,0})
Expand Down

0 comments on commit 2604b5a

Please sign in to comment.