6 from pyx
import mathtree
8 class MathTreeTestCase(unittest
.TestCase
):
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)"
25 myparser
= mathtree
.parser()
26 assert repr(myparser
.parse("a+b-c")) == """MathTreeOpSub(
34 assert repr(myparser
.parse("(a+b)-c")) == """MathTreeOpSub(
42 assert repr(myparser
.parse("a+(b-c)")) == """MathTreeOpAdd(
50 assert repr(myparser
.parse("a-b+c")) == """MathTreeOpAdd(
58 assert repr(myparser
.parse("(a-b)+c")) == """MathTreeOpAdd(
66 assert repr(myparser
.parse("a-(b+c)")) == """MathTreeOpSub(
74 assert repr(myparser
.parse("a+b-c*d/e**f")) == """MathTreeOpSub(
91 assert repr(myparser
.parse("a**b/c*d-e+f")) == """MathTreeOpAdd(
108 assert repr(myparser
.parse("((a-(b+c))/(d*e))**f")) == """MathTreeOpPow(
125 assert repr(myparser
.parse("sin(pi/2)")) == """MathTreeFunc1Sin(
131 assert repr(myparser
.parse("norm(a,b)")) == """MathTreeFunc2Norm(
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()
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()
183 assert 0, "OperandExpectedMathTreeParseError expected"
184 except mathtree
.OperandExpectedMathTreeParseError
: pass
186 myparser
.parse("???")
187 assert 0, "OperandExpectedMathTreeParseError expected"
188 except mathtree
.OperandExpectedMathTreeParseError
: pass
190 myparser
.parse("sin()")
191 assert 0, "OperandExpectedMathTreeParseError expected"
192 except mathtree
.OperandExpectedMathTreeParseError
: pass
194 myparser
.parse("sin(x,y)")
195 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
196 except mathtree
.RightParenthesisExpectedMathTreeParseError
: pass
198 myparser
.parse("norm(x)")
199 assert 0, "CommaExpectedMathTreeParseError expected"
200 except mathtree
.CommaExpectedMathTreeParseError
: pass
202 myparser
.parse("xxx(y)")
203 assert 0, "OperatorExpectedMathTreeParseError expected"
204 except mathtree
.OperatorExpectedMathTreeParseError
: pass
206 myparser
.parse("(1+2")
207 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
208 except mathtree
.RightParenthesisExpectedMathTreeParseError
: pass
210 myparser
.parse("1+2)")
211 assert 0, "RightParenthesisFoundExpectedMathTreeParseError expected"
212 except mathtree
.RightParenthesisFoundMathTreeParseError
: pass
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()