Skip to content

Commit c9c4686

Browse files
NHDalyomus
authored andcommitted
Adds tests for Type Stability of FD functions. (#48)
* 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`.
1 parent 815452a commit c9c4686

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/runtests.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,36 @@ epsi(::Type{T}) where T = eps(T)
786786
end
787787
end
788788

789+
@testset "type stability" begin
790+
# Test that basic operations are type stable for all the basic integer types.
791+
fs = [0, 1, 2, 7, 16, 38] # To save time, don't test all possible combinations.
792+
@testset for T in (CONTAINER_TYPES..., BigInt,)
793+
maxF = FixedPointDecimals.max_exp10(T)
794+
frange = filter(f->f<=maxF, fs)
795+
# Unary operations
796+
@testset for f in frange
797+
@test @inferred(zero(FD{T,f}(1))) === FD{T,f}(0)
798+
@test @inferred(one(FD{T,f}(1))) === FD{T,f}(1)
799+
@test @inferred(ceil(FD{T,f}(1))) === FD{T,f}(1)
800+
@test @inferred(round(FD{T,f}(1))) === FD{T,f}(1)
801+
@test @inferred(abs(FD{T,f}(1))) === FD{T,f}(1)
802+
@test @inferred(FD{T,f}(1)^2) === FD{T,f}(1)
803+
@test @inferred(typemax(FD{T,f})) isa FD{T,f}
804+
end
805+
# Binary operations
806+
@testset for (f1,f2) in Iterators.product(frange, frange)
807+
fmax = max(f1,f2)
808+
@test @inferred(FD{T,f1}(1) + FD{T,f2}(0)) === FD{T,fmax}(1)
809+
@test @inferred(FD{T,f1}(1) - FD{T,f2}(0)) === FD{T,fmax}(1)
810+
@test @inferred(FD{T,f1}(1) * FD{T,f2}(1)) === FD{T,fmax}(1)
811+
@test @inferred(FD{T,f1}(1) / FD{T,f2}(1)) === FD{T,fmax}(1)
812+
@test @inferred(FD{T,f1}(1) ÷ FD{T,f2}(1)) === FD{T,fmax}(1)
813+
@test @inferred(max(FD{T,f1}(1), FD{T,f2}(0))) === FD{T,fmax}(1)
814+
@test @inferred(min(FD{T,f1}(1), FD{T,f2}(0))) === FD{T,fmax}(0)
815+
end
816+
end
817+
end
818+
789819
@testset "print" begin
790820
@test string(FD2(1.00)) == "1.00"
791821
@test string(FD2(1.23)) == "1.23"

0 commit comments

Comments
 (0)