Simplify test_expression for py.test and adapt it for explain method
This commit is contained in:
parent
6d8b1786b3
commit
3c21b42d6c
@ -1,63 +1,77 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
|
""" Testing Expression """
|
||||||
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from pymath.expression import Expression
|
from pymath.expression import Expression
|
||||||
from pymath.fraction import Fraction
|
from pymath.fraction import Fraction
|
||||||
from pymath.generic import first_elem
|
from pymath.generic import first_elem
|
||||||
from pymath.render import txt, tex
|
from pymath.render import txt, tex
|
||||||
|
|
||||||
|
|
||||||
class TestExpression(unittest.TestCase):
|
def test_init_from_str():
|
||||||
"""Testing functions from pymath.expression"""
|
|
||||||
|
|
||||||
def setup(self):
|
|
||||||
Expression.set_render(txt)
|
|
||||||
|
|
||||||
def test_init_from_str(self):
|
|
||||||
exp = Expression("2 + 3")
|
exp = Expression("2 + 3")
|
||||||
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
assert exp.postfix_tokens == [2, 3, "+"]
|
||||||
|
|
||||||
def test_init_from_exp(self):
|
def test_init_from_exp():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_list(self):
|
def test_init_list():
|
||||||
exp = Expression([2, 3, "+"])
|
exp = Expression([2, 3, "+"])
|
||||||
self.assertEqual(exp.postfix_tokens, [2, 3, "+"])
|
assert exp.postfix_tokens == [2, 3, "+"]
|
||||||
|
|
||||||
def test_simplify_frac(self):
|
def test_init_one_element_int_from_str():
|
||||||
exp = Expression("1/2 - 4")
|
exp = Expression("1")
|
||||||
steps = ['\\frac{ 1 }{ 2 } - 4', \
|
|
||||||
'\\frac{ 1 \\times 1 }{ 2 \\times 1 } - \\frac{ 4 \\times 2 }{ 1 \\times 2 }',\
|
|
||||||
'\\frac{ 1 }{ 2 } - \\frac{ 8 }{ 2 }',\
|
|
||||||
'\\frac{ 1 - 8 }{ 2 }',\
|
|
||||||
'\\frac{ -7 }{ 2 }']
|
|
||||||
self.assertEqual(steps, list(exp.simplify()))
|
|
||||||
Expression.set_render(txt)
|
|
||||||
|
|
||||||
def test_add_exp(self):
|
def test_init_one_element_int_from_list():
|
||||||
e = Expression("12- 4")
|
exp = Expression([1])
|
||||||
f = Expression("4 + 1")
|
|
||||||
g = e + f
|
|
||||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "+"])
|
|
||||||
|
|
||||||
def test_mul_exp(self):
|
#def test_init_one_element_str_from_str():
|
||||||
e = Expression("12- 4")
|
# exp = Expression("x")
|
||||||
f = Expression("4 + 1")
|
#
|
||||||
g = e * f
|
#def test_init_one_element_str_from_list():
|
||||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', 4, 1, "+", "*"])
|
# exp = Expression(["x"])
|
||||||
|
|
||||||
def test_neg_exp(self):
|
def test_simplify_exp():
|
||||||
e = Expression("12- 4")
|
exp = Expression("1 + 2 * 3")
|
||||||
g = -e
|
simplified = exp.simplify()
|
||||||
self.assertEqual(g.postfix_tokens, [12, 4, '-', '-'])
|
ans = Expression("7")
|
||||||
|
assert ans == simplified
|
||||||
|
|
||||||
|
#def test_simplify_frac():
|
||||||
|
# exp = Expression("1/2 - 4")
|
||||||
|
# simplified = exp.simplify()
|
||||||
|
# ans = Expression("-7/2")
|
||||||
|
# assert simplified == ans
|
||||||
|
#
|
||||||
|
#def test_explain_frac():
|
||||||
|
# exp = Expression("1/2 - 4")
|
||||||
|
# simplified = exp.simplify()
|
||||||
|
#
|
||||||
|
# steps = ['\\frac{ 1 }{ 2 } - 4', \
|
||||||
|
# '\\frac{ 1 \\times 1 }{ 2 \\times 1 } - \\frac{ 4 \\times 2 }{ 1 \\times 2 }',\
|
||||||
|
# '\\frac{ 1 }{ 2 } - \\frac{ 8 }{ 2 }',\
|
||||||
|
# '\\frac{ 1 - 8 }{ 2 }',\
|
||||||
|
# '\\frac{ -7 }{ 2 }']
|
||||||
|
# assert simplified.steps == list(exp.simplify())
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def test_add_exp():
|
||||||
unittest.main()
|
e = Expression("12- 4")
|
||||||
|
f = Expression("4 + 1")
|
||||||
|
g = e + f
|
||||||
|
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "+"]
|
||||||
|
|
||||||
|
def test_mul_exp():
|
||||||
|
e = Expression("12- 4")
|
||||||
|
f = Expression("4 + 1")
|
||||||
|
g = e * f
|
||||||
|
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "*"]
|
||||||
|
|
||||||
|
def test_neg_exp():
|
||||||
|
e = Expression("12- 4")
|
||||||
|
g = -e
|
||||||
|
assert g.postfix_tokens == [12, 4, '-', '-']
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user