Skip to content

Commit

Permalink
Adds tests for Type Stability of FD functions. (#48)
Browse files Browse the repository at this point in the history
* Adds tests for Type Stability of FD functions.

Test that most of the API functions for FixedDecimals are type stable in
their return type, using the `@inferred` test macro.

Tests this for all built-in integer types, across many precisions.

(Adds around 10 seconds to test time.)

* Improve instability tests of return type: === && isa

Make all the inferred tests actually test the return type is the correct
type by comparing `===` instead of `==`.
Make the unary test `@inferred(typemax(FD{T,f}))` test that the return
type is the correct type via `isa`.
  • Loading branch information
NHDaly authored and omus committed Feb 15, 2019
1 parent 815452a commit c9c4686
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,36 @@ epsi(::Type{T}) where T = eps(T)
end
end

@testset "type stability" begin
# Test that basic operations are type stable for all the basic integer types.
fs = [0, 1, 2, 7, 16, 38] # To save time, don't test all possible combinations.
@testset for T in (CONTAINER_TYPES..., BigInt,)
maxF = FixedPointDecimals.max_exp10(T)
frange = filter(f->f<=maxF, fs)
# Unary operations
@testset for f in frange
@test @inferred(zero(FD{T,f}(1))) === FD{T,f}(0)
@test @inferred(one(FD{T,f}(1))) === FD{T,f}(1)
@test @inferred(ceil(FD{T,f}(1))) === FD{T,f}(1)
@test @inferred(round(FD{T,f}(1))) === FD{T,f}(1)
@test @inferred(abs(FD{T,f}(1))) === FD{T,f}(1)
@test @inferred(FD{T,f}(1)^2) === FD{T,f}(1)
@test @inferred(typemax(FD{T,f})) isa FD{T,f}
end
# Binary operations
@testset for (f1,f2) in Iterators.product(frange, frange)
fmax = max(f1,f2)
@test @inferred(FD{T,f1}(1) + FD{T,f2}(0)) === FD{T,fmax}(1)
@test @inferred(FD{T,f1}(1) - FD{T,f2}(0)) === FD{T,fmax}(1)
@test @inferred(FD{T,f1}(1) * FD{T,f2}(1)) === FD{T,fmax}(1)
@test @inferred(FD{T,f1}(1) / FD{T,f2}(1)) === FD{T,fmax}(1)
@test @inferred(FD{T,f1}(1) ÷ FD{T,f2}(1)) === FD{T,fmax}(1)
@test @inferred(max(FD{T,f1}(1), FD{T,f2}(0))) === FD{T,fmax}(1)
@test @inferred(min(FD{T,f1}(1), FD{T,f2}(0))) === FD{T,fmax}(0)
end
end
end

@testset "print" begin
@test string(FD2(1.00)) == "1.00"
@test string(FD2(1.23)) == "1.23"
Expand Down

0 comments on commit c9c4686

Please sign in to comment.