@@ -58,6 +58,44 @@ using Statistics: mean
58
58
@test mapfoldl (- , + , sv1; init= 0 ) === mapfoldl (- , + , v1, init= 0 )
59
59
end
60
60
61
+ @testset " empty array" begin
62
+ # issue #778
63
+ @test iszero (SVector {0,Int} ())
64
+
65
+ @testset " $fold " for fold in [reduce, foldl]
66
+ @test fold (+ , SVector {0,Bool} ()) === 0
67
+ @test fold (nothing , SVector {0,Int} (), init= :INIT ) === :INIT
68
+ end
69
+
70
+ @testset " $mapfold " for mapfold in [mapreduce, mapfoldl]
71
+ @test mapfold (identity, + , SVector {0,Bool} ()) === 0
72
+ @test mapfold (abs, + , SVector {0,Bool} ()) === 0
73
+ @test mapfold (nothing , nothing , SVector {0,Int} (), init= :INIT ) === :INIT
74
+ end
75
+
76
+ @test mapreduce (
77
+ (a, b) -> a + b,
78
+ (a, b) -> a * b,
79
+ SVector {0,Int} (),
80
+ SVector {0,Int} ();
81
+ init = :INIT ,
82
+ ) == :INIT
83
+
84
+ # When there are multiple inputs, the error is thrown by
85
+ # StaticArrays.jl:
86
+ @test_throws (
87
+ ArgumentError (" reducing over an empty collection is not allowed" ),
88
+ mapreduce ((a, b) -> a + b, (a, b) -> a * b, SVector {0,Int} (), SVector {0,Int} ())
89
+ )
90
+
91
+ # When the mapping and/or reducing functions are unsupported,
92
+ # the error is thrown by `Base.mapreduce_empty`:
93
+ @test_throws (
94
+ ArgumentError (" reducing over an empty collection is not allowed" ),
95
+ mapreduce (nothing , nothing , SVector {0,Int} ())
96
+ )
97
+ end
98
+
61
99
@testset " implemented by [map]reduce and [map]reducedim" begin
62
100
I, J, K = 2 , 2 , 2
63
101
OSArray = SArray{Tuple{I,J,K}} # original
0 commit comments