-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathstruct_copy_test.jl
More file actions
151 lines (124 loc) · 4.82 KB
/
struct_copy_test.jl
File metadata and controls
151 lines (124 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using RecursiveArrayTools, Test
# Test structures for struct-aware recursivecopy
struct SimpleStruct
a::Int
b::Float64
end
mutable struct MutableStruct
a::Vector{Float64}
b::Matrix{Int}
c::String
end
struct NestedStruct
simple::SimpleStruct
mutable::MutableStruct
array::Vector{Int}
end
struct ParametricStruct{T}
data::Vector{T}
metadata::T
end
@testset "Struct recursivecopy tests" begin
@testset "Simple immutable struct" begin
original = SimpleStruct(42, 3.14)
copied = recursivecopy(original)
@test copied isa SimpleStruct
@test copied.a == original.a
@test copied.b == original.b
# Note: For immutable structs with only primitive types, Julia may optimize
# to use the same memory location, so we test functionality rather than identity
end
@testset "Mutable struct with arrays" begin
original = MutableStruct([1.0, 2.0, 3.0], [1 2; 3 4], "test")
# Should error for mutable structs
@test_throws ErrorException("recursivecopy for mutable structs is not currently implemented. Use deepcopy instead.") recursivecopy(original)
end
@testset "Nested struct" begin
simple = SimpleStruct(10, 2.5)
# Create a nested struct with only immutable components
struct ImmutableNested
simple::SimpleStruct
array::Vector{Int}
name::String
end
original = ImmutableNested(simple, [100, 200, 300], "nested")
copied = recursivecopy(original)
@test copied isa ImmutableNested
@test copied.simple.a == original.simple.a
@test copied.simple.b == original.simple.b
@test copied.array == original.array
@test copied.name == original.name
@test copied !== original
@test copied.array !== original.array
# Test independence
original.array[1] = 999
@test copied.array[1] == 100 # Should remain unchanged
end
@testset "Parametric struct" begin
original = ParametricStruct([1, 2, 3], 42)
copied = recursivecopy(original)
@test copied isa ParametricStruct{Int}
@test copied.data == original.data
@test copied.metadata == original.metadata
@test copied !== original
@test copied.data !== original.data
end
@testset "Compatibility with existing types" begin
# Test that arrays still work
arr = [1, 2, 3]
copied_arr = recursivecopy(arr)
@test copied_arr == arr
@test copied_arr !== arr
# Test that numbers still work
num = 42
copied_num = recursivecopy(num)
@test copied_num == num
# Test that strings still work
str = "hello"
copied_str = recursivecopy(str)
@test copied_str == str
end
@testset "ArrayPartition with structs" begin
simple1 = SimpleStruct(1, 1.0)
simple2 = SimpleStruct(2, 2.0)
ap = ArrayPartition([simple1, simple2])
copied_ap = recursivecopy(ap)
@test copied_ap isa ArrayPartition
@test length(copied_ap.x) == length(ap.x)
@test copied_ap.x[1][1].a == ap.x[1][1].a
@test copied_ap !== ap
@test copied_ap.x[1] !== ap.x[1]
end
@testset "Array dispatch still works correctly" begin
# Test that our struct method doesn't interfere with existing array methods
# Arrays of numbers should use copy
num_array = [1, 2, 3]
copied_num = recursivecopy(num_array)
@test copied_num == num_array
@test copied_num !== num_array
# Arrays of arrays should recursively copy
nested_array = [[1, 2], [3, 4]]
copied_nested = recursivecopy(nested_array)
@test copied_nested == nested_array
@test copied_nested !== nested_array
@test copied_nested[1] !== nested_array[1]
@test copied_nested[2] !== nested_array[2]
# AbstractVectorOfArray should use its method
ap = ArrayPartition([1.0, 2.0], [3, 4])
copied_ap = recursivecopy(ap)
@test copied_ap isa ArrayPartition
@test copied_ap.x[1] == ap.x[1]
@test copied_ap.x[1] !== ap.x[1]
# Test that structs containing arrays still work
struct StructWithArrays
data::Vector{Vector{Int}}
metadata::String
end
original_struct = StructWithArrays([[1, 2], [3, 4]], "test")
copied_struct = recursivecopy(original_struct)
@test copied_struct.data == original_struct.data
@test copied_struct.data !== original_struct.data
@test copied_struct.data[1] !== original_struct.data[1]
@test copied_struct.metadata == original_struct.metadata
end
end