Skip to content

Commit

Permalink
fix tests?
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed May 8, 2021
1 parent 7a66328 commit 1a4aa62
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ reduce(::typeof(hcat), A::StaticArray{<:Tuple,<:StaticVecOrMatLike}) =
_mapreduce(identity, op, dims, init, Size(a), a)

@inline function _reduce(op, a::StaticArray, dims::Tuple, init = _InitialValue())
b = _reduce(op, a, first(dims), init)
return _reduce(op, b, Base.tail(dims))
b = _reduce(op, a, first(dims))
return _reduce(op, b, Base.tail(dims), init)
end
_reduce(op, a::StaticArray, dims::Tuple{}, ::_InitialValue) = a
_reduce(op, a::StaticArray, dims::Tuple{}, init) = op.(init, a)
Expand Down
8 changes: 8 additions & 0 deletions test/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,24 @@ using Statistics: mean
v2 = [4,3,2,1]; sv2 = SVector{4}(v2)
@test reduce(+, sv1) === reduce(+, v1)
@test reduce(+, sv1; init=0) === reduce(+, v1; init=0)
@test reduce(+, sv1; init=99) === reduce(+, v1; init=99)
@test reduce(max, sa; dims=Val(1), init=-1.) === SMatrix{1,J}(reduce(max, a, dims=1, init=-1.))
@test reduce(max, sa; dims=1, init=-1.) === SMatrix{1,J}(reduce(max, a, dims=1, init=-1.))
@test reduce(max, sa; dims=2, init=-1.) === SMatrix{I,1}(reduce(max, a, dims=2, init=-1.))
@test reduce(*, sa; dims=(1,2), init=2.0) === SMatrix{1,1}(reduce(*, a, dims=(1,2), init=2.0))
@test reduce(*, sa; dims=(), init=(1.0+im)) === SMatrix{I,J}(reduce(*, a, dims=(), init=(1.0+im)))
@test mapreduce(-, +, sv1) === mapreduce(-, +, v1)
@test mapreduce(-, +, sv1; init=0) === mapreduce(-, +, v1, init=0)
@test mapreduce(*, +, sv1, sv2) === 40
@test mapreduce(*, +, sv1, sv2; init=0) === 40
@test mapreduce(x->x^2, max, sa; dims=Val(1), init=-1.) == SMatrix{1,J}(mapreduce(x->x^2, max, a, dims=1, init=-1.))
@test mapreduce(x->x^2, max, sa; dims=1, init=-1.) == SMatrix{1,J}(mapreduce(x->x^2, max, a, dims=1, init=-1.))
@test mapreduce(x->x^2, max, sa; dims=2, init=-1.) == SMatrix{I,1}(mapreduce(x->x^2, max, a, dims=2, init=-1.))

# Zero-dim array
a0 = fill(rand()); sa0 = SArray{Tuple{}}(a0)
@test reduce(+, sa0) === reduce(+, a0)
@test reduce(/, sa0, dims=(), init=1.2) === SArray{Tuple{}}(reduce(/, a0, dims=(), init=1.2))
end

@testset "[map]foldl" begin
Expand Down

0 comments on commit 1a4aa62

Please sign in to comment.