|
| 1 | +# Note: This must be the first file executed in the tests. |
| 2 | +# |
| 3 | +# The following structure is used to test the problem reported at issue #507. We |
| 4 | +# need to define it here because, since `recursivecopy!` and `copy_fields!` are |
| 5 | +# generated functions, then the `ArrayInterface.isimmutable` method must be |
| 6 | +# defined before the first `using DiffEqBase` in a Julia session. |
| 7 | + |
| 8 | +struct Quaternion{T} <: AbstractVector{T} |
| 9 | + q0::T |
| 10 | + q1::T |
| 11 | + q2::T |
| 12 | + q3::T |
| 13 | +end |
| 14 | + |
| 15 | +using ArrayInterface |
| 16 | +ArrayInterface.ismutable(::Type{<:Quaternion}) = false |
| 17 | +Base.size(::Quaternion) = 4 |
| 18 | + |
1 | 19 | using DiffEqBase, RecursiveArrayTools, Test
|
2 | 20 |
|
3 | 21 | mutable struct VectorType{T} <: DEDataVector{T}
|
|
105 | 123 | s0 = SimWorkspace2{Float64}(SVector{2,Float64}(1.0,4.0),1.)
|
106 | 124 | s1 = SimWorkspace2{Float64}(SVector{2,Float64}(2.0,1.0),1.)
|
107 | 125 | s0 .+ s1 == SimWorkspace2{Float64}(SVector{2,Float64}(3.0,5.0),1.)
|
| 126 | + |
| 127 | +# Test `recursivecopy!` in immutable structures derived from `AbstractArrays`. |
| 128 | +# See issue #507. |
| 129 | +mutable struct SimWorkspace3{T} <: DEDataVector{T} |
| 130 | + x::Vector{T} |
| 131 | + q::Quaternion{T} |
| 132 | +end |
| 133 | + |
| 134 | +a = SimWorkspace3([1.0,2.0,3.0], Quaternion(cosd(15), 0.0, 0.0, sind(15))) |
| 135 | +b = SimWorkspace3([0.0,0.0,0.0], Quaternion(1.0, 0.0, 0.0, 0.0)) |
| 136 | + |
| 137 | +recursivecopy!(b,a) |
| 138 | +@test b.x == a.x |
| 139 | +@test b.q.q0 == a.q.q0 |
| 140 | +@test b.q.q1 == a.q.q1 |
| 141 | +@test b.q.q2 == a.q.q2 |
| 142 | +@test b.q.q3 == a.q.q3 |
| 143 | + |
| 144 | +a = SimWorkspace3([1.0,2.0,3.0], Quaternion(cosd(15), 0.0, 0.0, sind(15))) |
| 145 | +b = SimWorkspace3([0.0,0.0,0.0], Quaternion(1.0, 0.0, 0.0, 0.0)) |
| 146 | + |
| 147 | +DiffEqBase.copy_fields!(b, a) |
| 148 | +@test b.x == [0.0,0.0,0.0] |
| 149 | +@test b.q.q0 == a.q.q0 |
| 150 | +@test b.q.q1 == a.q.q1 |
| 151 | +@test b.q.q2 == a.q.q2 |
| 152 | +@test b.q.q3 == a.q.q3 |
| 153 | + |
0 commit comments