@@ -4,6 +4,9 @@ using Random
4
4
isdefined (Main, :OffsetArrays ) || @eval Main include (" testhelpers/OffsetArrays.jl" )
5
5
using . Main. OffsetArrays
6
6
7
+ == ₜ (:: Any , :: Any ) = false
8
+ == ₜ (a:: T , b:: T ) where {T} = isequal (a, b)
9
+
7
10
# fold(l|r) & mapfold(l|r)
8
11
@test foldl (+ , Int64[]) === Int64 (0 ) # In reference to issues #7465/#20144 (PR #20160)
9
12
@test foldl (+ , Int16[]) === Int16 (0 ) # In reference to issues #21536
@@ -172,6 +175,19 @@ for f in (sum3, sum4, sum7, sum8)
172
175
end
173
176
@test typeof (sum (Int8[])) == typeof (sum (Int8[1 ])) == typeof (sum (Int8[1 7 ]))
174
177
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
+
175
191
# check sum(abs, ...) for support of empty collections
176
192
@testset " sum(abs, [])" begin
177
193
@test @inferred (sum (abs, Float64[])) === 0.0
199
215
200
216
@test typeof (prod (Array (trues (10 )))) == Bool
201
217
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
+
202
231
# check type-stability
203
232
prod2 (itr) = invoke (prod, Tuple{Any}, itr)
204
233
@test prod (Int[]) === prod2 (Int[]) === 1
0 commit comments