Skip to content

Commit 76e9a5b

Browse files
committed
test: ensure no aliasing
1 parent 8e179ec commit 76e9a5b

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

test/test_array_node.jl

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ end
179179
operators = OperatorEnum(; binary_operators=[+, -, *, /], unary_operators=[sin, cos])
180180
X = [1.0 2.0; 0.5 1.0] # 2 features, 2 samples
181181

182-
183182
# Test that property access doesn't allocate
184183
@check_allocs get_degree(n) = n.degree
185184
@check_allocs get_val(n) = n.val
@@ -474,3 +473,58 @@ end
474473

475474
println("✅ ArrayNode tree_mapreduce operations match Node!")
476475
end
476+
477+
@testitem "ArrayNode copy has no array aliasing" begin
478+
using DynamicExpressions
479+
const ArrayNode = DynamicExpressions.ArrayNode
480+
481+
# Create a test tree
482+
x1 = ArrayNode{Float64,2,Vector}(; feature=1)
483+
x2 = ArrayNode{Float64,2,Vector}(; feature=2)
484+
tree = ArrayNode{Float64,2,Vector}(;
485+
op=1,
486+
l=ArrayNode{Float64,2,Vector}(;
487+
op=2, l=x1, r=ArrayNode{Float64,2,Vector}(; val=3.5)
488+
),
489+
r=x2,
490+
)
491+
492+
# Test 1: Copy entire tree (root node)
493+
tree_copy = copy(tree)
494+
495+
# Verify no aliasing - modifying copy shouldn't affect original
496+
tree_copy.val = 999.0
497+
tree_copy.l.val = 888.0
498+
499+
# Check that original is unchanged
500+
@test tree.l.r.val == 3.5 # Original value unchanged
501+
@test tree.l.r.val != 888.0
502+
503+
# Verify the backing arrays are different
504+
orig_tree = tree.tree
505+
copy_tree = tree_copy.tree
506+
@test orig_tree !== copy_tree # Different tree objects
507+
@test orig_tree.nodes.val !== copy_tree.nodes.val # Different arrays
508+
@test orig_tree.nodes.degree !== copy_tree.nodes.degree
509+
@test orig_tree.nodes.children !== copy_tree.nodes.children
510+
511+
# Test 2: Copy subtree (non-root node)
512+
subtree = tree.l
513+
subtree_copy = copy(subtree)
514+
515+
# Modify the copy
516+
subtree_copy.r.val = 777.0
517+
518+
# Original should be unchanged
519+
@test tree.l.r.val == 3.5
520+
@test subtree.r.val == 3.5
521+
522+
# Verify different backing arrays for subtree copy too
523+
subtree_copy_tree = subtree_copy.tree
524+
@test orig_tree !== subtree_copy_tree
525+
@test orig_tree.nodes.val !== subtree_copy_tree.nodes.val
526+
527+
# Test 3: Verify structure is preserved in copy
528+
@test copy(tree) == tree
529+
@test copy(subtree) == subtree
530+
end

0 commit comments

Comments
 (0)