11using CheckedSizeProduct
22using Test
3- using Aqua: Aqua
43
54module ExampleInts
65 export ExampleInt
@@ -20,46 +19,33 @@ module ExampleInts
2019 (p, f) = Base. Checked. mul_with_overflow (l. v, r. v)
2120 (ExampleInt (p), f)
2221 end
23- function Base.:(< )(l:: ExampleInt , r:: Int )
24- l. v < r
25- end
26- function Base. promote_rule (:: Type{Int} , :: Type{ExampleInt} )
27- ExampleInt
28- end
29- function Base. promote_rule (:: Type{ExampleInt} , :: Type{Int} )
30- ExampleInt
31- end
32- function Base. promote_rule (:: Type{Bool} , :: Type{ExampleInt} )
33- ExampleInt
34- end
35- function Base. promote_rule (:: Type{ExampleInt} , :: Type{Bool} )
36- ExampleInt
22+ function Base.:(< )(l:: ExampleInt , r:: ExampleInt )
23+ l. v < r. v
3724 end
3825 function ExampleInt (n)
26+ if n isa ExampleInt
27+ return n
28+ end
3929 ExampleInt (Int (n):: Int )
4030 end
4131 function Base. convert (:: Type{ExampleInt} , n)
4232 ExampleInt (n)
4333 end
44- function Base. convert (:: Type{ExampleInt} , n:: ExampleInt )
45- n
46- end
4734end
4835
4936using . ExampleInts: ExampleInt
5037
38+ const eltypes = (Int8, Int16, Int32, Int64, Int128, ExampleInt, BigInt)
39+
5140@testset " CheckedSizeProduct.jl" begin
52- @testset " Code quality (Aqua.jl)" begin
53- Aqua. test_all (CheckedSizeProduct)
54- end
5541 @testset " empty input" begin
56- @test_throws Exception checked_size_product (())
42+ @test_throws MethodError checked_size_product (())
5743 end
5844 @testset " heterogeneous input" begin
59- @test_throws Exception checked_size_product ((Int32 (2 ), Int64 (3 )))
45+ @test_throws MethodError checked_size_product ((Int32 (2 ), Int64 (3 )))
6046 end
6147 @testset " singleton input" begin
62- for T ∈ (Int8, Int16, Int32, Int64, Int128, ExampleInt)
48+ for T ∈ eltypes
6349 for x ∈ 0 : 100
6450 y = T (x)
6551 @test y === checked_size_product ((y,))
@@ -72,9 +58,9 @@ using .ExampleInts: ExampleInt
7258 (- 1 , typemax (Int)), (typemax (Int), - 1 ),
7359 (0 , - 4 , - 4 ), (- 4 , 1 , 0 ), (- 4 , - 4 , 1 ),
7460 )
75- @test checked_size_product (t) isa CheckedSizeProduct . StatusInvalidValue
61+ @test nothing === checked_size_product (t)
7662 s = map (ExampleInt, t)
77- @test checked_size_product (s) isa CheckedSizeProduct . StatusInvalidValue
63+ @test nothing === checked_size_product (s)
7864 end
7965 end
8066 @testset " input includes `typemax(T)`" begin
@@ -89,9 +75,9 @@ using .ExampleInts: ExampleInt
8975 for t ∈ (
9076 (m,), (m, m), (1 , m), (m, 1 ), (0 , m), (m, 0 ), (- 1 , m), (m, - 1 ),
9177 )
92- @test checked_size_product (t) isa CheckedSizeProduct . StatusInvalidValue
78+ @test nothing === checked_size_product (t)
9379 s = map (ExampleInt, t)
94- @test checked_size_product (s) isa CheckedSizeProduct . StatusInvalidValue
80+ @test nothing === checked_size_product (s)
9581 end
9682 end
9783 @testset " overflows" begin
@@ -101,9 +87,9 @@ using .ExampleInts: ExampleInt
10187 (m, m), (15 , m), (m, 15 ), (m, m, m), (1 , m, m), (m, 1 , m), (m, m, 1 ),
10288 (b, b), (1 , b, b),
10389 )
104- @test checked_size_product (t) isa CheckedSizeProduct . StatusOverflow
90+ @test nothing === checked_size_product (t)
10591 s = map (ExampleInt, t)
106- @test checked_size_product (s) isa CheckedSizeProduct . StatusOverflow
92+ @test nothing === checked_size_product (s)
10793 end
10894 end
10995 @testset " overflows, but OK because of multiplication with zero" begin
@@ -123,20 +109,25 @@ using .ExampleInts: ExampleInt
123109 for x ∈ ran
124110 for y ∈ ran
125111 for z ∈ ran
126- for T ∈ (Int8, Int16, Int32, Int64, Int128, ExampleInt)
112+ for T ∈ eltypes
127113 ref = T (prod ((x, y, z)))
128114 o = T (1 )
129115 a = T (x)
130116 b = T (y)
131117 c = T (z)
132- @test ref === checked_size_product ((a, b, c))
133- @test ref === checked_size_product ((o, a, b, c))
134- @test ref === checked_size_product ((a, o, b, c))
135- @test ref === checked_size_product ((a, b, o, c))
136- @test ref === checked_size_product ((a, b, c, o))
118+ @test ref == checked_size_product ((a, b, c)):: T
119+ @test ref == checked_size_product ((o, a, b, c)):: T
120+ @test ref == checked_size_product ((a, o, b, c)):: T
121+ @test ref == checked_size_product ((a, b, o, c)):: T
122+ @test ref == checked_size_product ((a, b, c, o)):: T
137123 end
138124 end
139125 end
140126 end
141127 end
142128end
129+
130+ using Aqua: Aqua
131+ @testset " Code quality (Aqua.jl)" begin
132+ Aqua. test_all (CheckedSizeProduct)
133+ end
0 commit comments