Skip to content

Commit

Permalink
Update tests (#300)
Browse files Browse the repository at this point in the history
* Backport "Fix test for exponentially growing `promote_type` (#280)"

* fix tests on 1.7 (#253)

Co-authored-by: Johnny Chen <[email protected]>
Co-authored-by: kimikage <[email protected]>

* Mark `test_broken` for conversion to `Float16`

* Backport "Add test for `unsafe_trunc` returning arbitrary values (#289)"

---------

Co-authored-by: Kristoffer Carlsson <[email protected]>
Co-authored-by: Johnny Chen <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 7310e7b commit a995a9d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 4 deletions.
55 changes: 53 additions & 2 deletions test/fixed.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers: bitwidth

# 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::F, ::Type{T}, fx, fy, fxf, fyf, tol) where {F,T}
# Make sure that the result is representable
(typemin(T) <= fun(fxf, fyf) <= typemax(T)) || return nothing
Expand Down Expand Up @@ -228,7 +268,7 @@ end
@test length(r) == 256
QInt1 = Fixed{Int,1}
@test length(QInt1(0):eps(QInt1):typemax(QInt1)-eps(QInt1)) == typemax(Int)
@test Base.unsafe_length(typemin(QInt1):eps(QInt1):typemax(QInt1)-eps(QInt1)) == -1
@test_throws OverflowError length(typemin(QInt1):eps(QInt1):typemax(QInt1)-eps(QInt1))
@test_throws OverflowError length(QInt1(-1):eps(QInt1):typemax(QInt1)-eps(QInt1))
end

Expand Down Expand Up @@ -311,6 +351,10 @@ end
f_actual = Tf(reinterpret(F, i))
float_err += abs(f_actual - f_expected)
end
if float_err != 0.0 # FIXME
@test_broken float_err == 0.0
continue
end
@test float_err == 0.0
end
end
Expand Down Expand Up @@ -459,7 +503,14 @@ end
@test promote_type(Int,Float32,Q0f7) == Float32
@test promote_type(Float32,Int,Q0f7) == Float32
@test promote_type(Float32,Q0f7,Int) == Float32
@test promote_type(Q0f7,Q1f6,Q2f5,Q3f4,Q4f3,Q5f2) == Fixed{Int128,7}

if promote_type(Int, Float32, Complex{Int}, typeof(pi)) === ComplexF64
# right-to-left
@test @inferred(promote_type(Q0f7, Q1f6, Q2f5, Q3f4, Q4f3, Q5f2)) == Fixed{Int128,7}
else
# left-to-right
@test @inferred(promote_type(Q5f2, Q4f3, Q3f4, Q2f5, Q1f6, Q0f7)) == Fixed{Int128,7}
end
end

@testset "show" begin
Expand Down
54 changes: 52 additions & 2 deletions test/normed.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
using FixedPointNumbers, Statistics, Test
using FixedPointNumbers: bitwidth

# 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 Expand Up @@ -195,6 +235,10 @@ end
f_actual = Tf(reinterpret(N, i))
float_err += abs(f_actual - f_expected)
end
if float_err != 0.0 # FIXME
@test_broken float_err == 0.0
continue
end
@test float_err == 0.0
end
end
Expand Down Expand Up @@ -377,7 +421,6 @@ end
NInt1 = Normed{UInt,1}
@test length(NInt1(0):typemax(NInt1)-oneunit(NInt1)) == typemax(UInt)
@test_throws OverflowError length(NInt1(0):typemax(NInt1))
@test Base.unsafe_length(NInt1(0):typemax(NInt1)) == 0 # overflow
N64f64 = Normed{UInt128,64}
@test_broken length(N64f64(0):typemax(N64f64)) == UInt128(typemax(UInt64)) + 1
@test length(N1f63(2):N1f63(0)) == 0
Expand Down Expand Up @@ -446,7 +489,14 @@ end
@test promote_type(Int,Float32,N0f8) == Float32
@test promote_type(Float32,Int,N0f8) == Float32
@test promote_type(Float32,N0f8,Int) == Float32
@test promote_type(N0f8,N1f7,N2f6,N3f5,N4f4,N5f3) == Normed{UInt128,8}

if promote_type(Int, Float32, Complex{Int}, typeof(pi)) === ComplexF64
# right-to-left
@test @inferred(promote_type(N0f8, N1f7, N2f6, N3f5, N4f4, N5f3)) === Normed{UInt128,8}
else
# left-to-right
@test @inferred(promote_type(N5f3, N4f4, N3f5, N2f6, N1f7, N0f8)) === Normed{UInt128,8}
end
end

@testset "show" begin
Expand Down

0 comments on commit a995a9d

Please sign in to comment.