-
Notifications
You must be signed in to change notification settings - Fork 15
/
mxpr_test.sj
126 lines (96 loc) · 2.41 KB
/
mxpr_test.sj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Fails in 0.5.0-dev+3385, probably because the parser has changed
T ==(2,2) # is an expression with Head '=='
T [1,2,3][2] == 2
## Head of Mxpr is Julia function
ClearAll(f,g,x,c)
# We use an anonymous function to avoid the redefinition warning if this test
# is run more than once.
g = J( (x) -> mpow(x,2) ) # g is SJulia Symbol bound to Julia Function
# Creates Mxpr with head of type Function.
# The apprule for head Function is to call it on the args
T g(3) == 9
T g(c) == c^2
ClearAll(f,g,x,c)
## SetDelay for SJSym
Clear(a,b,c)
T a == a
## Remove these, I think
# T (a == b) != False
# T (a != b) != True
# T (a != b) != False
T Head(a == b) == Equal
T Head(a != b) == Unequal
T (1 < 2) == True
T (1 < 2 < 3) == True
T (1 < 3 < 2) == False
T Head(a == a != b) == Unequal
T (a != a == b) == False
(a = 1)
(b = a)
(c := a)
(a = 2)
T (b) == 1
T (c) == 2
Clear(a,b,res)
res = a * b
a = 1
T res == b # test fix for bug in commit 6565
Clear(a,b,c,g)
a = g
T a(b) == g(b)
Clear(a,g)
ClearAll(a,b,x,y)
## Destructured
[a,b] = [1,2]
T [a,b] == [1,2]
[a,b] = [b,a]
T [a,b] == [2,1]
# Test non-Symbol Head for Mxpr
ClearAll(a,c,f,b)
a = f(c)
b = ReplaceAll(a, f => 4) # Parser will not allow us to construct 4(c)
T Head(b) == 4
T Head(Apply(5.0,[1,2,3])) == 5.0
ClearAll(a,c,f,b)
ClearAll(g,h,x,m,y)
m = [g,h]
g(x_) := x^2
h(x_) := x^3
T m[1](y) == y^2
T m[2](y) == y^3
ClearAll(g,h,x,m,y)
# Test replacement
ClearAll(a,b,c,g)
cossinrule = Cos(x_)^2 + Sin(x_)^2 => 1
# Does not match
T (Replace( Cos(a+b)^2 + Sin(a+c)^2, cossinrule)) == (Cos(a+b)^2 + Sin(a+c)^2)
# Does not match
T (Replace( Cos(a+b)^2 + Sin(a+b)^2, cossinrule)) == 1
# One level down. Does not match
T Replace( g(Cos(a+b)^2 + Sin(a+b)^2), cossinrule) == g(Cos(a + b)^2 + Sin(a + b)^2)
# Match at every level
T ReplaceAll( g(Cos(a+b)^2 + Sin(a+b)^2), cossinrule) == g(1)
ClearAll(cossinrule)
T Replace( a , a => 1) == 1
T Replace( b , a => 1) == b
# ClearAll(a,b)
## Set a Julia variable
(SetJ(a,"cat"))
T J( Main.a == "cat" )
## Test compound expression
Clear(a)
res = ( a = 3, 4)
T a == 3
T res == 4
Clear(a,res)
ClearAll(f,a,b)
f(x_) := ( a = 1, x + 1 )
T f(b) == b + 1
ClearAll(f,a,b)
## Special rule
T Cos(ArcCos(x)) == x
T Attributes(Plus) == [Flat,Listable,NumericFunction,OneIdentity,Orderless,Protected]
## Orderless
ClearAll(f,a,c,z)
T Apply(List,f(z,c,a)) == [z,c,a]
ClearAll(f,a,c,z,g,res,x)