Skip to content

Commit 6902190

Browse files
Add more expansive test suite
1 parent fc47a39 commit 6902190

File tree

4 files changed

+686
-26
lines changed

4 files changed

+686
-26
lines changed

test/reduce.jl

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,9 @@
7171
# @testset for T in [UInt8, UInt16, UInt32]
7272
# @test vvsum(T[]) === UInt(0)
7373
# end
74-
@testset for T in [Int, Int64, Int128, UInt, UInt64, UInt128,
75-
Float32, Float64]
74+
@testset for T in [Int, Int64, UInt, UInt64, Float32, Float64]
7675
@test vvsum(T[]) === T(0)
7776
end
78-
@test vvsum(BigInt[]) == big(0) && vvsum(BigInt[]) isa BigInt
7977
end
8078

8179
@test vvsum(Bool[]) === vvsum(Bool[false]) === vvsum(Bool[false, false]) === 0
@@ -182,17 +180,6 @@ end
182180
end
183181
end
184182

185-
@testset "maximum works on generic order #30320" begin
186-
for n in [1:20;1500]
187-
arr = randn(n)
188-
@test GenericOrder(maximum(arr)) === maximum(map(GenericOrder, arr))
189-
@test GenericOrder(minimum(arr)) === minimum(map(GenericOrder, arr))
190-
f = x -> x
191-
@test GenericOrder(maximum(f,arr)) === maximum(f,map(GenericOrder, arr))
192-
@test GenericOrder(minimum(f,arr)) === minimum(f,map(GenericOrder, arr))
193-
end
194-
end
195-
196183
@testset "maximum no out of bounds access #30462" begin
197184
arr = fill(-Inf, 128,128)
198185
@test vvmaximum(arr) == -Inf
@@ -295,20 +282,20 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
295282
@test @inferred vall(x->x>0, [4]) == true
296283
@test @inferred vall(x->x>0, [-3, 4, 5]) == false
297284

298-
let f(x) = ifelse(x == 1, true, ifelse(x == 2, false, 1))
299-
@test vany(Any[false,true,false])
300-
@test @inferred vany(map(f, [2,1,2]))
301-
@test @inferred vany([f(x) for x in [2,1,2]])
285+
# let f(x) = ifelse(x == 1, true, ifelse(x == 2, false, 1))
286+
# @test vany(Any[false,true,false])
287+
# @test @inferred vany(map(f, [2,1,2]))
288+
# @test @inferred vany([f(x) for x in [2,1,2]])
302289

303-
@test vall(Any[true,true,true])
304-
@test @inferred vall(map(f, [1,1,1]))
305-
@test @inferred vall([f(x) for x in [1,1,1]])
290+
# @test vall(Any[true,true,true])
291+
# @test @inferred vall(map(f, [1,1,1]))
292+
# @test @inferred vall([f(x) for x in [1,1,1]])
306293

307-
# @test_throws TypeError vany([1,true])
308-
# @test_throws TypeError vall([true,1])
309-
# @test_throws TypeError vany(map(f,[3,1]))
310-
# @test_throws TypeError vall(map(f,[1,3]))
311-
end
294+
# # @test_throws TypeError vany([1,true])
295+
# # @test_throws TypeError vall([true,1])
296+
# # @test_throws TypeError vany(map(f,[3,1]))
297+
# # @test_throws TypeError vall(map(f,[1,3]))
298+
# end
312299

313300

314301
@test vcount(x -> x > 0, Int[]) == vcount(Bool[]) == 0

test/reducedim.jl

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Essentially, a direct reproduction of Base's reducedim.jl tests.
2+
# Some small allowances are made for deviations.
3+
4+
@testset "test reductions over region: $region" for region in Any[
5+
1, 2, 3, 4, 5, (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4),
6+
(1, 2, 3), (1, 3, 4), (2, 3, 4), (1, 2, 3, 4)]
7+
A = rand(3, 4, 5, 6)
8+
9+
@test sum(A, dims=region) vvsum(A, dims=region)
10+
@test prod(A, dims=region) vvprod(A, dims=region)
11+
@test maximum(A, dims=region) vvmaximum(A, dims=region)
12+
@test minimum(A, dims=region) vvminimum(A, dims=region)
13+
14+
@test sum(abs2, A, dims=region) vvsum(abs2, A, dims=region)
15+
@test prod(abs2, A, dims=region) vvprod(abs2, A, dims=region)
16+
@test maximum(abs2, A, dims=region) vvmaximum(abs2, A, dims=region)
17+
@test minimum(abs2, A, dims=region) vvminimum(abs2, A, dims=region)
18+
19+
20+
@test count((0.5), A, dims=region) == vcount((0.5), A, dims=region)
21+
22+
# With init=false
23+
@test sum(A, init=false) vvsum(A, init=false)
24+
@test prod(A, init=false) vvprod(A, init=false)
25+
@test maximum(A, init=false) vvmaximum(A, init=false)
26+
@test minimum(A, init=false) vvminimum(A, init=false)
27+
28+
@test sum(abs2, A, init=false) vvsum(abs2, A, init=false)
29+
@test prod(abs2, A, init=false) vvprod(abs2, A, init=false)
30+
@test maximum(abs2, A, init=false) vvmaximum(abs2, A, init=false)
31+
@test minimum(abs2, A, init=false) vvminimum(abs2, A, init=false)
32+
33+
@test @inferred vvsum(A, dims=region) sum(A, dims=region)
34+
@test @inferred(vvprod(A, dims=region)) prod(A, dims=region)
35+
@test @inferred(vvmaximum(A, dims=region)) maximum(A, dims=region)
36+
@test @inferred(vvminimum(A, dims=region)) minimum(A, dims=region)
37+
38+
@test @inferred(vvsum(abs, A, dims=region)) sum(abs, A, dims=region)
39+
@test @inferred(vvsum(abs2, A, dims=region)) sum(abs2, A, dims=region)
40+
@test @inferred(vvmaximum(abs, A, dims=region)) maximum(abs, A, dims=region)
41+
@test @inferred(vvminimum(abs, A, dims=region)) minimum(abs, A, dims=region)
42+
43+
end
44+
45+
# Small integers
46+
@test @inferred(vvsum(Int8[1], dims=1)) == [1]
47+
@test @inferred(vvsum(UInt8[1], dims=1)) == [1]
48+
49+
A = [1.0 5.0 6.0;
50+
5.0 2.0 4.0]
51+
for (tup, rval, rind) in [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]),
52+
((2,), reshape([1.0,2.0], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,2)], 2, 1)),
53+
((1,2), fill(1.0,1,1),fill(CartesianIndex(1,1),1,1))]
54+
@test vfindmin1(A, dims=tup) == (rval, rind)
55+
@test isequal(vvminimum(A, dims=tup), rval)
56+
end
57+
58+
for (tup, rval, rind) in [((1,), [5.0 5.0 6.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]),
59+
((2,), reshape([6.0,5.0], 2, 1), reshape([CartesianIndex(1,3),CartesianIndex(2,1)], 2, 1)),
60+
((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))]
61+
@test vfindmax1(A, dims=tup) == (rval, rind)
62+
@test isequal(vvmaximum(A, dims=tup), rval)
63+
end
64+
@testset "findmin/findmax transformed arguments, numeric values" begin
65+
A = [1.0 -5.0 -6.0;
66+
-5.0 2.0 4.0]
67+
TA = [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]),
68+
((2,), reshape([1.0, 2.0], 2, 1), reshape([CartesianIndex(1,1), CartesianIndex(2,2)], 2, 1)),
69+
((1,2), fill(1.0,1,1), fill(CartesianIndex(1,1),1,1))]
70+
TA2 = [((1,), [1.0 4.0 16.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]),
71+
((2,), reshape([1.0, 4.0], 2, 1), reshape([CartesianIndex(1,1), CartesianIndex(2,2)], 2, 1)),
72+
((1,2), fill(1.0,1,1), fill(CartesianIndex(1,1),1,1))]
73+
TAc = [((1,), [0.28366218546322625 -0.4161468365471424 -0.6536436208636119], [CartesianIndex(2,1) CartesianIndex(2,2) CartesianIndex(2,3)]),
74+
((2,), reshape([0.28366218546322625, -0.6536436208636119], 2, 1), reshape([CartesianIndex(1,2), CartesianIndex(2,3)], 2, 1)),
75+
((1,2), fill(-0.6536436208636119,1,1), fill(CartesianIndex(2,3),1,1))]
76+
for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc))
77+
A′ = map(f, A)
78+
for (tup, rval, rind) in At
79+
(rval′, rind′) = vfindmin1(f, A, dims=tup)
80+
@test all(rval′ .≈ rval)
81+
@test rind′ == rind
82+
(rval′′, rind′′) = vfindmin1(A′, dims=tup)
83+
@test all(rval′ .≈ rval′′)
84+
@test rind′ == rind′′
85+
end
86+
end
87+
88+
TA = [((1,), [5.0 5.0 6.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]),
89+
((2,), reshape([6.0,5.0], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)),
90+
((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))]
91+
TA2 = [((1,), [25.0 25.0 36.0], [CartesianIndex(2,1) CartesianIndex(1,2) CartesianIndex(1,3)]),
92+
((2,), reshape([36.0, 25.0], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)),
93+
((1,2), fill(36.0,1,1), fill(CartesianIndex(1,3),1,1))]
94+
TAc = [((1,), [0.5403023058681398 0.28366218546322625 0.960170286650366], [CartesianIndex(1,1) CartesianIndex(1,2) CartesianIndex(1,3)]),
95+
((2,), reshape([0.960170286650366, 0.28366218546322625], 2, 1), reshape([CartesianIndex(1,3), CartesianIndex(2,1)], 2, 1)),
96+
((1,2), fill(0.960170286650366,1,1), fill(CartesianIndex(1,3),1,1))]
97+
for (f, At) in ((abs, TA), (abs2, TA2), (cos, TAc))
98+
A′ = map(f, A)
99+
for (tup, rval, rind) in At
100+
(rval′, rind′) = vfindmax1(f, A, dims=tup)
101+
@test all(rval′ .≈ rval)
102+
@test rind′ == rind
103+
(rval′′, rind′′) = vfindmax1(A′, dims=tup)
104+
@test all(rval′ .≈ rval′′)
105+
@test rind′ == rind′′
106+
end
107+
end
108+
end
109+
110+
@testset "NaN in findmin/findmax/minimum/maximum" begin
111+
A = [1.0 NaN 6.0;
112+
NaN 2.0 4.0]
113+
A′ = [-1.0 NaN -6.0;
114+
NaN -2.0 4.0]
115+
for (tup, rval, rind) in [((1,), [1.0 2.0 4.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(2,3)]),
116+
((2,), reshape([1.0, 2.0], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,2)], 2, 1)),
117+
((1,2), fill(1.0,1,1),fill(CartesianIndex(1,1),1,1))]
118+
@test isequal(vfindmin1(A, dims=tup), (rval, rind))
119+
@test isequal(vfindmin1(abs, A′, dims=tup), (rval, rind))
120+
@test isequal(vvminimum(A, dims=tup), rval)
121+
@test isequal(vvminimum(abs, A′, dims=tup), rval)
122+
end
123+
124+
for (tup, rval, rind) in [((1,), [1.0 2.0 6.0], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(1,3)]),
125+
((2,), reshape([6.0, 4.0], 2, 1), reshape([CartesianIndex(1,3),CartesianIndex(2,3)], 2, 1)),
126+
((1,2), fill(6.0,1,1),fill(CartesianIndex(1,3),1,1))]
127+
@test isequal(vfindmax1(A, dims=tup), (rval, rind))
128+
@test isequal(vfindmax1(abs, A′, dims=tup), (rval, rind))
129+
@test isequal(vvmaximum(A, dims=tup), rval)
130+
@test isequal(vvmaximum(abs, A′, dims=tup), rval)
131+
end
132+
end
133+
134+
@testset "+/-Inf in findmin/findmax/minimum/maximum" begin
135+
A = [Inf -Inf Inf -Inf;
136+
Inf Inf -Inf -Inf]
137+
A′ = [1 0 1 0;
138+
1 1 0 0]
139+
retinf(x::T) where {T} = ifelse(x == one(T), Inf, -Inf)
140+
for (tup, rval, rind) in [((1,), [Inf -Inf -Inf -Inf], [CartesianIndex(1,1) CartesianIndex(1,2) CartesianIndex(2,3) CartesianIndex(1,4)]),
141+
((2,), reshape([-Inf -Inf], 2, 1), reshape([CartesianIndex(1,2),CartesianIndex(2,3)], 2, 1)),
142+
((1,2), fill(-Inf,1,1),fill(CartesianIndex(1,2),1,1))]
143+
@test isequal(vfindmin1(A, dims=tup), (rval, rind))
144+
@test isequal(vfindmin1(retinf, A′, dims=tup), (rval, rind))
145+
@test isequal(vvminimum(A, dims=tup), rval)
146+
@test isequal(vvminimum(retinf, A′, dims=tup), rval)
147+
end
148+
149+
for (tup, rval, rind) in [((1,), [Inf Inf Inf -Inf], [CartesianIndex(1,1) CartesianIndex(2,2) CartesianIndex(1,3) CartesianIndex(1,4)]),
150+
((2,), reshape([Inf Inf], 2, 1), reshape([CartesianIndex(1,1),CartesianIndex(2,1)], 2, 1)),
151+
((1,2), fill(Inf,1,1),fill(CartesianIndex(1,1),1,1))]
152+
@test isequal(vfindmax1(A, dims=tup), (rval, rind))
153+
@test isequal(vfindmax1(retinf, A′, dims=tup), (rval, rind))
154+
@test isequal(vvmaximum(A, dims=tup), rval)
155+
@test isequal(vvmaximum(retinf, A′, dims=tup), rval)
156+
end
157+
end
158+
159+
@testset "region=$region" for region in Any[[0, 1], [0 1; 2 3], "hello"]
160+
Areduc = rand(3, 4, 5, 6)
161+
162+
@test_throws MethodError vvsum(Areduc, dims=region)
163+
@test_throws MethodError vvprod(Areduc, dims=region)
164+
@test_throws MethodError vvmaximum(Areduc, dims=region)
165+
@test_throws MethodError vvminimum(Areduc, dims=region)
166+
@test_throws MethodError vvsum(abs, Areduc, dims=region)
167+
@test_throws MethodError vvsum(abs2, Areduc, dims=region)
168+
@test_throws MethodError vvmaximum(abs, Areduc, dims=region)
169+
@test_throws MethodError vvminimum(abs, Areduc, dims=region)
170+
end

0 commit comments

Comments
 (0)