|
| 1 | +using StaticArrays, Test |
| 2 | + |
| 3 | +@testset "accumulate" begin |
| 4 | + @testset "cumsum(::$label)" for (label, T) in [ |
| 5 | + # label, T |
| 6 | + ("SVector", SVector), |
| 7 | + ("MVector", MVector), |
| 8 | + ("SizedVector", SizedVector), |
| 9 | + ] |
| 10 | + @testset "$label" for (label, a) in [ |
| 11 | + ("[1, 2, 3]", T{3}(SA[1, 2, 3])), |
| 12 | + ("[]", T{0,Int}(())), |
| 13 | + ] |
| 14 | + @test cumsum(a) == cumsum(collect(a)) |
| 15 | + @test cumsum(a) isa similar_type(a) |
| 16 | + @inferred cumsum(a) |
| 17 | + end |
| 18 | + @test eltype(cumsum(T{0,Int8}(()))) == eltype(cumsum(Int8[])) |
| 19 | + @test eltype(cumsum(T{1,Int8}((1)))) == eltype(cumsum(Int8[1])) |
| 20 | + @test eltype(cumsum(T{2,Int8}((1, 2)))) == eltype(cumsum(Int8[1, 2])) |
| 21 | + end |
| 22 | + |
| 23 | + @testset "cumsum(::$label; dims=2)" for (label, T) in [ |
| 24 | + # label, T |
| 25 | + ("SMatrix", SMatrix), |
| 26 | + ("MMatrix", MMatrix), |
| 27 | + ("SizedMatrix", SizedMatrix), |
| 28 | + ] |
| 29 | + @testset "$label" for (label, a) in [ |
| 30 | + ("[1 2; 3 4; 5 6]", T{3,2}(SA[1 2; 3 4; 5 6])), |
| 31 | + ("0 x 2 matrix", T{0,2,Float64}()), |
| 32 | + ("2 x 0 matrix", T{2,0,Float64}()), |
| 33 | + ] |
| 34 | + @test cumsum(a; dims = 2) == cumsum(collect(a); dims = 2) |
| 35 | + @test cumsum(a; dims = 2) isa similar_type(a) |
| 36 | + v"1.1" <= VERSION < v"1.2" && continue |
| 37 | + @inferred cumsum(a; dims = Val(2)) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + @testset "cumsum(a::SArray; dims=$i); ndims(a) = $d" for d in 1:4, i in 1:d |
| 42 | + shape = Tuple(1:d) |
| 43 | + a = similar_type(SArray, Int, Size(shape))(1:prod(shape)) |
| 44 | + @test cumsum(a; dims = i) == cumsum(collect(a); dims = i) |
| 45 | + @test cumsum(a; dims = i) isa SArray |
| 46 | + v"1.1" <= VERSION < v"1.2" && continue |
| 47 | + @inferred cumsum(a; dims = Val(i)) |
| 48 | + end |
| 49 | + |
| 50 | + @testset "cumprod" begin |
| 51 | + a = SA[1, 2, 3] |
| 52 | + @test cumprod(a)::SArray == cumprod(collect(a)) |
| 53 | + @inferred cumprod(a) |
| 54 | + |
| 55 | + @test eltype(cumsum(SA{Int8}[])) == eltype(cumsum(Int8[])) |
| 56 | + @test eltype(cumsum(SA{Int8}[1])) == eltype(cumsum(Int8[1])) |
| 57 | + @test eltype(cumsum(SA{Int8}[1, 2])) == eltype(cumsum(Int8[1, 2])) |
| 58 | + end |
| 59 | + |
| 60 | + @testset "empty vector with init" begin |
| 61 | + a = SA{Int}[] |
| 62 | + right(_, x) = x |
| 63 | + @test accumulate(right, a; init = Val(1)) === SA{Int}[] |
| 64 | + @inferred accumulate(right, a; init = Val(1)) |
| 65 | + end |
| 66 | +end |
0 commit comments