|
| 1 | +@testitem "ReadOnlyNode construction and access" begin |
| 2 | + using DynamicExpressions |
| 3 | + using DynamicExpressions: ReadOnlyNode |
| 4 | + |
| 5 | + inner_node = Node{Float64}(; val=42.0) |
| 6 | + readonly_node = ReadOnlyNode(inner_node) |
| 7 | + |
| 8 | + @test readonly_node isa ReadOnlyNode |
| 9 | + @test getfield(readonly_node, :_inner) === inner_node |
| 10 | + @test readonly_node.degree == inner_node.degree |
| 11 | + @test readonly_node.constant == inner_node.constant |
| 12 | + @test readonly_node.val == inner_node.val |
| 13 | +end |
| 14 | + |
| 15 | +@testitem "ReadOnlyNode immutability" begin |
| 16 | + using DynamicExpressions |
| 17 | + using DynamicExpressions: ReadOnlyNode |
| 18 | + |
| 19 | + inner_node = Node{Float64}(; val=42.0) |
| 20 | + readonly_node = ReadOnlyNode(inner_node) |
| 21 | + |
| 22 | + @test_throws ErrorException readonly_node.val = 100.0 |
| 23 | + @test_throws "Cannot set properties on a ReadOnlyNode" readonly_node.val = 100.0 |
| 24 | +end |
| 25 | + |
| 26 | +@testitem "ReadOnlyNode - accessing children should return ReadOnlyNode" begin |
| 27 | + using DynamicExpressions |
| 28 | + using DynamicExpressions: ReadOnlyNode |
| 29 | + |
| 30 | + operators = OperatorEnum(; binary_operators=(+, -, *, /), unary_operators=(sin, exp)) |
| 31 | + x1 = Node{Float64}(; feature=1) |
| 32 | + x2 = Node{Float64}(; feature=2) |
| 33 | + tree = 2 * x1 - sin(x2) |
| 34 | + readonly_node = ReadOnlyNode(tree) |
| 35 | + |
| 36 | + @test typeof(readonly_node.l) === typeof(readonly_node) |
| 37 | +end |
| 38 | + |
| 39 | +@testitem "ReadOnlyNode copy" begin |
| 40 | + using DynamicExpressions |
| 41 | + using DynamicExpressions: ReadOnlyNode |
| 42 | + |
| 43 | + inner_node = Node{Float64}(; val=42.0) |
| 44 | + readonly_node = ReadOnlyNode(inner_node) |
| 45 | + copied_node = copy(readonly_node) |
| 46 | + |
| 47 | + @test copied_node !== readonly_node |
| 48 | + @test copied_node == readonly_node |
| 49 | +end |
| 50 | + |
| 51 | +@testitem "StructuredExpression returns ReadOnlyNode" begin |
| 52 | + using DynamicExpressions |
| 53 | + using DynamicExpressions: ReadOnlyNode |
| 54 | + using DynamicExpressions: StructuredExpression |
| 55 | + |
| 56 | + operators = OperatorEnum(; binary_operators=[+, -, *, /], unary_operators=[-, cos, exp]) |
| 57 | + variable_names = ["x", "y"] |
| 58 | + kws = (; operators, variable_names) |
| 59 | + f = parse_expression(:(x * x - cos(2.5f0 * y + -0.5f0)); kws...) |
| 60 | + g = parse_expression(:(exp(-(y * y))); kws...) |
| 61 | + |
| 62 | + structured_expr = StructuredExpression((; f, g); structure=nt -> nt.f + nt.g, kws...) |
| 63 | + |
| 64 | + tree = get_tree(structured_expr) |
| 65 | + @test tree isa ReadOnlyNode |
| 66 | + @test string_tree(tree, operators; variable_names) == |
| 67 | + "((x * x) - cos((2.5 * y) + -0.5)) + exp(-(y * y))" |
| 68 | + @test getfield(tree, :_inner) isa Node |
| 69 | +end |
0 commit comments