Skip to content

Commit 94fefa1

Browse files
committed
Add test for unsafe_trunc returning arbitrary values (#289)
1 parent 3181022 commit 94fefa1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

test/fixed.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,46 @@ function symbol_to_inttype(::Type{Fixed}, s::Symbol)
77
d[s]
88
end
99

10+
# issue #288
11+
# The following needs to be outside of `@testset` to reproduce the issue.
12+
_to_fixed(::Val, x) = x % Q0f7
13+
_to_fixed(::Val{:Q0f7}, x) = x % Q0f7
14+
_to_fixed(::Val{:Q0f15}, x) = x % Q0f15
15+
buf = IOBuffer()
16+
# in range
17+
for vs in ((:Q0f7, :Q0f15), (:Q0f15, :Q0f7))
18+
for v in vs
19+
show(buf, _to_fixed(Val(v), -1.0))
20+
print(buf, " ")
21+
end
22+
end
23+
issue288_in = String(take!(buf))
24+
# out of range
25+
for vs in ((:Q0f7, :Q0f15), (:Q0f15, :Q0f7))
26+
for v in vs
27+
show(buf, _to_fixed(Val(v), 1.0))
28+
print(buf, " ")
29+
end
30+
end
31+
issue288_out = String(take!(buf))
32+
33+
@testset "issue288" begin
34+
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
35+
if issue288_in == expected_issue288 # just leave it in the report
36+
@test issue288_in == expected_issue288
37+
else
38+
@test_broken issue288_in == expected_issue288
39+
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
40+
end
41+
expected_issue288 = "-1.0Q0f7 -1.0Q0f15 -1.0Q0f15 -1.0Q0f7 "
42+
if issue288_out == expected_issue288 # just leave it in the report
43+
@test issue288_out == expected_issue288
44+
else
45+
@test_broken issue288_out == expected_issue288
46+
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
47+
end
48+
end
49+
1050
function test_op(fun::Fun, fx::F, fy::F, fxf, fyf, tol) where {Fun, F}
1151
# Make sure that the result is representable
1252
zf = fun(fxf, fyf)

test/normed.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,46 @@ function symbol_to_inttype(::Type{Normed}, s::Symbol)
77
d[s]
88
end
99

10+
# issue #288
11+
# The following needs to be outside of `@testset` to reproduce the issue.
12+
_to_normed(::Val, x) = x % N0f8
13+
_to_normed(::Val{:N0f8}, x) = x % N0f8
14+
_to_normed(::Val{:N0f16}, x) = x % N0f16
15+
buf = IOBuffer()
16+
# in range
17+
for vs in ((:N0f8, :N0f16), (:N0f16, :N0f8))
18+
for v in vs
19+
show(buf, _to_normed(Val(v), 1.0))
20+
print(buf, " ")
21+
end
22+
end
23+
issue288_in = String(take!(buf))
24+
# out of range
25+
for vs in ((:N0f8, :N0f16), (:N0f16, :N0f8))
26+
for v in vs
27+
show(buf, _to_normed(Val(v), -1.0))
28+
print(buf, " ")
29+
end
30+
end
31+
issue288_out = String(take!(buf))
32+
33+
@testset "issue288" begin
34+
expected_issue288 = "1.0N0f8 1.0N0f16 1.0N0f16 1.0N0f8 "
35+
if issue288_in == expected_issue288 # just leave it in the report
36+
@test issue288_in == expected_issue288
37+
else
38+
@test_broken issue288_in == expected_issue288
39+
@warn """broken: "$issue288_in"\nexpected: "$expected_issue288" """
40+
end
41+
expected_issue288 = "0.004N0f8 2.0e-5N0f16 2.0e-5N0f16 0.004N0f8 "
42+
if issue288_out == expected_issue288 # just leave it in the report
43+
@test issue288_out == expected_issue288
44+
else
45+
@test_broken issue288_out == expected_issue288
46+
@warn """broken: "$issue288_out"\nexpected: "$expected_issue288" """
47+
end
48+
end
49+
1050
@testset "domain of f" begin
1151
@test_throws DomainError zero(Normed{UInt8,-1})
1252
@test_throws DomainError zero(Normed{UInt8,0})

0 commit comments

Comments
 (0)