Skip to content

Commit 72d01c0

Browse files
committed
Test sum and prod
1 parent 16b13d2 commit 72d01c0

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

test/reduce.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ using Random
44
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
55
using .Main.OffsetArrays
66

7+
==(::Any, ::Any) = false
8+
==(a::T, b::T) where {T} = isequal(a, b)
9+
710
# fold(l|r) & mapfold(l|r)
811
@test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160)
912
@test foldl(+, Int16[]) === Int16(0) # In reference to issues #21536
@@ -172,6 +175,19 @@ for f in (sum3, sum4, sum7, sum8)
172175
end
173176
@test typeof(sum(Int8[])) == typeof(sum(Int8[1])) == typeof(sum(Int8[1 7]))
174177

178+
@testset "`sum` of empty collections with `init`" begin
179+
@testset for init in [0, 0.0]
180+
@test sum([]; init = init) === init
181+
@test sum((x for x in [123] if false); init = init) === init
182+
@test sum(nothing, []; init = init) === init
183+
@test sum(nothing, (x for x in [123] if false); init = init) === init
184+
@test sum(Array{Any,3}(undef, 3, 2, 0); dims = 1, init = init) ==
185+
zeros(typeof(init), 1, 2, 0)
186+
@test sum(nothing, Array{Any,3}(undef, 3, 2, 0); dims = 1, init = init) ==
187+
zeros(typeof(init), 1, 2, 0)
188+
end
189+
end
190+
175191
# check sum(abs, ...) for support of empty collections
176192
@testset "sum(abs, [])" begin
177193
@test @inferred(sum(abs, Float64[])) === 0.0
@@ -199,6 +215,19 @@ end
199215

200216
@test typeof(prod(Array(trues(10)))) == Bool
201217

218+
@testset "`prod` of empty collections with `init`" begin
219+
@testset for init in [1, 1.0, ""]
220+
@test prod([]; init = init) === init
221+
@test prod((x for x in [123] if false); init = init) === init
222+
@test prod(nothing, []; init = init) === init
223+
@test prod(nothing, (x for x in [123] if false); init = init) === init
224+
@test prod(Array{Any,3}(undef, 3, 2, 0); dims = 1, init = init) ==
225+
ones(typeof(init), 1, 2, 0)
226+
@test prod(nothing, Array{Any,3}(undef, 3, 2, 0); dims = 1, init = init) ==
227+
ones(typeof(init), 1, 2, 0)
228+
end
229+
end
230+
202231
# check type-stability
203232
prod2(itr) = invoke(prod, Tuple{Any}, itr)
204233
@test prod(Int[]) === prod2(Int[]) === 1

0 commit comments

Comments
 (0)