2014-02-21 10:09:12 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from pymath.expression import Expression
|
|
|
|
from pymath.fraction import Fraction
|
|
|
|
from pymath.generic import first_elem
|
2014-11-11 07:29:19 +00:00
|
|
|
from pymath.render import txt, tex
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestExpression(unittest.TestCase):
|
|
|
|
"""Testing functions from pymath.expression"""
|
|
|
|
|
2014-12-21 17:10:52 +00:00
|
|
|
def setup(self):
|
|
|
|
Expression.set_render(txt)
|
|
|
|
|
2014-02-28 09:31:56 +00:00
|
|
|
def test_init_from_str(self):
|
|
|
|
exp = Expression("2 + 3")
|
|
|
|
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_init_from_exp(self):
|
|
|
|
pass
|
|
|
|
|
2014-11-11 08:58:47 +00:00
|
|
|
def test_list(self):
|
|
|
|
exp = Expression([2, 3, "+"])
|
|
|
|
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
2014-02-28 09:31:56 +00:00
|
|
|
def test_simplify_frac(self):
|
2014-11-21 14:49:43 +00:00
|
|
|
render = lambda _,x : str(x)
|
|
|
|
Expression.set_render(render)
|
2014-02-28 09:31:56 +00:00
|
|
|
exp = Expression("1/2 - 4")
|
|
|
|
steps = ["[1, 2, '/', 4, '-']", \
|
|
|
|
"[< Fraction 1 / 2>, 4, '-']", \
|
|
|
|
"[1, 1, '*', 2, 1, '*', '/', 4, 2, '*', 1, 2, '*', '/', '-']", \
|
|
|
|
"[1, 8, '-', 2, '/']", \
|
|
|
|
'[< Fraction -7 / 2>]']
|
2014-11-21 14:49:43 +00:00
|
|
|
self.assertEqual(steps, list(exp.simplify()))
|
|
|
|
Expression.set_render(txt)
|
2014-08-29 15:15:29 +00:00
|
|
|
|
2014-11-14 15:48:38 +00:00
|
|
|
def test_add_exp(self):
|
|
|
|
e = Expression("12- 4")
|
|
|
|
f = Expression("4 + 1")
|
|
|
|
g = e + f
|
|
|
|
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "+"])
|
|
|
|
|
|
|
|
def test_mul_exp(self):
|
|
|
|
e = Expression("12- 4")
|
|
|
|
f = Expression("4 + 1")
|
|
|
|
g = e * f
|
|
|
|
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "*"])
|
|
|
|
|
|
|
|
def test_neg_exp(self):
|
|
|
|
e = Expression("12- 4")
|
|
|
|
g = -e
|
|
|
|
self.assertEqual(g.postfix_tokens, [12, 4, '-', '-'])
|
|
|
|
|
2014-02-28 09:31:56 +00:00
|
|
|
|
2014-02-21 10:09:12 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|