Skip to content

Commit d619b9a

Browse files
committed
Add correctness tests for 128-bit corner cases
Perf tests next
1 parent 0e04214 commit d619b9a

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test/runtests.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using Printf
55
using Base.Checked: checked_mul
66
using Parsers
77

8+
Base.Experimental.@optlevel 2 # For perf tests
9+
810
pkg_path = pkgdir(FixedPointDecimals)
911
include(joinpath(pkg_path, "test", "utils.jl"))
1012

@@ -16,6 +18,8 @@ const FD3 = FixedDecimal{Int, 3}
1618
const FD4 = FixedDecimal{Int, 4}
1719
const WFD2 = FixedDecimal{Int128, 2}
1820
const WFD4 = FixedDecimal{Int128, 4}
21+
const UFD2 = FixedDecimal{UInt, 2}
22+
const UWFD2 = FixedDecimal{UInt128, 2}
1923

2024
const CONTAINER_TYPES = Base.BitInteger_types # Integer concrete subtypes which are bits
2125

@@ -262,6 +266,59 @@ end
262266
end
263267
end
264268

269+
@testset "128-bit conversion correctness" begin
270+
# TODO: Negatives
271+
@testset "Convert from 64-bit to 128-bit" begin
272+
@test Base.convert(WFD2, 1).i === Int128(100)
273+
@test Base.convert(UWFD2, 1).i === UInt128(100)
274+
@test Base.convert(WFD2, -1).i === Int128(-100)
275+
@test_throws InexactError(:convert, UWFD2, -1) Base.convert(UWFD2, -1)
276+
@test Base.convert(WFD2, UInt(1)).i === Int128(100)
277+
@test Base.convert(UWFD2, UInt(1)).i === UInt128(100)
278+
@test Base.convert(WFD2, typemax(Int)).i === Int128(922337203685477580700)
279+
@test Base.convert(UWFD2, typemax(Int)).i === UInt128(922337203685477580700)
280+
@test Base.convert(WFD2, typemin(Int)).i === Int128(-922337203685477580800)
281+
@test_throws InexactError(:convert, UWFD2, typemin(Int)) Base.convert(UWFD2, typemin(Int))
282+
@test Base.convert(WFD2, typemax(UInt)).i === Int128(1844674407370955161500)
283+
@test Base.convert(UWFD2, typemax(UInt)).i === UInt128(1844674407370955161500)
284+
end
285+
286+
@testset "Convert from 128-bit to 128-bit" begin
287+
@test Base.convert(WFD2, Int128(1)).i === Int128(100)
288+
@test Base.convert(UWFD2, Int128(1)).i === UInt128(100)
289+
@test Base.convert(WFD2, UInt128(1)).i === Int128(100)
290+
@test Base.convert(UWFD2, UInt128(1)).i === UInt128(100)
291+
@test Base.convert(WFD2, Int128(-1)).i === Int128(-100)
292+
@test_throws InexactError(:convert, UWFD2, Int128(-1)) Base.convert(UWFD2, Int128(-1))
293+
@test_throws InexactError(:convert, WFD2, typemax(Int128)) Base.convert(WFD2, typemax(Int128))
294+
@test_throws InexactError(:convert, UWFD2, typemax(Int128)) Base.convert(UWFD2, typemax(Int128))
295+
@test_throws InexactError(:convert, WFD2, typemin(Int128)) Base.convert(WFD2, typemin(Int128))
296+
@test_throws InexactError(:convert, UWFD2, typemin(Int128)) Base.convert(UWFD2, typemin(Int128))
297+
@test_throws InexactError(:convert, WFD2, typemax(UInt128)) Base.convert(WFD2, typemax(UInt128))
298+
@test_throws InexactError(:convert, UWFD2, typemax(UInt128)) Base.convert(UWFD2, typemax(UInt128))
299+
end
300+
301+
@testset "Convert from 128-bit to 64-bit" begin
302+
@test Base.convert(FD2, Int128(1)).i === Int(100)
303+
@test Base.convert(UFD2, Int128(1)).i === UInt(100)
304+
@test Base.convert(FD2, UInt128(1)).i === Int(100)
305+
@test Base.convert(UFD2, UInt128(1)).i === UInt(100)
306+
@test Base.convert(FD2, Int128(-1)).i === Int(-100)
307+
@test_throws InexactError(:convert, UFD2, Int128(-1)) Base.convert(UFD2, Int128(-1))
308+
@test_throws InexactError(:convert, FD2, typemax(Int128)) Base.convert(FD2, typemax(Int128))
309+
@test_throws InexactError(:convert, UFD2, typemax(Int128)) Base.convert(UFD2, typemax(Int128))
310+
@test_throws InexactError(:convert, FD2, typemin(Int128)) Base.convert(FD2, typemin(Int128))
311+
@test_throws InexactError(:convert, UFD2, typemin(Int128)) Base.convert(UFD2, typemin(Int128))
312+
@test_throws InexactError(:convert, FD2, typemax(UInt128)) Base.convert(FD2, typemax(UInt128))
313+
@test_throws InexactError(:convert, UFD2, typemax(UInt128)) Base.convert(UFD2, typemax(UInt128))
314+
end
315+
end
316+
@testset "128-bit conversion performance" begin
317+
#@test Base.convert(WFD2, 1) == WFD2(1)
318+
#@test @allocated(Base.convert(WFD2, 1)) == 0
319+
end
320+
321+
265322
@testset "promotion" begin
266323
@test 1//10 + FD2(0.1) === 1//5
267324
@test 0.1 + FD2(0.1) === 0.2

0 commit comments

Comments
 (0)