Skip to content

Commit 706d4f8

Browse files
test: update some tests
1 parent 142c0c9 commit 706d4f8

File tree

10 files changed

+77
-73
lines changed

10 files changed

+77
-73
lines changed

test/basics.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SymbolicUtils: Symbolic, Sym, FnType, Term, Add, Mul, Pow, symtype, operation, arguments, issym, isterm, BasicSymbolic, term, isequal_with_metadata
1+
using SymbolicUtils: Symbolic, Sym, FnType, Term, Add, Mul, Pow, symtype, operation, arguments, issym, isterm, BasicSymbolic, term, isequal_core
22
using SymbolicUtils
33
using ConstructionBase: setproperties
44
using Setfield
@@ -226,7 +226,7 @@ end
226226

227227
# test that the "x^2 + y^-1 + sin(a)^3.5 + 2t + 1//1" expression from Symbolics.jl/build_targets.jl is properly sorted
228228
@syms x1 y1 a1 t1
229-
@test repr(x1^2 + y1^-1 + sin(a1)^3.5 + 2t1 + 1//1) == "(1//1) + 2t1 + 1 / y1 + x1^2 + sin(a1)^3.5"
229+
@test repr(x1^2 + y1^-1 + sin(a1)^3.5 + 2t1 + 1//1) == "1 + 2t1 + 1 / y1 + x1^2 + sin(a1)^3.5"
230230
end
231231

232232
@testset "inspect" begin
@@ -304,7 +304,7 @@ toterm(t) = Term{symtype(t)}(operation(t), arguments(t))
304304
@syms a b c
305305
@test isequal(toterm(-1c), Term{Number}(*, [-1, c]))
306306
@test isequal(toterm(-1(a+b)), Term{Number}(+, [-1a, -b]))
307-
@test isequal(toterm((a + b) - (b + c)), Term{Number}(+, [a, -1c]))
307+
@test isequal(toterm((a + b) - (b + c)), Term{Number}(+, [-1c, a]))
308308
end
309309

310310
@testset "hash" begin
@@ -346,9 +346,9 @@ end
346346
a1 = setmetadata(a, Ctx1, "meta_1")
347347
a2 = setmetadata(a, Ctx1, "meta_1")
348348
a3 = setmetadata(a, Ctx2, "meta_2")
349-
@test !isequal_with_metadata(a, a1)
350-
@test isequal_with_metadata(a1, a2)
351-
@test !isequal_with_metadata(a1, a3)
349+
@test !isequal_core(a, a1; full=true)
350+
@test isequal_core(a1, a2; full=true)
351+
@test !isequal_core(a1, a3; full=true)
352352
end
353353

354354
@testset "subtyping" begin
@@ -437,8 +437,8 @@ end
437437
x = setmetadata(x(t), Int, 3)
438438
ex = x * y
439439
res = substitute(ex, Dict(y => 1))
440-
@test SymbolicUtils.isequal_with_metadata(res, x)
440+
@test SymbolicUtils.isequal_core(res, x; full=true)
441441
ex = x + y
442442
res = substitute(ex, Dict(y => 0))
443-
@test SymbolicUtils.isequal_with_metadata(res, x)
443+
@test SymbolicUtils.isequal_core(res, x; full=true)
444444
end

test/code.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ nanmath_st.rewrites[:nanmath] = true
2121
@test toexpr(a+b+c+d+e) == :($(+)($(+)($(+)($(+)(a, b), c), d), e))
2222
@test toexpr(a+b) == :($(+)(a, b))
2323
@test toexpr(x(t)+y(t)) == :($(+)(x(t), y(t)))
24-
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)($(+)(x(t), y(t)), x($(+)(1, t))))
24+
@test toexpr(x(t)+y(t)+x(t+1)) == :($(+)($(+)(x(t), x($(+)(1, t))), y(t)))
2525
s = LazyState()
2626
Code.union_rewrites!(s.rewrites, [x(t), y(t)])
27-
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)($(+)(var"x(t)", var"y(t)"), x($(+)(1, t))))
27+
@test toexpr(x(t)+y(t)+x(t+1), s) == :($(+)($(+)(var"x(t)", x($(+)(1, t))), var"y(t)"))
2828

2929
ex = :(let a = 3, b = $(+)(1,a)
3030
$(+)(a, b)
@@ -38,7 +38,7 @@ nanmath_st.rewrites[:nanmath] = true
3838

3939
test_repr(toexpr(Func([x(t), x],[b a+2, y(t) b], x(t)+x(t+1)+b+y(t))),
4040
:(function (var"x(t)", x; b = $(+)(2, a), var"y(t)" = b)
41-
$(+)($(+)($(+)(b, var"x(t)"), var"y(t)"), x($(+)(1, t)))
41+
$(+)($(+)($(+)(b, var"x(t)"), x($(+)(1, t))), var"y(t)")
4242
end))
4343
test_repr(toexpr(Func([DestructuredArgs([x, x(t)], :state),
4444
DestructuredArgs((a, b), :params)], [],

test/hash_consing.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using SymbolicUtils, Test
2-
using SymbolicUtils: Term, Add, Mul, Div, Pow, hash2, metadata, BasicSymbolic, Symbolic,
3-
isequal_with_metadata
2+
using SymbolicUtils: Term, Add, Mul, Div, Pow, hash_core, metadata, BasicSymbolic, Symbolic,
3+
isequal_core
44
import TermInterface
55

6+
hash2(a, b) = hash_core(a, b; full=true)
7+
hash2(a) = hash_core(a, zero(UInt); full=true)
8+
isequal_with_metadata(a, b) = isequal_core(a, b; full=true)
9+
610
struct Ctx1 end
711
struct Ctx2 end
812

@@ -81,7 +85,7 @@ end
8185
v5 = 3a/6
8286
v6 = 2a/4
8387
@test v5 === v6
84-
v7 = Div{Float64}(-1,a)
88+
v7 = Div{Float64}(-1,a, false)
8589
@test v7 !== v3
8690

8791
vm1 = setmetadata(v1,Ctx1, "meta_1")
@@ -117,8 +121,8 @@ end
117121
b1 = setmetadata(b, Int, 3)
118122
a2 = setmetadata(a, Int, b1)
119123
@test a1 !== a2
120-
@test !SymbolicUtils.isequal_with_metadata(a1, a2)
121-
@test metadata(metadata(a1)[Int]) === nothing
124+
@test !isequal_with_metadata(a1, a2)
125+
@test isempty(metadata(metadata(a1)[Int]))
122126
@test metadata(metadata(a2)[Int])[Int] == 3
123127
end
124128

@@ -127,8 +131,8 @@ end
127131
aa = setmetadata(a, Int, b)
128132
@test aa !== a
129133
@test isequal(a, aa)
130-
@test !SymbolicUtils.isequal_with_metadata(a, aa)
131-
@test !SymbolicUtils.isequal_with_metadata(2a, 2aa)
134+
@test !isequal_with_metadata(a, aa)
135+
@test !isequal_with_metadata(2a, 2aa)
132136
end
133137

134138
@testset "Hashconsing can be toggled" begin
@@ -143,7 +147,7 @@ end
143147
@testset "`hash2` is cached" begin
144148
@syms a b f(..)
145149
for ex in [a + b, a * b, f(a)]
146-
h = SymbolicUtils.hash2(ex)
150+
h = hash2(ex)
147151
@test h == ex.hash2[]
148152
ex2 = setmetadata(ex, Int, 3)
149153
@test ex2.hash2[] != h
@@ -193,12 +197,12 @@ end
193197
@syms x
194198
tmp1 = x ^ 3.0
195199
tmp2 = x ^ 3
196-
@test !SymbolicUtils.isequal_with_metadata(tmp1, tmp2)
200+
@test !isequal_with_metadata(tmp1, tmp2)
197201
@test arguments(tmp1)[2] isa Float64
198202
@test arguments(tmp2)[2] isa Int
199203
tmp1 = 2tmp1
200204
tmp2 = 2tmp2
201-
@test !SymbolicUtils.isequal_with_metadata(tmp1, tmp2)
205+
@test !isequal_with_metadata(tmp1, tmp2)
202206
@test arguments(arguments(tmp1)[2])[2] isa Float64
203207
@test arguments(arguments(tmp2)[2])[2] isa Int
204208
end

test/inspect_output/ex-md.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
1 DIV
2-
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2))
3-
3 │ ├─ POW
4-
4 │ │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2))
1+
1 Div metadata=()
2+
2 ├─ AddOrMul(variant = "MUL", scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) metadata=()
3+
3 │ ├─ Pow metadata=()
4+
4 │ │ ├─ AddOrMul(variant = "ADD", scalar = 1, coeffs = (y => 3, x => 2)) metadata=()
55
5 │ │ │ ├─ 1
6-
6 │ │ │ ├─ MUL(scalar = 2, powers = (x => 1,))
6+
6 │ │ │ ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,)) metadata=()
77
7 │ │ │ │ ├─ 2
8-
8 │ │ │ │ └─ SYM(x)
9-
9 │ │ │ └─ MUL(scalar = 3, powers = (y => 1,))
8+
8 │ │ │ │ └─ Sym(x) metadata=()
9+
9 │ │ │ └─ AddOrMul(variant = "MUL", scalar = 3, powers = (y => 1,)) metadata=()
1010
10 │ │ │ ├─ 3
11-
11 │ │ │ └─ SYM(y) metadata=(Integer => 42,)
11+
11 │ │ │ └─ Sym(y) metadata=(Integer => 42,)
1212
12 │ │ └─ 2
13-
13 │ └─ SYM(z)
14-
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2))
15-
15 ├─ MUL(scalar = 2, powers = (x => 1,))
13+
13 │ └─ Sym(z) metadata=()
14+
14 └─ AddOrMul(variant = "ADD", scalar = 0, coeffs = (z => 1, x => 2)) metadata=()
15+
15 ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,)) metadata=()
1616
16 │ ├─ 2
17-
17 │ └─ SYM(x)
18-
18 └─ SYM(z)
17+
17 │ └─ Sym(x) metadata=()
18+
18 └─ Sym(z) metadata=()
1919

2020
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number

test/inspect_output/ex-nohint.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
1 DIV
2-
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2))
3-
3 │ ├─ POW
4-
4 │ │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2))
1+
1 Div
2+
2 ├─ AddOrMul(variant = "MUL", scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2))
3+
3 │ ├─ Pow
4+
4 │ │ ├─ AddOrMul(variant = "ADD", scalar = 1, coeffs = (y => 3, x => 2))
55
5 │ │ │ ├─ 1
6-
6 │ │ │ ├─ MUL(scalar = 2, powers = (x => 1,))
6+
6 │ │ │ ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,))
77
7 │ │ │ │ ├─ 2
8-
8 │ │ │ │ └─ SYM(x)
9-
9 │ │ │ └─ MUL(scalar = 3, powers = (y => 1,))
8+
8 │ │ │ │ └─ Sym(x)
9+
9 │ │ │ └─ AddOrMul(variant = "MUL", scalar = 3, powers = (y => 1,))
1010
10 │ │ │ ├─ 3
11-
11 │ │ │ └─ SYM(y)
11+
11 │ │ │ └─ Sym(y)
1212
12 │ │ └─ 2
13-
13 │ └─ SYM(z)
14-
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2))
15-
15 ├─ MUL(scalar = 2, powers = (x => 1,))
13+
13 │ └─ Sym(z)
14+
14 └─ AddOrMul(variant = "ADD", scalar = 0, coeffs = (z => 1, x => 2))
15+
15 ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,))
1616
16 │ ├─ 2
17-
17 │ └─ SYM(x)
18-
18 └─ SYM(z)
17+
17 │ └─ Sym(x)
18+
18 └─ Sym(z)

test/inspect_output/ex.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
1 DIV
2-
2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2))
3-
3 │ ├─ POW
4-
4 │ │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2))
1+
1 Div
2+
2 ├─ AddOrMul(variant = "MUL", scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2))
3+
3 │ ├─ Pow
4+
4 │ │ ├─ AddOrMul(variant = "ADD", scalar = 1, coeffs = (y => 3, x => 2))
55
5 │ │ │ ├─ 1
6-
6 │ │ │ ├─ MUL(scalar = 2, powers = (x => 1,))
6+
6 │ │ │ ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,))
77
7 │ │ │ │ ├─ 2
8-
8 │ │ │ │ └─ SYM(x)
9-
9 │ │ │ └─ MUL(scalar = 3, powers = (y => 1,))
8+
8 │ │ │ │ └─ Sym(x)
9+
9 │ │ │ └─ AddOrMul(variant = "MUL", scalar = 3, powers = (y => 1,))
1010
10 │ │ │ ├─ 3
11-
11 │ │ │ └─ SYM(y)
11+
11 │ │ │ └─ Sym(y)
1212
12 │ │ └─ 2
13-
13 │ └─ SYM(z)
14-
14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2))
15-
15 ├─ MUL(scalar = 2, powers = (x => 1,))
13+
13 │ └─ Sym(z)
14+
14 └─ AddOrMul(variant = "ADD", scalar = 0, coeffs = (z => 1, x => 2))
15+
15 ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,))
1616
16 │ ├─ 2
17-
17 │ └─ SYM(x)
18-
18 └─ SYM(z)
17+
17 │ └─ Sym(x)
18+
18 └─ Sym(z)
1919

2020
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number

test/inspect_output/sub10.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
1 MUL(scalar = 3, powers = (y => 1,))
1+
1 AddOrMul(variant = "MUL", scalar = 3, powers = (y => 1,))
22
2 ├─ 3
3-
3 └─ SYM(y)
3+
3 └─ Sym(y)
44

55
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number

test/inspect_output/sub14.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
1 ADD(scalar = 0, coeffs = (z => 1, x => 2))
2-
2 ├─ MUL(scalar = 2, powers = (x => 1,))
1+
1 AddOrMul(variant = "ADD", scalar = 0, coeffs = (z => 1, x => 2))
2+
2 ├─ AddOrMul(variant = "MUL", scalar = 2, powers = (x => 1,))
33
3 │ ├─ 2
4-
4 │ └─ SYM(x)
5-
5 └─ SYM(z)
4+
4 │ └─ Sym(x)
5+
5 └─ Sym(z)
66

77
Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number

test/order.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ end
6464
@test !(b(t) <a(t))
6565

6666
@syms y() x()
67-
@test x() <y()
68-
@test !(y() <x())
67+
@test y() <x()
68+
@test !(x() <y())
6969
end
7070

7171
@testset "Sym vs Term" begin

test/rewrite.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using SymbolicUtils
2-
2+
using Test
33
include("utils.jl")
44

55
@syms a b c
@@ -38,7 +38,7 @@ end
3838
@test @rule((~x)^(~x) => ~x)(b^a) === nothing
3939
@test @rule((~x)^(~x) => ~x)(a+a) === nothing
4040
@eqtest @rule((~x)^(~x) => ~x)(sin(a)^sin(a)) == sin(a)
41-
@eqtest @rule((~x*~y + ~x*~z) => ~x * (~y+~z))(a*b + a*c) == a*(b+c)
41+
@eqtest @rule((~x*~y + ~z*~x) => ~x * (~y+~z))(a*b + a*c) == a*(b+c)
4242

4343
@eqtest @rule(+(~~x) => ~~x)(a + b) == [a,b]
4444
@eqtest @rule(+(~~x) => ~~x)(term(+, a, b, c)) == [a,b,c]
@@ -77,8 +77,8 @@ end
7777
@test r_pow2(a+b) === 1
7878

7979
r_mix = @rule (~x + (~y)*(~!c))^(~!m) => ~m + ~c
80-
@test r_mix((a + b*c)^2) === 2 + c
81-
@test r_mix((a + b*c)) === 1 + c
80+
@test r_mix((a + b*c)^2) === 2 + b
81+
@test r_mix((a + b*c)) === 1 + b
8282
@test r_mix((a + b)) === 2 #1+1
8383
end
8484

@@ -143,4 +143,4 @@ end
143143
ex1 = ex * b
144144

145145
@test getmetadata(arguments(ex1)[1], MetaData) == :metadata
146-
end
146+
end

0 commit comments

Comments
 (0)