Mapytex/pymath/calculus/test/test_expression.py

87 lines
1.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# encoding: utf-8
""" Testing Expression """
2016-01-07 16:56:30 +00:00
from pymath.calculus.expression import Expression
2016-02-27 09:01:26 +00:00
from pymath.calculus.operator import op
def test_init_from_str():
2016-01-17 15:07:05 +00:00
exp = Expression("2 + 3")
2016-02-27 09:01:26 +00:00
assert exp.postfix_tokens == [2, 3, op.add]
2016-02-13 03:49:37 +00:00
def test_init_from_exp():
pass
2016-02-13 03:49:37 +00:00
def test_init_list():
exp = Expression([2, 3, "+"])
2016-02-27 09:01:26 +00:00
assert exp.postfix_tokens == [2, 3, op.add]
2016-02-13 03:49:37 +00:00
def test_init_one_element_int_from_str():
exp = Expression("1")
2016-02-13 03:49:37 +00:00
def test_init_one_element_int_from_list():
exp = Expression([1])
2016-02-13 03:49:37 +00:00
# def test_init_one_element_str_from_str():
# exp = Expression("x")
#
2016-02-13 03:49:37 +00:00
# def test_init_one_element_str_from_list():
# exp = Expression(["x"])
2016-02-13 03:49:37 +00:00
def test_simplify_exp():
exp = Expression("1 + 2 * 3")
simplified = exp.simplify()
ans = Expression("7")
assert ans == simplified
2016-02-13 03:49:37 +00:00
# def test_simplify_frac():
# exp = Expression("1/2 - 4")
# simplified = exp.simplify()
# ans = Expression("-7/2")
# assert simplified == ans
#
2016-02-13 03:49:37 +00:00
# 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())
2016-02-13 03:49:37 +00:00
def test_add_exp():
e = Expression("12- 4")
f = Expression("4 + 1")
g = e + f
2016-02-27 09:01:26 +00:00
assert g.postfix_tokens == [12, 4, op.sub, 4, 1, op.add, op.add]
2016-02-13 03:49:37 +00:00
def test_mul_exp():
e = Expression("12- 4")
f = Expression("4 + 1")
g = e * f
2016-02-27 09:01:26 +00:00
assert g.postfix_tokens == [12, 4, op.sub, 4, 1, op.add, op.mul]
2016-02-13 03:49:37 +00:00
def test_neg_exp():
e = Expression("12- 4")
2016-02-13 03:29:26 +00:00
g = -e
2016-02-27 09:01:26 +00:00
assert g.postfix_tokens == [12, 4, op.sub, op.sub1]
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2016-02-13 03:29:26 +00:00
# cursor: 16 del