4 from pyx
import mathtree
6 class MathTreeTestCase(unittest
.TestCase
):
9 myparser
= mathtree
.parser()
10 assert str(myparser
.parse("a+b-c")) == "a+b-c"
11 assert str(myparser
.parse("(a+b)-c")) == "a+b-c"
12 assert str(myparser
.parse("a+(b-c)")) == "a+(b-c)" # XXX: needs no parenthesis
13 assert str(myparser
.parse("a-b+c")) == "a-b+c"
14 assert str(myparser
.parse("(a-b)+c")) == "a-b+c"
15 assert str(myparser
.parse("a-(b+c)")) == "a-(b+c)"
16 assert str(myparser
.parse("a+b-c*d/e**f")) == "a+b-c*d/e^f"
17 assert str(myparser
.parse("a**b/c*d-e+f")) == "a^b/c*d-e+f"
18 assert str(myparser
.parse("((a-(b+c))/(d*e))**f")) == "((a-(b+c))/(d*e))^f"
19 assert str(myparser
.parse("sin(pi/2)")) == "sin(pi/2.0)"
20 assert str(myparser
.parse("a+b*sin(c)**d")) == "a+b*sin(c)^d"
21 assert str(myparser
.parse("a+b*(c)")) == "a+b*c"
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("a+b*sin(c)**d")) == """MathTreeOpAdd(
143 assert repr(myparser
.parse("a+b*(c)")) == """MathTreeOpAdd(
151 assert repr(myparser
.parse("norm(a,b)")) == """MathTreeFunc2Norm(
158 myparser
= mathtree
.parser()
159 abc
= {"a": 1, "b": 2, "c": 3}
160 abcdef
= {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6}
161 assert abs(myparser
.parse("a+b-c").Calc(**abc
)) <= 1e-10
162 assert abs(myparser
.parse("(a+b)-c").Calc(**abc
)) <= 1e-10
163 assert abs(myparser
.parse("a+(b-c)").Calc(**abc
)) <= 1e-10
164 assert abs(myparser
.parse("a-b+c-2").Calc(**abc
)) <= 1e-10
165 assert abs(myparser
.parse("(a-b)+c-2").Calc(**abc
)) <= 1e-10
166 assert abs(myparser
.parse("a-(b+c)+4").Calc(**abc
)) <= 1e-10
167 assert abs(myparser
.parse("a+b-c*d/e**f-2.999232").Calc(**abcdef
)) <= 1e-10
168 assert abs(myparser
.parse("a**b/c*d-e+f-7/3").Calc(**abcdef
)) <= 1e-10
169 assert abs(myparser
.parse("((a-(b+c))/(d*e))**f-6.4e-5").Calc(**abcdef
)) <= 1e-10
170 assert abs(myparser
.parse("-a**2+1").Calc(**abc
)) <= 1e-10
171 assert abs(myparser
.parse("-1**2+1").Calc(**abc
)) <= 1e-10
172 assert abs(myparser
.parse("1-a**2").Calc(**abc
)) <= 1e-10
173 assert abs(myparser
.parse("--1-a**2").Calc(**abc
)) <= 1e-10
174 # assert abs(myparser.parse("1+-a**2").Calc(**abc)) <= 1e-10
175 assert abs(myparser
.parse("neg(1)+1").Calc()) <= 1e-10
176 assert abs(myparser
.parse("abs(2)-2").Calc()) <= 1e-10
177 assert abs(myparser
.parse("abs(-2)-2").Calc()) <= 1e-10
178 assert abs(myparser
.parse("sgn(1)-1").Calc()) <= 1e-10
179 assert abs(myparser
.parse("sgn(-1)+1").Calc()) <= 1e-10
180 assert abs(myparser
.parse("sqrt(4)-2").Calc()) <= 1e-10
181 assert abs(myparser
.parse("log(e)-1").Calc()) <= 1e-10
182 assert abs(myparser
.parse("exp(1)-e").Calc()) <= 1e-10
183 assert abs(myparser
.parse("sin(pi/2)-1").Calc()) <= 1e-10
184 assert abs(myparser
.parse("cos(pi)+1").Calc()) <= 1e-10
185 assert abs(myparser
.parse("tan(pi/4)-1").Calc()) <= 1e-10
186 assert abs(myparser
.parse("asin(1) - pi/2").Calc()) <= 1e-10
187 assert abs(myparser
.parse("acos(-1) - pi").Calc()) <= 1e-10
188 assert abs(myparser
.parse("atan(1) - pi/4").Calc()) <= 1e-10
189 assert abs(myparser
.parse("sind(90)-1").Calc()) <= 1e-10
190 assert abs(myparser
.parse("cosd(180)+1").Calc()) <= 1e-10
191 assert abs(myparser
.parse("tand(45)-1").Calc()) <= 1e-10
192 assert abs(myparser
.parse("asind(1) - 90").Calc()) <= 1e-10
193 assert abs(myparser
.parse("acosd(-1) - 180").Calc()) <= 1e-10
194 assert abs(myparser
.parse("atand(1) - 45").Calc()) <= 1e-10
195 assert abs(myparser
.parse("norm(3,4)").Calc() - 5) <= 1e-10
197 def testExtern(self
):
198 myparser
= mathtree
.parser()
199 assert abs(myparser
.parse("a+b-c").Calc(a
=1, b
=2, c
=3)) <= 1e-10
200 assert abs(myparser
.parse("f(2)-4").Calc(f
=lambda x
: x
*x
)) <= 1e-10
202 def testException(self
):
203 myparser
= mathtree
.parser()
206 assert 0, "OperandExpectedMathTreeParseError expected"
207 except mathtree
.OperandExpectedMathTreeParseError
: pass
209 myparser
.parse("???")
210 assert 0, "OperandExpectedMathTreeParseError expected"
211 except mathtree
.OperandExpectedMathTreeParseError
: pass
213 myparser
.parse("sin()")
214 assert 0, "OperandExpectedMathTreeParseError expected"
215 except mathtree
.OperandExpectedMathTreeParseError
: pass
217 myparser
.parse("sin(x,y)")
218 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
219 except mathtree
.RightParenthesisExpectedMathTreeParseError
: pass
221 myparser
.parse("norm(x)")
222 assert 0, "CommaExpectedMathTreeParseError expected"
223 except mathtree
.CommaExpectedMathTreeParseError
: pass
225 myparser
.parse("xxx yyy")
226 assert 0, "OperatorExpectedMathTreeParseError expected"
227 except mathtree
.OperatorExpectedMathTreeParseError
: pass
229 myparser
.parse("(1+2")
230 assert 0, "RightParenthesisExpectedMathTreeParseError expected"
231 except mathtree
.RightParenthesisExpectedMathTreeParseError
: pass
233 myparser
.parse("1+2)")
234 assert 0, "RightParenthesisFoundExpectedMathTreeParseError expected"
235 except mathtree
.RightParenthesisFoundMathTreeParseError
: pass
237 myparser
.parse("1,2")
238 assert 0, "CommaFoundExpectedMathTreeParseError expected"
239 except mathtree
.CommaFoundMathTreeParseError
: pass
241 suite
= unittest
.TestSuite((unittest
.makeSuite(MathTreeTestCase
, 'test'), ))
243 if __name__
== "__main__":
244 runner
= unittest
.TextTestRunner()