Skip to content

Commit b3a06b8

Browse files
committed
Generalize some print tests
1 parent d42dd35 commit b3a06b8

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

test/runtests.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const FD4 = FixedDecimal{Int, 4}
1515
const WFD2 = FixedDecimal{Int128, 2}
1616
const WFD4 = FixedDecimal{Int128, 4}
1717

18+
const CONTAINER_TYPES = [subtypes(Signed); subtypes(Unsigned)]
19+
1820
# these arrays should be kept sorted manually
1921
const keyvalues = Dict(
2022
FD2 => [typemin(FD2), # near minimum range
@@ -68,10 +70,9 @@ end
6870
# ensure that the coefficient multiplied by the highest and lowest representable values of
6971
# the container type do not result in overflow.
7072
@testset "coefficient" begin
71-
container_types = [Int8, Int16, Int32, Int64, Int128]
72-
for (i, T) in enumerate(container_types)
73-
for j in i:length(container_types)
74-
f = FixedPointDecimals.max_exp10(container_types[j])
73+
for (i, T) in enumerate(CONTAINER_TYPES)
74+
for j in i:length(CONTAINER_TYPES)
75+
f = FixedPointDecimals.max_exp10(CONTAINER_TYPES[j])
7576
powt = FixedPointDecimals.coefficient(FD{T, f}(0))
7677
@test checked_mul(powt, typemax(T)) == powt * typemax(T)
7778
@test checked_mul(powt, typemin(T)) == powt * typemin(T)
@@ -441,10 +442,16 @@ end
441442
@test string(FixedDecimal{Int,0}(123.4)) == "123"
442443

443444
# Displaying a decimal could be incorrect when using a decimal place precision which is
444-
# close to or at the limit the limit for our storage type. Int32 for example can only
445-
# store up to 10 digits of precision.
446-
@test string(reinterpret(FD{Int32,10}, typemax(Int32))) == "0.2147483647"
447-
@test string(reinterpret(FD{Int32,10}, typemin(Int32))) == "-0.2147483648"
445+
# close to or at the limit for our storage type.
446+
for T in CONTAINER_TYPES
447+
f = FixedPointDecimals.max_exp10(T) + 1
448+
max_str = "0." * rpad(typemax(T), f, '0')
449+
min_str = (typemin(T) < 0 ? "-" : "") * "0." * rpad(abs(widen(typemin(T))), f, '0')
450+
@eval begin
451+
@test string(reinterpret(FD{$T,$f}, typemax($T))) == $max_str
452+
@test string(reinterpret(FD{$T,$f}, typemin($T))) == $min_str
453+
end
454+
end
448455
end
449456

450457
@testset "show" begin

0 commit comments

Comments
 (0)