@@ -2,6 +2,14 @@ include("test_params.jl")
22using DynamicExpressions, Test
33import SymbolicUtils: simplify, Symbolic
44import Random: MersenneTwister
5+ import Base: ≈
6+
7+ function Base.:≈ (a:: String , b:: String )
8+ a = replace (a, r" \s +" => " " )
9+ b = replace (b, r" \s +" => " " )
10+ return a == b
11+ end
12+
513
614simplify_tree = DynamicExpressions. SimplifyEquationModule. simplify_tree
715combine_operators = DynamicExpressions. SimplifyEquationModule. combine_operators
@@ -33,10 +41,10 @@ tree = convert(Node, eqn2, operators)
3341# that SymbolicUtils does not convert it to a power:
3442tree = Node (" x1" ) * Node (" x1" )
3543eqn = convert (Symbolic, tree, operators)
36- @test repr (eqn) == " x1*x1"
44+ @test repr (eqn) ≈ " x1*x1"
3745# Test converting back:
3846tree_copy = convert (Node, eqn, operators)
39- @test repr (tree_copy) == " (x1 * x1)"
47+ @test repr (tree_copy) ≈ " (x1* x1)"
4048
4149# Let's test a much more complex function,
4250# with custom operators, and unary operators:
@@ -105,42 +113,42 @@ x1, x2, x3 = [Node(; feature=i) for i in 1:3]
105113
106114# unary operator applied to constant => constant:
107115tree = Node (1 , Node (; val= 0.0 ))
108- @test repr (tree) == " cos(0.0)"
109- @test repr (simplify_tree (tree, operators)) == " 1.0"
116+ @test repr (tree) ≈ " cos(0.0)"
117+ @test repr (simplify_tree (tree, operators)) ≈ " 1.0"
110118
111119# except when the result is a NaN, then we don't change it:
112120tree = Node (1 , Node (; val= NaN ))
113- @test repr (tree) == " cos(NaN)"
114- @test repr (simplify_tree (tree, operators)) == " cos(NaN)"
121+ @test repr (tree) ≈ " cos(NaN)"
122+ @test repr (simplify_tree (tree, operators)) ≈ " cos(NaN)"
115123
116124# the same as above, but inside a binary tree.
117125tree =
118126 Node (1 , Node (1 , Node (; val= 0.1 ), Node (; val= 0.2 )) + Node (; val= 0.2 )) + Node (; val= 2.0 )
119- @test repr (tree) == " (cos((0.1 + 0.2) + 0.2) + 2.0)"
120- @test repr (combine_operators (tree, operators)) == " (cos(0.4 + 0.1) + 2.0)"
127+ @test repr (tree) ≈ " (cos((0.1 + 0.2) + 0.2) + 2.0)"
128+ @test repr (combine_operators (tree, operators)) ≈ " (cos(0.4 + 0.1) + 2.0)"
121129
122130# left is constant:
123131tree = Node (; val= 0.5 ) + (Node (; val= 0.2 ) + x1)
124- @test repr (tree) == " (0.5 + (0.2 + x1))"
125- @test repr (combine_operators (tree, operators)) == " (x1 + 0.7)"
132+ @test repr (tree) ≈ " (0.5 + (0.2 + x1))"
133+ @test repr (combine_operators (tree, operators)) ≈ " (x1 + 0.7)"
126134
127135# (const - (const - var)) => (var - const)
128136tree = Node (2 , Node (; val= 0.5 ), Node (; val= 0.2 ) - x1)
129- @test repr (tree) == " (0.5 - (0.2 - x1))"
130- @test repr (combine_operators (tree, operators)) == " (x1 - -0.3)"
137+ @test repr (tree) ≈ " (0.5 - (0.2 - x1))"
138+ @test repr (combine_operators (tree, operators)) ≈ " (x1 - -0.3)"
131139
132140# ((const - var) - const) => (const - var)
133141tree = Node (2 , Node (; val= 0.5 ) - x1, Node (; val= 0.2 ))
134- @test repr (tree) == " ((0.5 - x1) - 0.2)"
135- @test repr (combine_operators (tree, operators)) == " (0.3 - x1)"
142+ @test repr (tree) ≈ " ((0.5 - x1) - 0.2)"
143+ @test repr (combine_operators (tree, operators)) ≈ " (0.3 - x1)"
136144
137145# (const - (var - const)) => (const - var)
138146tree = Node (2 , Node (; val= 0.5 ), x1 - Node (; val= 0.2 ))
139- @test repr (tree) == " (0.5 - (x1 - 0.2))"
140- @test repr (combine_operators (tree, operators)) == " (0.7 - x1)"
147+ @test repr (tree) ≈ " (0.5 - (x1 - 0.2))"
148+ @test repr (combine_operators (tree, operators)) ≈ " (0.7 - x1)"
141149
142150# ((var - const) - const) => (var - const)
143151tree = ((x1 - 0.2 ) - 0.6 )
144- @test repr (tree) == " ((x1 - 0.2) - 0.6)"
145- @test repr (combine_operators (tree, operators)) == " (x1 - 0.8)"
152+ @test repr (tree) ≈ " ((x1 - 0.2) - 0.6)"
153+ @test repr (combine_operators (tree, operators)) ≈ " (x1 - 0.8)"
146154# ##############################################################################
0 commit comments