From 51ccc062e1fd2bc3549345c09d3d0b08e0d8c73d Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Thu, 31 Aug 2017 10:03:58 +0200 Subject: [PATCH] recursivecopy! for StaticArrays --- src/utils.jl | 3 ++- test/copy_static_array_test.jl | 42 ++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/copy_static_array_test.jl diff --git a/src/utils.jl b/src/utils.jl index 18586ba2..ce74b637 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -8,7 +8,8 @@ end function recursivecopy!{T<:StaticArray,N}(b::AbstractArray{T,N},a::AbstractArray{T,N}) @inbounds for i in eachindex(a) - b[i] = a[i] + # TODO: Check for `setindex!`` and use `copy!(b[i],a[i])` or `b[i] = a[i]`, see #19 + b[i] = copy(a[i]) end end diff --git a/test/copy_static_array_test.jl b/test/copy_static_array_test.jl new file mode 100644 index 00000000..64eade2f --- /dev/null +++ b/test/copy_static_array_test.jl @@ -0,0 +1,42 @@ +using Base.Test, RecursiveArrayTools, StaticArrays + +struct ImmutableFV <: FieldVector{2,Float64} + a::Float64 + b::Float64 +end + +mutable struct MutableFV <: FieldVector{2,Float64} + a::Float64 + b::Float64 +end + +# Immutable FieldVector +vec = ImmutableFV(1.,2.) +a = [vec] +b = zeros(a) +recursivecopy!(b, a) +@test a[1] == b[1] + +# Mutable FieldVector +vec = MutableFV(1.,2.) +a = [vec] +b = zeros(a) +recursivecopy!(b, a) +@test a[1] == b[1] +a[1][1] *= 5 +@test a[1] != b[1] + +# SArray +vec = @SArray [1., 2.] +a = [vec] +b = zeros(a) +recursivecopy!(b, a) +@test a[1] == b[1] + +# MArray +vec = @MArray [1., 2.] +a = [vec] +b = zeros(a) +recursivecopy!(b, a) +a[1][1] *= 5 +@test a[1] != b[1] diff --git a/test/runtests.jl b/test/runtests.jl index bfe277b7..761eb0e5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,5 +7,6 @@ tic() @time @testset "Partitions Tests" begin include("partitions_test.jl") end @time @testset "VecOfArr Indexing Tests" begin include("basic_indexing.jl") end @time @testset "VecOfArr Interface Tests" begin include("interface_tests.jl") end +@time @testset "StaticArrays (recursivecopy!) Tests" begin include("copy_static_array_test.jl") end toc() # Test the VectorOfArray code