pipethread EOF fix
[PyX/mjg.git] / test / test_mathtree.py
blobe118a0faaad1ac73468c524d4e220528a7593292
1 #!/usr/bin/env python
2 import sys, unittest
3 sys.path[:0] = [".."]
5 from pyx import *
6 from pyx import mathtree
8 class MathTreeTestCase(unittest.TestCase):
10 def testStr(self):
11 myparser = mathtree.parser()
12 assert str(myparser.parse("a+b-c")) == "a+b-c"
13 assert str(myparser.parse("(a+b)-c")) == "a+b-c"
14 assert str(myparser.parse("a+(b-c)")) == "a+(b-c)" # XXX: needs no parenthesis
15 assert str(myparser.parse("a-b+c")) == "a-b+c"
16 assert str(myparser.parse("(a-b)+c")) == "a-b+c"
17 assert str(myparser.parse("a-(b+c)")) == "a-(b+c)"
18 assert str(myparser.parse("a+b-c*d/e**f")) == "a+b-c*d/e^f"
19 assert str(myparser.parse("a**b/c*d-e+f")) == "a^b/c*d-e+f"
20 assert str(myparser.parse("((a-(b+c))/(d*e))**f")) == "((a-(b+c))/(d*e))^f"
21 assert str(myparser.parse("sin(pi/2)")) == "sin(pi/2.0)"
22 assert str(myparser.parse("norm(a,b)")) == "norm(a,b)"
24 def testRepr(self):
25 myparser = mathtree.parser()
26 assert repr(myparser.parse("a+b-c")) == """MathTreeOpSub(
27 MathTreeOpAdd(
28 MathTreeValVar(
29 'a'),
30 MathTreeValVar(
31 'b')),
32 MathTreeValVar(
33 'c'))"""
34 assert repr(myparser.parse("(a+b)-c")) == """MathTreeOpSub(
35 MathTreeOpAdd(
36 MathTreeValVar(
37 'a'),
38 MathTreeValVar(
39 'b')),
40 MathTreeValVar(
41 'c'))"""
42 assert repr(myparser.parse("a+(b-c)")) == """MathTreeOpAdd(
43 MathTreeValVar(
44 'a'),
45 MathTreeOpSub(
46 MathTreeValVar(
47 'b'),
48 MathTreeValVar(
49 'c')))"""
50 assert repr(myparser.parse("a-b+c")) == """MathTreeOpAdd(
51 MathTreeOpSub(
52 MathTreeValVar(
53 'a'),
54 MathTreeValVar(
55 'b')),
56 MathTreeValVar(
57 'c'))"""
58 assert repr(myparser.parse("(a-b)+c")) == """MathTreeOpAdd(
59 MathTreeOpSub(
60 MathTreeValVar(
61 'a'),
62 MathTreeValVar(
63 'b')),
64 MathTreeValVar(
65 'c'))"""
66 assert repr(myparser.parse("a-(b+c)")) == """MathTreeOpSub(
67 MathTreeValVar(
68 'a'),
69 MathTreeOpAdd(
70 MathTreeValVar(
71 'b'),
72 MathTreeValVar(
73 'c')))"""
74 assert repr(myparser.parse("a+b-c*d/e**f")) == """MathTreeOpSub(
75 MathTreeOpAdd(
76 MathTreeValVar(
77 'a'),
78 MathTreeValVar(
79 'b')),
80 MathTreeOpDiv(
81 MathTreeOpMul(
82 MathTreeValVar(
83 'c'),
84 MathTreeValVar(
85 'd')),
86 MathTreeOpPow(
87 MathTreeValVar(
88 'e'),
89 MathTreeValVar(
90 'f'))))"""
91 assert repr(myparser.parse("a**b/c*d-e+f")) == """MathTreeOpAdd(
92 MathTreeOpSub(
93 MathTreeOpMul(
94 MathTreeOpDiv(
95 MathTreeOpPow(
96 MathTreeValVar(
97 'a'),
98 MathTreeValVar(
99 'b')),
100 MathTreeValVar(
101 'c')),
102 MathTreeValVar(
103 'd')),
104 MathTreeValVar(
105 'e')),
106 MathTreeValVar(
107 'f'))"""
108 assert repr(myparser.parse("((a-(b+c))/(d*e))**f")) == """MathTreeOpPow(
109 MathTreeOpDiv(
110 MathTreeOpSub(
111 MathTreeValVar(
112 'a'),
113 MathTreeOpAdd(
114 MathTreeValVar(
115 'b'),
116 MathTreeValVar(
117 'c'))),
118 MathTreeOpMul(
119 MathTreeValVar(
120 'd'),
121 MathTreeValVar(
122 'e'))),
123 MathTreeValVar(
124 'f'))"""
125 assert repr(myparser.parse("sin(pi/2)")) == """MathTreeFunc1Sin(
126 MathTreeOpDiv(
127 MathTreeValVar(
128 'pi'),
129 MathTreeValConst(
130 2.0)))"""
131 assert repr(myparser.parse("norm(a,b)")) == """MathTreeFunc2Norm(
132 MathTreeValVar(
133 'a'),
134 MathTreeValVar(
135 'b'))"""
137 def testCalc(self):
138 myparser = mathtree.parser()
139 abc = {"a": 1, "b": 2, "c": 3}
140 abcdef = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6}
141 assert abs(myparser.parse("a+b-c").Calc(abc)) <= 1e-10
142 assert abs(myparser.parse("(a+b)-c").Calc(abc)) <= 1e-10
143 assert abs(myparser.parse("a+(b-c)").Calc(abc)) <= 1e-10
144 assert abs(myparser.parse("a-b+c-2").Calc(abc)) <= 1e-10
145 assert abs(myparser.parse("(a-b)+c-2").Calc(abc)) <= 1e-10
146 assert abs(myparser.parse("a-(b+c)+4").Calc(abc)) <= 1e-10
147 assert abs(myparser.parse("a+b-c*d/e**f-2.999232").Calc(abcdef)) <= 1e-10
148 assert abs(myparser.parse("a**b/c*d-e+f-7/3").Calc(abcdef)) <= 1e-10
149 assert abs(myparser.parse("((a-(b+c))/(d*e))**f-6.4e-5").Calc(abcdef)) <= 1e-10
150 assert abs(myparser.parse("neg(1)+1").Calc({})) <= 1e-10
151 assert abs(myparser.parse("sgn(1)-1").Calc({})) <= 1e-10
152 assert abs(myparser.parse("sgn(-1)+1").Calc({})) <= 1e-10
153 assert abs(myparser.parse("sqrt(4)-2").Calc({})) <= 1e-10
154 assert abs(myparser.parse("log(e)-1").Calc({})) <= 1e-10
155 assert abs(myparser.parse("exp(1)-e").Calc({})) <= 1e-10
156 assert abs(myparser.parse("sin(pi/2)-1").Calc({})) <= 1e-10
157 assert abs(myparser.parse("cos(pi)+1").Calc({})) <= 1e-10
158 assert abs(myparser.parse("tan(pi/4)-1").Calc({})) <= 1e-10
159 assert abs(myparser.parse("asin(1) - pi/2").Calc({})) <= 1e-10
160 assert abs(myparser.parse("acos(-1) - pi").Calc({})) <= 1e-10
161 assert abs(myparser.parse("atan(1) - pi/4").Calc({})) <= 1e-10
162 assert abs(myparser.parse("sind(90)-1").Calc({})) <= 1e-10
163 assert abs(myparser.parse("cosd(180)+1").Calc({})) <= 1e-10
164 assert abs(myparser.parse("tand(45)-1").Calc({})) <= 1e-10
165 assert abs(myparser.parse("asind(1) - 90").Calc({})) <= 1e-10
166 assert abs(myparser.parse("acosd(-1) - 180").Calc({})) <= 1e-10
167 assert abs(myparser.parse("atand(1) - 45").Calc({})) <= 1e-10
168 assert abs(myparser.parse("norm(3,4)").Calc({}) - 5) <= 1e-10
170 def testExtern(self):
171 myparser = mathtree.parser()
172 a = 1
173 b = 2
174 c = 3
175 f = lambda x: x*x
176 assert abs(myparser.parse("a+b-c").Calc(locals())) <= 1e-10
177 assert abs(myparser.parse("f(2)-4", extern=locals()).Calc()) <= 1e-10
179 def testException(self):
180 myparser = mathtree.parser()
181 try:
182 myparser.parse("")
183 assert 0, "OperandExpectedMathTreeParseError expected"
184 except mathtree.OperandExpectedMathTreeParseError: pass
185 try:
186 myparser.parse("???")
187 assert 0, "OperandExpectedMathTreeParseError expected"
188 except mathtree.OperandExpectedMathTreeParseError: pass
189 try:
190 myparser.parse("sin()")
191 assert 0, "OperandExpectedMathTreeParseError expected"
192 except mathtree.OperandExpectedMathTreeParseError: pass
193 try:
194 myparser.parse("sin(x,y)")
195 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
196 except mathtree.RightParenthesisExpectedMathTreeParseError: pass
197 try:
198 myparser.parse("norm(x)")
199 assert 0, "CommaExpectedMathTreeParseError expected"
200 except mathtree.CommaExpectedMathTreeParseError: pass
201 try:
202 myparser.parse("xxx(y)")
203 assert 0, "OperatorExpectedMathTreeParseError expected"
204 except mathtree.OperatorExpectedMathTreeParseError: pass
205 try:
206 myparser.parse("(1+2")
207 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
208 except mathtree.RightParenthesisExpectedMathTreeParseError: pass
209 try:
210 myparser.parse("1+2)")
211 assert 0, "RightParenthesisFoundExpectedMathTreeParseError expected"
212 except mathtree.RightParenthesisFoundMathTreeParseError: pass
213 try:
214 myparser.parse("1,2")
215 assert 0, "CommaFoundExpectedMathTreeParseError expected"
216 except mathtree.CommaFoundMathTreeParseError: pass
218 suite = unittest.TestSuite((unittest.makeSuite(MathTreeTestCase, 'test'), ))
220 if __name__ == "__main__":
221 runner = unittest.TextTestRunner()
222 runner.run(suite)