Skip to content

Commit 4f621ec

Browse files
committed
Add unittests and docs for AbstractNode
1 parent 3d57bd4 commit 4f621ec

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

docs/src/types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ You can create a copy of a node with `copy_node`:
8383
```@docs
8484
copy_node(tree::Node)
8585
```
86+
87+
There is also an abstract type `AbstractNode` which is a supertype of `Node`:
88+
89+
```@docs
90+
AbstractNode
91+
```

src/Equation.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ Abstract type for binary trees. Must have the following fields:
1313
- `degree::Integer`: Degree of the node. Either 0, 1, or 2. If 1,
1414
then `l` needs to be defined as the left child. If 2,
1515
then `r` also needs to be defined as the right child.
16-
- `l::AbstractNode`: Left child of the current node.
17-
- `r::AbstractNode`: Right child of the current node.
16+
- `l::AbstractNode`: Left child of the current node. Should only be
17+
defined if `degree >= 1`; otherwise, leave it undefined (see the
18+
the constructors of `Node{T}` for an example).
19+
Don't use `nothing` to represent an undefined value
20+
as it will incur a large performance penalty.
21+
- `r::AbstractNode`: Right child of the current node. Should only
22+
be defined if `degree == 2`.
1823
"""
1924
abstract type AbstractNode end
2025

test/test_custom_node_type.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using DynamicExpressions
2+
using Test
3+
4+
mutable struct MyCustomNode{A,B} <: AbstractNode
5+
degree::Int
6+
val1::A
7+
val2::B
8+
l::MyCustomNode{A,B}
9+
r::MyCustomNode{A,B}
10+
11+
MyCustomNode(val1, val2) = new{typeof(val1),typeof(val2)}(0, val1, val2)
12+
MyCustomNode(val1, val2, l) = new{typeof(val1),typeof(val2)}(1, val1, val2, l)
13+
MyCustomNode(val1, val2, l, r) = new{typeof(val1),typeof(val2)}(2, val1, val2, l, r)
14+
end
15+
16+
node1 = MyCustomNode(1.0, 2)
17+
18+
@test typeof(node1) == MyCustomNode{Float64,Int}
19+
@test node1.degree == 0
20+
@test count_depth(node1) == 1
21+
@test count_nodes(node1) == 1
22+
23+
node2 = MyCustomNode(1.5, 3, node1)
24+
25+
@test typeof(node2) == MyCustomNode{Float64,Int}
26+
@test node2.degree == 1
27+
@test node2.l.degree == 0
28+
@test count_depth(node2) == 2
29+
@test count_nodes(node2) == 2
30+
31+
node2 = MyCustomNode(1.5, 3, node1, node1)
32+
33+
@test count_depth(node2) == 2
34+
@test count_nodes(node2) == 3
35+
@test sum(t -> t.val1, node2) == 1.5 + 1.0 + 1.0
36+
@test sum(t -> t.val2, node2) == 3 + 2 + 2
37+
@test count(t -> t.degree == 0, node2) == 2

test/unittest.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,7 @@ end
9191
@safetestset "Test helpers break upon redefining" begin
9292
include("test_safe_helpers.jl")
9393
end
94+
95+
@safetestset "Test custom node type" begin
96+
include("test_custom_node_type.jl")
97+
end

0 commit comments

Comments
 (0)