|
152 | 152 | @test a.u[1][1] == 1.0 |
153 | 153 | end |
154 | 154 |
|
| 155 | +@testset "recursivecopyto!" begin |
| 156 | + # Same-shape scalar arrays — should match copyto! |
| 157 | + b = zeros(3) |
| 158 | + a = [1.0, 2.0, 3.0] |
| 159 | + recursivecopyto!(b, a) |
| 160 | + @test b == a |
| 161 | + |
| 162 | + b = zeros(2, 2) |
| 163 | + a = [1.0 2.0; 3.0 4.0] |
| 164 | + recursivecopyto!(b, a) |
| 165 | + @test b == a |
| 166 | + |
| 167 | + # Issue #589: Matrix ← Vector of matching length (rejected by recursivecopy!, |
| 168 | + # allowed by recursivecopyto!). |
| 169 | + b = zeros(2, 3) |
| 170 | + a = collect(1.0:6.0) |
| 171 | + recursivecopyto!(b, a) |
| 172 | + @test b == reshape(a, 2, 3) |
| 173 | + @test_throws MethodError recursivecopy!(b, a) |
| 174 | + |
| 175 | + # Vector ← Matrix |
| 176 | + b = zeros(6) |
| 177 | + a = reshape(collect(1.0:6.0), 2, 3) |
| 178 | + recursivecopyto!(b, a) |
| 179 | + @test b == collect(1.0:6.0) |
| 180 | + |
| 181 | + # Different-shape matrices, same total length |
| 182 | + b = zeros(2, 3) |
| 183 | + a = reshape(collect(1.0:6.0), 3, 2) |
| 184 | + recursivecopyto!(b, a) |
| 185 | + @test vec(b) == 1.0:6.0 |
| 186 | + |
| 187 | + # dst longer than src — tail untouched, matches Base.copyto! |
| 188 | + b = ones(5) |
| 189 | + a = [10.0, 20.0, 30.0] |
| 190 | + recursivecopyto!(b, a) |
| 191 | + @test b == [10.0, 20.0, 30.0, 1.0, 1.0] |
| 192 | + |
| 193 | + # dst shorter than src — BoundsError, matches Base.copyto! |
| 194 | + b = zeros(2) |
| 195 | + a = [1.0, 2.0, 3.0] |
| 196 | + @test_throws BoundsError recursivecopyto!(b, a) |
| 197 | + |
| 198 | + # Nested: Vector of Vectors, matching shapes |
| 199 | + a = [ones(3), 2 * ones(3)] |
| 200 | + b = [zeros(3), zeros(3)] |
| 201 | + recursivecopyto!(b, a) |
| 202 | + @test b[1] == ones(3) && b[2] == 2 * ones(3) |
| 203 | + # Verify deep copy semantics — mutating dst leaves src untouched |
| 204 | + b[1][1] = 99.0 |
| 205 | + @test a[1][1] == 1.0 |
| 206 | + |
| 207 | + # Nested with shape mismatch at the leaves — inner copyto! handles it |
| 208 | + a = [collect(1.0:6.0), collect(7.0:12.0)] |
| 209 | + b = [zeros(2, 3), zeros(2, 3)] |
| 210 | + recursivecopyto!(b, a) |
| 211 | + @test b[1] == reshape(1.0:6.0, 2, 3) |
| 212 | + @test b[2] == reshape(7.0:12.0, 2, 3) |
| 213 | + |
| 214 | + # Static array element |
| 215 | + a = [@SVector([1.0, 2.0]), @SVector([3.0, 4.0])] |
| 216 | + b = [@SVector(zeros(2)), @SVector(zeros(2))] |
| 217 | + recursivecopyto!(b, a) |
| 218 | + @test b == a |
| 219 | + |
| 220 | + # ArrayPartition with matching shapes (sanity — parity with recursivecopy!) |
| 221 | + A = ArrayPartition(zeros(2), zeros(3)) |
| 222 | + B = ArrayPartition([1.0, 2.0], [3.0, 4.0, 5.0]) |
| 223 | + recursivecopyto!(A, B) |
| 224 | + @test A.x[1] == [1.0, 2.0] |
| 225 | + @test A.x[2] == [3.0, 4.0, 5.0] |
| 226 | + |
| 227 | + # VectorOfArray |
| 228 | + u1 = VA[zeros(MVector{2, Float64}), zeros(MVector{2, Float64})] |
| 229 | + u2 = VA[fill(4, MVector{2, Float64}), 2 .* ones(MVector{2, Float64})] |
| 230 | + recursivecopyto!(u1, u2) |
| 231 | + @test u1.u[1] == [4.0, 4.0] |
| 232 | + @test u1.u[2] == [2.0, 2.0] |
| 233 | + @test u1.u[1] isa MVector |
| 234 | +end |
| 235 | + |
155 | 236 | @testset "VectorOfArray similar with nested scalar leaves" begin |
156 | 237 | a = VA[ones(2), VA[1.0, 1.0]] |
157 | 238 | b = similar(a, Float64) |
|
0 commit comments