autopep8 on calculus test

This commit is contained in:
Benjamin Bertrand 2016-02-13 06:49:37 +03:00
parent bc4f1fe384
commit e43544006e
11 changed files with 1805 additions and 280 deletions

File diff suppressed because one or more lines are too long

View File

@ -4,29 +4,29 @@
from pymath.calculus import arithmetic from pymath.calculus import arithmetic
def test_gcd_commu(): def test_gcd_commu():
assert arithmetic.gcd(3, 15) == arithmetic.gcd(15,3) assert arithmetic.gcd(3, 15) == arithmetic.gcd(15, 3)
def test_gcd1(): def test_gcd1():
assert arithmetic.gcd(3, 15) == 3 assert arithmetic.gcd(3, 15) == 3
def test_gcd2(): def test_gcd2():
assert arithmetic.gcd(14, 21) == 7 assert arithmetic.gcd(14, 21) == 7
def test_gcd_prem(): def test_gcd_prem():
assert arithmetic.gcd(14, 19) == 1 assert arithmetic.gcd(14, 19) == 1
def test_gcd_neg(): def test_gcd_neg():
assert arithmetic.gcd(3, -15) == 3 assert arithmetic.gcd(3, -15) == 3
assert arithmetic.gcd(-3, -15) == -3 assert arithmetic.gcd(-3, -15) == -3
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -14,38 +14,43 @@ def test_init_from_str():
exp = Expression("2 + 3") exp = Expression("2 + 3")
assert exp.postfix_tokens == [2, 3, "+"] assert exp.postfix_tokens == [2, 3, "+"]
def test_init_from_exp(): def test_init_from_exp():
pass pass
def test_init_list(): def test_init_list():
exp = Expression([2, 3, "+"]) exp = Expression([2, 3, "+"])
assert exp.postfix_tokens == [2, 3, "+"] assert exp.postfix_tokens == [2, 3, "+"]
def test_init_one_element_int_from_str(): def test_init_one_element_int_from_str():
exp = Expression("1") exp = Expression("1")
def test_init_one_element_int_from_list(): def test_init_one_element_int_from_list():
exp = Expression([1]) exp = Expression([1])
#def test_init_one_element_str_from_str(): # def test_init_one_element_str_from_str():
# exp = Expression("x") # exp = Expression("x")
# #
#def test_init_one_element_str_from_list(): # def test_init_one_element_str_from_list():
# exp = Expression(["x"]) # exp = Expression(["x"])
def test_simplify_exp(): def test_simplify_exp():
exp = Expression("1 + 2 * 3") exp = Expression("1 + 2 * 3")
simplified = exp.simplify() simplified = exp.simplify()
ans = Expression("7") ans = Expression("7")
assert ans == simplified assert ans == simplified
#def test_simplify_frac(): # def test_simplify_frac():
# exp = Expression("1/2 - 4") # exp = Expression("1/2 - 4")
# simplified = exp.simplify() # simplified = exp.simplify()
# ans = Expression("-7/2") # ans = Expression("-7/2")
# assert simplified == ans # assert simplified == ans
# #
#def test_explain_frac(): # def test_explain_frac():
# exp = Expression("1/2 - 4") # exp = Expression("1/2 - 4")
# simplified = exp.simplify() # simplified = exp.simplify()
# #
@ -56,18 +61,21 @@ def test_simplify_exp():
# '\\frac{ -7 }{ 2 }'] # '\\frac{ -7 }{ 2 }']
# assert simplified.steps == list(exp.simplify()) # assert simplified.steps == list(exp.simplify())
def test_add_exp(): def test_add_exp():
e = Expression("12- 4") e = Expression("12- 4")
f = Expression("4 + 1") f = Expression("4 + 1")
g = e + f g = e + f
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "+"] assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "+"]
def test_mul_exp(): def test_mul_exp():
e = Expression("12- 4") e = Expression("12- 4")
f = Expression("4 + 1") f = Expression("4 + 1")
g = e * f g = e * f
assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "*"] assert g.postfix_tokens == [12, 4, '-', 4, 1, "+", "*"]
def test_neg_exp(): def test_neg_exp():
e = Expression("12- 4") e = Expression("12- 4")
g = -e g = -e

View File

@ -5,23 +5,33 @@ import unittest
from pymath.calculus.fraction import Fraction from pymath.calculus.fraction import Fraction
class TestFraction(unittest.TestCase): class TestFraction(unittest.TestCase):
"""Testing functions from pymath.calculus.Fraction""" """Testing functions from pymath.calculus.Fraction"""
def setUp(self): def setUp(self):
self.listFrom = [Fraction(1,3), 1] self.listFrom = [Fraction(1, 3), 1]
self.listAgainst = [ Fraction(1,3), \ self.listAgainst = [Fraction(1, 3),
Fraction(2,3), \ Fraction(2, 3),
Fraction(4,5), \ Fraction(4, 5),
Fraction(-1, 3), \ Fraction(-1, 3),
Fraction(1,-3), \ Fraction(1, -3),
1, 1,
] ]
def test_add(self): def test_add(self):
ans = [[Fraction(2, 3), 1, Fraction(17, 15), 0, 0, Fraction(4,3)], \ ans = [
[Fraction(4,3), Fraction(5,3), Fraction(9,5), Fraction(2,3), Fraction(2,3), 2] \ [
] Fraction(
2, 3), 1, Fraction(
17, 15), 0, 0, Fraction(
4, 3)], [
Fraction(
4, 3), Fraction(
5, 3), Fraction(
9, 5), Fraction(
2, 3), Fraction(
2, 3), 2]]
for (i, f1) in enumerate(self.listFrom): for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst): for (j, f2) in enumerate(self.listAgainst):
@ -29,9 +39,9 @@ class TestFraction(unittest.TestCase):
self.assertEqual(res, ans[i][j]) self.assertEqual(res, ans[i][j])
def test_sub(self): def test_sub(self):
ans = [[0, Fraction(-1,3), Fraction(-7, 15), Fraction(2,3), Fraction(2,3), Fraction(-2,3)], \ ans = [[0, Fraction(-1, 3), Fraction(-7, 15), Fraction(2, 3), Fraction(2, 3), Fraction(-2, 3)],
[Fraction(2,3), Fraction(1,3), Fraction(1,5), Fraction(4,3), Fraction(4,3), 0] \ [Fraction(2, 3), Fraction(1, 3), Fraction(1, 5), Fraction(4, 3), Fraction(4, 3), 0]
] ]
for (i, f1) in enumerate(self.listFrom): for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst): for (j, f2) in enumerate(self.listAgainst):
@ -39,21 +49,20 @@ class TestFraction(unittest.TestCase):
self.assertEqual(res, ans[i][j]) self.assertEqual(res, ans[i][j])
def test_neg(self): def test_neg(self):
ans = [ Fraction(-1,3), \ ans = [Fraction(-1, 3),
Fraction(-2,3), \ Fraction(-2, 3),
Fraction(-4,5), \ Fraction(-4, 5),
Fraction(1, 3), \ Fraction(1, 3),
Fraction(1,3), \ Fraction(1, 3),
-1 -1
] ]
for (j, f) in enumerate(self.listAgainst): for (j, f) in enumerate(self.listAgainst):
res = -f res = -f
self.assertEqual(res, ans[j]) self.assertEqual(res, ans[j])
def test_mul(self): def test_mul(self):
ans = [[Fraction(1, 9), Fraction(2,9), Fraction(4, 15), Fraction(-1,9), Fraction(-1,9), Fraction(1,3)], \ ans = [[Fraction(1, 9), Fraction(2, 9), Fraction(4, 15), Fraction(-1, 9), Fraction(-1, 9), Fraction(
[ Fraction(1,3), Fraction(2,3), Fraction(4,5), Fraction(-1, 3), Fraction(1,-3), 1] \ 1, 3)], [Fraction(1, 3), Fraction(2, 3), Fraction(4, 5), Fraction(-1, 3), Fraction(1, -3), 1]]
]
for (i, f1) in enumerate(self.listFrom): for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst): for (j, f2) in enumerate(self.listAgainst):
@ -61,9 +70,9 @@ class TestFraction(unittest.TestCase):
self.assertEqual(res, ans[i][j]) self.assertEqual(res, ans[i][j])
def test_truediv(self): def test_truediv(self):
ans = [[1, Fraction(1,2), Fraction(5, 12), -1, -1, Fraction(1,3)], \ ans = [[1, Fraction(1, 2), Fraction(5, 12), -1, -1, Fraction(1, 3)],
[3, Fraction(3,2), Fraction(5,4), -3, -3, 1] \ [3, Fraction(3, 2), Fraction(5, 4), -3, -3, 1]
] ]
for (i, f1) in enumerate(self.listFrom): for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst): for (j, f2) in enumerate(self.listAgainst):

View File

@ -6,14 +6,15 @@ import unittest
from pymath.calculus import generic from pymath.calculus import generic
class TestGeneric(unittest.TestCase): class TestGeneric(unittest.TestCase):
"""Testing functions from pymath.calculus.generic""" """Testing functions from pymath.calculus.generic"""
def test_flatten_list1(self): def test_flatten_list1(self):
l = [1, [2,3], [[4,5], 6], 7] l = [1, [2, 3], [[4, 5], 6], 7]
flat_l = generic.flatten_list(l) flat_l = generic.flatten_list(l)
true_flat = list(range(1,8)) true_flat = list(range(1, 8))
self.assertEqual(flat_l, true_flat) self.assertEqual(flat_l, true_flat)
@ -30,7 +31,7 @@ class TestGeneric(unittest.TestCase):
l = range(10) l = range(10)
first = generic.first_elem(l) first = generic.first_elem(l)
self.assertAlmostEqual(0,first) self.assertAlmostEqual(0, first)
s = "plopplop" s = "plopplop"
first = generic.first_elem(s) first = generic.first_elem(s)
@ -38,7 +39,7 @@ class TestGeneric(unittest.TestCase):
def test_first_elem_iter_in_iter(self): def test_first_elem_iter_in_iter(self):
""" Interable in iterable """ """ Interable in iterable """
l = [[1,2],[4, 5, [6,7,8]], 9] l = [[1, 2], [4, 5, [6, 7, 8]], 9]
first = generic.first_elem(l) first = generic.first_elem(l)
self.assertAlmostEqual(first, 1) self.assertAlmostEqual(first, 1)
@ -53,12 +54,12 @@ class TestGeneric(unittest.TestCase):
self.assertAlmostEqual(first, "a") self.assertAlmostEqual(first, "a")
l = ["abc",[4, 5, [6,7,8]], 9] l = ["abc", [4, 5, [6, 7, 8]], 9]
first = generic.first_elem(l) first = generic.first_elem(l)
self.assertAlmostEqual(first, "a") self.assertAlmostEqual(first, "a")
l = [["abc",1],[4, 5, [6,7,8]], 9] l = [["abc", 1], [4, 5, [6, 7, 8]], 9]
first = generic.first_elem(l) first = generic.first_elem(l)
self.assertAlmostEqual(first, "a") self.assertAlmostEqual(first, "a")
@ -67,9 +68,6 @@ if __name__ == '__main__':
unittest.main() unittest.main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -7,25 +7,29 @@ from pymath.calculus.operator import op
# Test de op.add # Test de op.add
def test_add_render_tex(): def test_add_render_tex():
assert op.add.__tex__('1','2') == '1 + 2' assert op.add.__tex__('1', '2') == '1 + 2'
assert op.add.__tex__('1','-2') == '1 - 2' assert op.add.__tex__('1', '-2') == '1 - 2'
def test_add_render_txt(): def test_add_render_txt():
assert op.add.__txt__('1','2') == '1 + 2' assert op.add.__txt__('1', '2') == '1 + 2'
assert op.add.__txt__('1','-2') == '1 - 2' assert op.add.__txt__('1', '-2') == '1 - 2'
# Test de op.sub # Test de op.sub
def test_sub_render_tex(): def test_sub_render_tex():
assert op.sub.__tex__('1','2') == '1 - 2' assert op.sub.__tex__('1', '2') == '1 - 2'
assert op.sub.__tex__('1','-2') == '1 - ( -2 )' assert op.sub.__tex__('1', '-2') == '1 - ( -2 )'
def test_sub_render_txt(): def test_sub_render_txt():
assert op.sub.__txt__('1','2') == '1 - 2' assert op.sub.__txt__('1', '2') == '1 - 2'
assert op.sub.__txt__('1','-2') == '1 - ( -2 )' assert op.sub.__txt__('1', '-2') == '1 - ( -2 )'
# Test de op.sub1 # Test de op.sub1
def test_sub1_render(): def test_sub1_render():
assert op.sub1.__tex__('1') == '- 1' assert op.sub1.__tex__('1') == '- 1'
assert op.sub1.__tex__('-1') == '- ( -1 )' assert op.sub1.__tex__('-1') == '- ( -1 )'
@ -33,56 +37,62 @@ def test_sub1_render():
assert op.sub1.__txt__('-1') == '- ( -1 )' assert op.sub1.__txt__('-1') == '- ( -1 )'
# Test de op.mul # Test de op.mul
def test_mul_render_tex(): def test_mul_render_tex():
assert op.mul.__tex__('1','2') == '1 \\times 2' assert op.mul.__tex__('1', '2') == '1 \\times 2'
assert op.mul.__tex__('1','-2') == '1 \\times ( -2 )' assert op.mul.__tex__('1', '-2') == '1 \\times ( -2 )'
def test_mul_render_txt(): def test_mul_render_txt():
assert op.mul.__txt__('1','2') == '1 * 2' assert op.mul.__txt__('1', '2') == '1 * 2'
assert op.mul.__txt__('1','-2') == '1 * ( -2 )' assert op.mul.__txt__('1', '-2') == '1 * ( -2 )'
def test_mul_is_visible(): def test_mul_is_visible():
assert op.mul.is_visible(2,3) == True assert op.mul.is_visible(2, 3)
assert op.mul.is_visible(2,-3) == True assert op.mul.is_visible(2, -3)
assert op.mul.is_visible(-2,3) == True assert op.mul.is_visible(-2, 3)
assert op.mul.is_visible('a',2) == True assert op.mul.is_visible('a', 2)
assert op.mul.is_visible('(2a + 1)', 2) == True assert op.mul.is_visible('(2a + 1)', 2)
assert op.mul.is_visible(2, '(-2)') == True assert op.mul.is_visible(2, '(-2)')
assert op.mul.is_visible(2, '2a') == True assert op.mul.is_visible(2, '2a')
assert op.mul.is_visible(2, '(-2a)') == True assert op.mul.is_visible(2, '(-2a)')
assert op.mul.is_visible(2, '(-2abc)') == True assert op.mul.is_visible(2, '(-2abc)')
assert op.mul.is_visible(2,'a') == False assert op.mul.is_visible(2, 'a') == False
assert op.mul.is_visible(2, '(2a + 1)') == False assert op.mul.is_visible(2, '(2a + 1)') == False
assert op.mul.is_visible('(3x - 1)', '(2a + 1)') == False assert op.mul.is_visible('(3x - 1)', '(2a + 1)') == False
assert op.mul.is_visible(2, '(-2x + 1)(3x + 2)') == False assert op.mul.is_visible(2, '(-2x + 1)(3x + 2)') == False
# Test de op.div # Test de op.div
def test_div_render_tex(): def test_div_render_tex():
assert op.div.__tex__('1','2') == '\\frac{ 1 }{ 2 }' assert op.div.__tex__('1', '2') == '\\frac{ 1 }{ 2 }'
assert op.div.__tex__('1','-2') == '\\frac{ 1 }{ -2 }' assert op.div.__tex__('1', '-2') == '\\frac{ 1 }{ -2 }'
def test_div_render_txt(): def test_div_render_txt():
assert op.div.__txt__('1','2') == '1 / 2' assert op.div.__txt__('1', '2') == '1 / 2'
assert op.div.__txt__('1','-2') == '1 / ( -2 )' assert op.div.__txt__('1', '-2') == '1 / ( -2 )'
# Test de op.pw # Test de op.pw
def test_pw_render_tex(): def test_pw_render_tex():
assert op.pw.__tex__('1','2') == '1^{ 2 }' assert op.pw.__tex__('1', '2') == '1^{ 2 }'
#assert op.pw.__tex__('1','-2') == '1^{-2}' #assert op.pw.__tex__('1','-2') == '1^{-2}'
#assert op.pw.__tex__('-1','2') == '( -1 )^{ 2 }' #assert op.pw.__tex__('-1','2') == '( -1 )^{ 2 }'
def test_pw_render_txt(): def test_pw_render_txt():
assert op.pw.__txt__('1','2') == '1 ^ 2' assert op.pw.__txt__('1', '2') == '1 ^ 2'
assert op.pw.__txt__('1','-2') == '1 ^ ( -2 )' assert op.pw.__txt__('1', '-2') == '1 ^ ( -2 )'
#assert op.pw.__txt__('-1','2') == '( -1 ) ^ 2 ' #assert op.pw.__txt__('-1','2') == '( -1 ) ^ 2 '
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -22,10 +22,10 @@ class TestPolynom(unittest.TestCase):
def test_init_multi(self): def test_init_multi(self):
p = Polynom([1, [2, 3], 4], "x") p = Polynom([1, [2, 3], 4], "x")
#def test_init_arith(self): # def test_init_arith(self):
# p = Polynom([1, [2, 3, "+"], 4], "x") # p = Polynom([1, [2, 3, "+"], 4], "x")
#def test_init_arith_2(self): # def test_init_arith_2(self):
# p = Polynom([1, [[2, 3, "*"],3], 4], "x") # p = Polynom([1, [[2, 3, "*"],3], 4], "x")
def test_deg(self): def test_deg(self):
@ -45,66 +45,67 @@ class TestPolynom(unittest.TestCase):
def test_eval_poly(self): def test_eval_poly(self):
p = Polynom([1, 2]) p = Polynom([1, 2])
self.assertEqual(p("h+1"), Polynom([3,2], "h")) self.assertEqual(p("h+1"), Polynom([3, 2], "h"))
#def test_print(self): # def test_print(self):
# p = Polynom([1,2,3]) # p = Polynom([1,2,3])
# ans = "1 + 2 x + 3 x^2" # ans = "1 + 2 x + 3 x^2"
# self.assertEqual(ans, str(p)) # self.assertEqual(ans, str(p))
#def test_print_monom(self): # def test_print_monom(self):
# p = Polynom([0,2]) # p = Polynom([0,2])
# ans = "2 x" # ans = "2 x"
# self.assertEqual(ans, str(p)) # self.assertEqual(ans, str(p))
#def test_print_0_coef(self): # def test_print_0_coef(self):
# p = Polynom([0,1,3]) # p = Polynom([0,1,3])
# ans = "x + 3 x^2" # ans = "x + 3 x^2"
# self.assertEqual(ans, str(p)) # self.assertEqual(ans, str(p))
#def test_print_multi_coef(self): # def test_print_multi_coef(self):
# p = Polynom([1,[2, -2],3]) # p = Polynom([1,[2, -2],3])
# ans = "1 + 2 x - 2 x + 3 x^2" # ans = "1 + 2 x - 2 x + 3 x^2"
# self.assertEqual(ans, str(p)) # self.assertEqual(ans, str(p))
def test_postfix(self): def test_postfix(self):
p = Polynom([1,2,3]) p = Polynom([1, 2, 3])
#ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"] #ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"]
ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+'] ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_monom(self): def test_postfix_monom(self):
p = Polynom([0,2]) p = Polynom([0, 2])
ans = [2, "x", "*"] ans = [2, "x", "*"]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_0_coef(self): def test_postfix_0_coef(self):
p = Polynom([0,2,0,4]) p = Polynom([0, 2, 0, 4])
#ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"] #ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"]
ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+'] ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_1_coef(self): def test_postfix_1_coef(self):
p = Polynom([0,1,1]) p = Polynom([0, 1, 1])
#ans = ["x", "x", 2, "^", "+"] #ans = ["x", "x", 2, "^", "+"]
ans = ["x", 2, "^", "x", "+"] ans = ["x", 2, "^", "x", "+"]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_neg_coef(self): def test_postfix_neg_coef(self):
# TODO: Choix arbitraire (vis à vis des + et des -) il faudra faire en fonction de render |sam. juin 14 09:45:55 CEST 2014 # TODO: Choix arbitraire (vis à vis des + et des -) il faudra faire en
p = Polynom([-1,-2,-3]) # fonction de render |sam. juin 14 09:45:55 CEST 2014
p = Polynom([-1, -2, -3])
#ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"] #ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"]
ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-'] ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_multi_coef(self): def test_postfix_multi_coef(self):
p = Polynom([1,[2, 3],4]) p = Polynom([1, [2, 3], 4])
#ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"] #ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"]
ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+'] ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_arithm_coef(self): def test_postfix_arithm_coef(self):
p = Polynom([1,Expression([2, 3, "+"]),4]) p = Polynom([1, Expression([2, 3, "+"]), 4])
#ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"] #ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"]
ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+'] ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
@ -115,7 +116,7 @@ class TestPolynom(unittest.TestCase):
def test_reduce(self): def test_reduce(self):
p = Polynom([1, [2, 3], 4]) p = Polynom([1, [2, 3], 4])
ans = '4 x^{ 2 } + 5 x + 1' ans = '4 x^{ 2 } + 5 x + 1'
self.assertEqual(str(p.reduce()), ans) self.assertEqual(str(p.reduce()), ans)
def test_add_int(self): def test_add_int(self):
@ -129,7 +130,7 @@ class TestPolynom(unittest.TestCase):
f = Fraction(1, 2) f = Fraction(1, 2)
q = p + f q = p + f
ans = '3 x^{ 2 } + 2 x + \\frac{ 3 }{ 2 }' ans = '3 x^{ 2 } + 2 x + \\frac{ 3 }{ 2 }'
self.assertEqual(str(q),ans) self.assertEqual(str(q), ans)
def test_add_poly(self): def test_add_poly(self):
p = Polynom([1, 0, 3]) p = Polynom([1, 0, 3])
@ -142,7 +143,7 @@ class TestPolynom(unittest.TestCase):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
q = p - 2 q = p - 2
ans = '3 x^{ 2 } + 2 x - 1' ans = '3 x^{ 2 } + 2 x - 1'
self.assertEqual(str(q),ans ) self.assertEqual(str(q), ans)
def test_sub_frac(self): def test_sub_frac(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
@ -159,31 +160,23 @@ class TestPolynom(unittest.TestCase):
self.assertEqual(str(r), ans) self.assertEqual(str(r), ans)
def test_pow_monome(self): def test_pow_monome(self):
p = Polynom([0,-2]) p = Polynom([0, -2])
r = p**3 r = p**3
ans = '- 8 x^{ 3 }' ans = '- 8 x^{ 3 }'
self.assertEqual(str(r), ans) self.assertEqual(str(r), ans)
def test_pow2_monome(self): def test_pow2_monome(self):
p = Polynom([0,-2]) p = Polynom([0, -2])
r = p^3 r = p ^ 3
ans = '- 8 x^{ 3 }' ans = '- 8 x^{ 3 }'
self.assertEqual(str(r), ans) self.assertEqual(str(r), ans)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -7,7 +7,6 @@ import unittest
from pymath.calculus.polynomDeg2 import Polynom_deg2 from pymath.calculus.polynomDeg2 import Polynom_deg2
class TestPolynomDeg2(unittest.TestCase): class TestPolynomDeg2(unittest.TestCase):
"""Testing functions from pymath.calculus.polynomDeg2""" """Testing functions from pymath.calculus.polynomDeg2"""
@ -18,14 +17,7 @@ if __name__ == '__main__':
unittest.main() unittest.main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -2,7 +2,6 @@
# encoding: utf-8 # encoding: utf-8
from pymath.calculus.random_expression import RdExpression from pymath.calculus.random_expression import RdExpression
@ -17,6 +16,7 @@ def test_only_form():
assert set(rdExp._gene_varia.keys()) == {'a'} assert set(rdExp._gene_varia.keys()) == {'a'}
assert set(rdExp._gene_2replaced.keys()) == {'a'} assert set(rdExp._gene_2replaced.keys()) == {'a'}
def test_form_with_underscores(): def test_form_with_underscores():
form = "_ + 2*_" form = "_ + 2*_"
rdExp = RdExpression(form) rdExp = RdExpression(form)
@ -28,6 +28,7 @@ def test_form_with_underscores():
assert set(rdExp._gene_varia.keys()) == {'A', 'B'} assert set(rdExp._gene_varia.keys()) == {'A', 'B'}
assert set(rdExp._gene_2replaced.keys()) == {'A', 'B'} assert set(rdExp._gene_2replaced.keys()) == {'A', 'B'}
def test_only_form_calc(): def test_only_form_calc():
form = "{a+b} + 2" form = "{a+b} + 2"
rdExp = RdExpression(form) rdExp = RdExpression(form)
@ -39,6 +40,7 @@ def test_only_form_calc():
assert set(rdExp._gene_varia.keys()), {'a' == 'b'} assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
assert set(rdExp._gene_2replaced.keys()) == {'a+b'} assert set(rdExp._gene_2replaced.keys()) == {'a+b'}
def test_only_form_cond(): def test_only_form_cond():
form = "{a} + 2" form = "{a} + 2"
cond = ["{a} == 3"] cond = ["{a} == 3"]
@ -53,6 +55,7 @@ def test_only_form_cond():
assert rdExp._gene_varia['a'] == 3 assert rdExp._gene_varia['a'] == 3
def test_only_form_conds(): def test_only_form_conds():
form = "{a} + 2" form = "{a} + 2"
cond = ["{a} in list(range(5))", "{a} % 2 == 1"] cond = ["{a} in list(range(5))", "{a} % 2 == 1"]
@ -68,6 +71,7 @@ def test_only_form_conds():
assert rdExp._gene_varia['a'] in list(range(5)) assert rdExp._gene_varia['a'] in list(range(5))
assert rdExp._gene_varia['a'] % 2 == 1 assert rdExp._gene_varia['a'] % 2 == 1
def test_only_form_calc_cond(): def test_only_form_calc_cond():
form = "{a*3} * {b}" form = "{a*3} * {b}"
cond = ["{a} == 3"] cond = ["{a} == 3"]
@ -82,6 +86,7 @@ def test_only_form_calc_cond():
assert rdExp._gene_varia['a'] == 3 assert rdExp._gene_varia['a'] == 3
def test_only_form_calc_cond_calc(): def test_only_form_calc_cond_calc():
form = "{a*3} * {b}" form = "{a*3} * {b}"
cond = ["{a+b} == 3"] cond = ["{a+b} == 3"]
@ -97,9 +102,7 @@ def test_only_form_calc_cond_calc():
assert (rdExp._gene_varia['a'] + rdExp._gene_varia['b']) == 3 assert (rdExp._gene_varia['a'] + rdExp._gene_varia['b']) == 3
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -4,7 +4,7 @@
import unittest import unittest
from pymath.calculus.render import tex, txt,p2i from pymath.calculus.render import tex, txt, p2i
from pymath.calculus.fraction import Fraction from pymath.calculus.fraction import Fraction
from pymath.calculus.polynom import Polynom from pymath.calculus.polynom import Polynom
from pymath.calculus.operator import op from pymath.calculus.operator import op
@ -13,18 +13,20 @@ from pymath.calculus.expression import Expression
from itertools import permutations from itertools import permutations
def mass_poly_test(operation, rg= 5): def mass_poly_test(operation, rg=5):
""" @todo """ @todo
:op: the operation :op: the operation
:rg: number of potential values for coefs :rg: number of potential values for coefs
:returns: @todo :returns: @todo
""" """
coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(rg),2)] coefs_p = [[(i - 2), (j - 2)] for i, j in permutations(range(rg), 2)]
coefs_q = [[2*(i-2),2*(j-2)] for i,j in permutations(range(rg),2)] coefs_q = [[2 * (i - 2), 2 * (j - 2)]
for i, j in permutations(range(rg), 2)]
l_p = [Polynom(i) for i in coefs_p] l_p = [Polynom(i) for i in coefs_p]
l_q = [Polynom(i) for i in coefs_q] l_q = [Polynom(i) for i in coefs_q]
return [Expression([l_p[i],l_q[j],op.get_op(operation)]) for i,j in permutations(range(len(coefs_p)),2)] return [Expression([l_p[i], l_q[j], op.get_op(operation)])
for i, j in permutations(range(len(coefs_p)), 2)]
class TestTexRender(unittest.TestCase): class TestTexRender(unittest.TestCase):
@ -37,163 +39,165 @@ class TestTexRender(unittest.TestCase):
self.assertEqual(tex(["a"]), "a") self.assertEqual(tex(["a"]), "a")
def test_type_render_fraction(self): def test_type_render_fraction(self):
self.assertEqual(tex([Fraction(1,2)]), "\\frac{ 1 }{ 2 }") self.assertEqual(tex([Fraction(1, 2)]), "\\frac{ 1 }{ 2 }")
def test_type_render_poly(self): def test_type_render_poly(self):
P = Polynom([1,2,3]) P = Polynom([1, 2, 3])
self.assertEqual(tex([P]), "3 x^{ 2 } + 2 x + 1") self.assertEqual(tex([P]), "3 x^{ 2 } + 2 x + 1")
def test_add_interger(self): def test_add_interger(self):
exps = [ [2, 3, op.add ], exps = [[2, 3, op.add],
[2, -3, op.add ], [2, -3, op.add],
[-2, 3, op.add ], [-2, 3, op.add],
] ]
wanted_render = [ "2 + 3", wanted_render = ["2 + 3",
"2 - 3", "2 - 3",
"-2 + 3", "-2 + 3",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mass_add(self): def test_mass_add(self):
permu = mass_poly_test("+",5) permu = mass_poly_test("+", 5)
from .mass_test import POLY_ADD_VALID_RESULTS from .mass_test import POLY_ADD_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(tex(v), POLY_ADD_VALID_RESULTS[i]) self.assertEqual(tex(v), POLY_ADD_VALID_RESULTS[i])
def test_mass_sub(self): def test_mass_sub(self):
permu = mass_poly_test("-",5) permu = mass_poly_test("-", 5)
from .mass_test import POLY_SUB_VALID_RESULTS from .mass_test import POLY_SUB_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(tex(v), POLY_SUB_VALID_RESULTS[i]) self.assertEqual(tex(v), POLY_SUB_VALID_RESULTS[i])
def test_mass_mul(self): def test_mass_mul(self):
permu = mass_poly_test("*",5) permu = mass_poly_test("*", 5)
from .mass_test import TEX_POLY_MUL_VALID_RESULTS from .mass_test import TEX_POLY_MUL_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(tex(v), TEX_POLY_MUL_VALID_RESULTS[i]) self.assertEqual(tex(v), TEX_POLY_MUL_VALID_RESULTS[i])
def test_add_letter(self): def test_add_letter(self):
exps = [[2, "a", op.add ], exps = [[2, "a", op.add],
["a", 3, op.add ], ["a", 3, op.add],
[-2, "a", op.add ], [-2, "a", op.add],
["a", -2, op.add ], ["a", -2, op.add],
] ]
wanted_render = [ "2 + a", wanted_render = ["2 + a",
"a + 3", "a + 3",
"-2 + a", "-2 + a",
"a - 2", "a - 2",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_add_fraction(self): def test_add_fraction(self):
exps = [[2, Fraction(1,2), op.add], exps = [[2, Fraction(1, 2), op.add],
[Fraction(1,2), 3, op.add], [Fraction(1, 2), 3, op.add],
] ]
wanted_render = [ "2 + \\frac{ 1 }{ 2 }", wanted_render = ["2 + \\frac{ 1 }{ 2 }",
"\\frac{ 1 }{ 2 } + 3", "\\frac{ 1 }{ 2 } + 3",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_add_poly(self): def test_add_poly(self):
exps = [[2, Polynom([1,2,3]), op.mul], exps = [[2, Polynom([1, 2, 3]), op.mul],
[Polynom([1,2,3]), 2, op.mul], [Polynom([1, 2, 3]), 2, op.mul],
[Polynom([1,2,3]), Polynom([4,5,6]), op.mul], [Polynom([1, 2, 3]), Polynom([4, 5, 6]), op.mul],
] ]
wanted_render = [ "2 ( 3 x^{ 2 } + 2 x + 1 )", wanted_render = ["2 ( 3 x^{ 2 } + 2 x + 1 )",
"( 3 x^{ 2 } + 2 x + 1 ) \\times 2", "( 3 x^{ 2 } + 2 x + 1 ) \\times 2",
"( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )", "( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_interger(self): def test_mult_interger(self):
exps = [[2, 3, op.mul], exps = [[2, 3, op.mul],
[2, -3, op.mul], [2, -3, op.mul],
[-2, 3, op.mul], [-2, 3, op.mul],
] ]
wanted_render = [ "2 \\times 3", wanted_render = ["2 \\times 3",
"2 \\times ( -3 )", "2 \\times ( -3 )",
"-2 \\times 3", "-2 \\times 3",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_letter(self): def test_mult_letter(self):
exps = [[2, "a", op.mul], exps = [[2, "a", op.mul],
["a", 3, op.mul], ["a", 3, op.mul],
[-2, "a", op.mul], [-2, "a", op.mul],
["a", -2, op.mul], ["a", -2, op.mul],
] ]
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"] wanted_render = ["2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_fraction(self): def test_mult_fraction(self):
exps = [ [2, Fraction(1,2), op.mul], [Fraction(1,2), 3, op.mul]] exps = [[2, Fraction(1, 2), op.mul], [Fraction(1, 2), 3, op.mul]]
wanted_render = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 2 } \\times 3"] wanted_render = [
for (i,e) in enumerate(exps): "2 \\times \\frac{ 1 }{ 2 }",
"\\frac{ 1 }{ 2 } \\times 3"]
for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_poly(self): def test_mult_poly(self):
exps = [[2, Polynom([1,2,3]), op.mul], exps = [[2, Polynom([1, 2, 3]), op.mul],
[Polynom([1,2,3]), 2, op.mul], [Polynom([1, 2, 3]), 2, op.mul],
[Polynom([1,2,3]), Polynom([4,5,6]), op.mul], [Polynom([1, 2, 3]), Polynom([4, 5, 6]), op.mul],
] ]
wanted_render = [ "2 ( 3 x^{ 2 } + 2 x + 1 )", wanted_render = ["2 ( 3 x^{ 2 } + 2 x + 1 )",
"( 3 x^{ 2 } + 2 x + 1 ) \\times 2", "( 3 x^{ 2 } + 2 x + 1 ) \\times 2",
"( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )", "( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_parentheses_int(self): def test_parentheses_int(self):
exps = [\ exps = [
[ 2, 3, op.add, 4, op.mul],\ [2, 3, op.add, 4, op.mul],
[ 2, 3, op.mul, 4, op.add],\ [2, 3, op.mul, 4, op.add],
[ 2, 3, 4, op.mul, op.add],\ [2, 3, 4, op.mul, op.add],
[ 2, 3, 4, op.add, op.add],\ [2, 3, 4, op.add, op.add],
[ 2, 3, 4, op.add, op.sub],\ [2, 3, 4, op.add, op.sub],
] ]
wanted_render = [\ wanted_render = [
'( 2 + 3 ) \\times 4',\ '( 2 + 3 ) \\times 4',
'2 \\times 3 + 4',\ '2 \\times 3 + 4',
'2 + 3 \\times 4',\ '2 + 3 \\times 4',
'2 + 3 + 4',\ '2 + 3 + 4',
'2 - ( 3 + 4 )',\ '2 - ( 3 + 4 )',
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_parentheses_poly(self): def test_parentheses_poly(self):
P = Polynom([1,2,3]) P = Polynom([1, 2, 3])
Q = Polynom([4,5,6]) Q = Polynom([4, 5, 6])
exps = [\ exps = [
[ 2, P, op.add],\ [2, P, op.add],
[ 2, P, op.sub],\ [2, P, op.sub],
[ 2, P, P, op.mul, op.sub],\ [2, P, P, op.mul, op.sub],
[ Q, P, op.add],\ [Q, P, op.add],
[ Q, P, op.sub],\ [Q, P, op.sub],
] ]
wanted_render = [\ wanted_render = [
'2 + 3 x^{ 2 } + 2 x + 1' ,\ '2 + 3 x^{ 2 } + 2 x + 1',
'2 - ( 3 x^{ 2 } + 2 x + 1 )' ,\ '2 - ( 3 x^{ 2 } + 2 x + 1 )',
'2 - ( 3 x^{ 2 } + 2 x + 1 ) ( 3 x^{ 2 } + 2 x + 1 )' ,\ '2 - ( 3 x^{ 2 } + 2 x + 1 ) ( 3 x^{ 2 } + 2 x + 1 )',
'6 x^{ 2 } + 5 x + 4 + 3 x^{ 2 } + 2 x + 1' ,\ '6 x^{ 2 } + 5 x + 4 + 3 x^{ 2 } + 2 x + 1',
'6 x^{ 2 } + 5 x + 4 - ( 3 x^{ 2 } + 2 x + 1 )' ,\ '6 x^{ 2 } + 5 x + 4 - ( 3 x^{ 2 } + 2 x + 1 )',
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
@ -201,8 +205,6 @@ class TestTexRender(unittest.TestCase):
pass pass
class TesttxtRender(unittest.TestCase): class TesttxtRender(unittest.TestCase):
"""Testing functions from pymath.calculus.renders.txt""" """Testing functions from pymath.calculus.renders.txt"""
@ -213,60 +215,60 @@ class TesttxtRender(unittest.TestCase):
self.assertEqual(txt(["a"]), "a") self.assertEqual(txt(["a"]), "a")
def test_type_render_fraction(self): def test_type_render_fraction(self):
self.assertEqual(txt([Fraction(1,2)]), "1 / 2") self.assertEqual(txt([Fraction(1, 2)]), "1 / 2")
def test_mult_interger(self): def test_mult_interger(self):
exps = [ [2, 3, op.mul], \ exps = [[2, 3, op.mul],
[2, -3, op.mul], \ [2, -3, op.mul],
[-2, 3, op.mul]] [-2, 3, op.mul]]
wanted_render = [ "2 * 3", "2 * ( -3 )", "-2 * 3"] wanted_render = ["2 * 3", "2 * ( -3 )", "-2 * 3"]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = txt(e) rend = txt(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_letter(self): def test_mult_letter(self):
exps = [ [2, "a", op.mul], \ exps = [[2, "a", op.mul],
["a", 3, op.mul], \ ["a", 3, op.mul],
[-2, "a", op.mul], \ [-2, "a", op.mul],
["a", -2, op.mul], ["a", -2, op.mul],
["a", -2, op.mul, -2, op.mul], ["a", -2, op.mul, -2, op.mul],
["a", -2, op.mul, "a", op.mul], ["a", -2, op.mul, "a", op.mul],
] ]
wanted_render = [ "2 a", wanted_render = ["2 a",
"a * 3", "a * 3",
"-2 a", "-2 a",
"a * ( -2 )", "a * ( -2 )",
"a * ( -2 ) * ( -2 )", "a * ( -2 ) * ( -2 )",
"a * ( -2 ) a", "a * ( -2 ) a",
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = txt(e) rend = txt(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_mult_fraction(self): def test_mult_fraction(self):
exps = [ [2, Fraction(1,2), op.mul], \ exps = [[2, Fraction(1, 2), op.mul],
[Fraction(1,2), 3, op.mul]] [Fraction(1, 2), 3, op.mul]]
wanted_render = [ "2 * 1 / 2", "1 / 2 * 3"] wanted_render = ["2 * 1 / 2", "1 / 2 * 3"]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = txt(e) rend = txt(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
def test_parentheses(self): def test_parentheses(self):
mul = op.get_op("*", 2) mul = op.get_op("*", 2)
add = op.get_op("+", 2) add = op.get_op("+", 2)
exps = [\ exps = [
[ 2, 3, add, 4, mul],\ [2, 3, add, 4, mul],
[ 2, 3, mul, 4, add],\ [2, 3, mul, 4, add],
[ 2, 3, 4, mul, add],\ [2, 3, 4, mul, add],
[ 2, 3, 4, add, add],\ [2, 3, 4, add, add],
] ]
wanted_render = [\ wanted_render = [
'( 2 + 3 ) * 4',\ '( 2 + 3 ) * 4',
'2 * 3 + 4',\ '2 * 3 + 4',
'2 + 3 * 4',\ '2 + 3 * 4',
'2 + 3 + 4',\ '2 + 3 + 4',
] ]
for (i,e) in enumerate(exps): for (i, e) in enumerate(exps):
rend = txt(e) rend = txt(e)
self.assertEqual(rend, wanted_render[i]) self.assertEqual(rend, wanted_render[i])
@ -274,21 +276,21 @@ class TesttxtRender(unittest.TestCase):
pass pass
def test_mass_add(self): def test_mass_add(self):
permu = mass_poly_test("+",5) permu = mass_poly_test("+", 5)
from .mass_test import POLY_ADD_VALID_RESULTS from .mass_test import POLY_ADD_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(txt(v), POLY_ADD_VALID_RESULTS[i]) self.assertEqual(txt(v), POLY_ADD_VALID_RESULTS[i])
def test_mass_sub(self): def test_mass_sub(self):
permu = mass_poly_test("-",5) permu = mass_poly_test("-", 5)
from .mass_test import POLY_SUB_VALID_RESULTS from .mass_test import POLY_SUB_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(txt(v), POLY_SUB_VALID_RESULTS[i]) self.assertEqual(txt(v), POLY_SUB_VALID_RESULTS[i])
def test_mass_mul(self): def test_mass_mul(self):
permu = mass_poly_test("*",5) permu = mass_poly_test("*", 5)
from .mass_test import TXT_POLY_MUL_VALID_RESULTS from .mass_test import TXT_POLY_MUL_VALID_RESULTS
for (i,v) in enumerate(permu): for (i, v) in enumerate(permu):
self.assertEqual(txt(v), TXT_POLY_MUL_VALID_RESULTS[i]) self.assertEqual(txt(v), TXT_POLY_MUL_VALID_RESULTS[i])
@ -296,15 +298,7 @@ if __name__ == '__main__':
unittest.main() unittest.main()
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del

View File

@ -7,6 +7,7 @@ import unittest
from pymath.calculus.str2tokens import str2tokens, str2in_tokens, in2post_fix from pymath.calculus.str2tokens import str2tokens, str2in_tokens, in2post_fix
from pymath.calculus.polynom import Polynom from pymath.calculus.polynom import Polynom
class TestStr2tokens(unittest.TestCase): class TestStr2tokens(unittest.TestCase):
"""Testing functions from pymath.calculus.str2tokens""" """Testing functions from pymath.calculus.str2tokens"""
@ -17,7 +18,6 @@ class TestStr2tokens(unittest.TestCase):
ans = str2in_tokens("2*3+4") ans = str2in_tokens("2*3+4")
self.assertEqual(ans, [2, "*", 3, "+", 4]) self.assertEqual(ans, [2, "*", 3, "+", 4])
def test_in2post_fix(self): def test_in2post_fix(self):
in_tokens = str2in_tokens("2+3*4") in_tokens = str2in_tokens("2+3*4")
ans = in2post_fix(in_tokens) ans = in2post_fix(in_tokens)
@ -25,10 +25,10 @@ class TestStr2tokens(unittest.TestCase):
in_tokens = str2in_tokens("2*3+4") in_tokens = str2in_tokens("2*3+4")
ans = in2post_fix(in_tokens) ans = in2post_fix(in_tokens)
self.assertEqual(ans, [2, 3,"*", 4, "+"]) self.assertEqual(ans, [2, 3, "*", 4, "+"])
# TODO: Ajouter des tests pour les cas particuliers... |sam. nov. 8
# TODO: Ajouter des tests pour les cas particuliers... |sam. nov. 8 17:39:18 CET 2014 # 17:39:18 CET 2014
def test_str2in_tokens_big_num(self): def test_str2in_tokens_big_num(self):
exp = "123 + 3" exp = "123 + 3"
tok = str2in_tokens(exp) tok = str2in_tokens(exp)
@ -42,14 +42,13 @@ class TestStr2tokens(unittest.TestCase):
def test_str2in_tokens_time_lack(self): def test_str2in_tokens_time_lack(self):
exp = "(-3)(2)" exp = "(-3)(2)"
tok = str2in_tokens(exp) tok = str2in_tokens(exp)
self.assertEqual(tok, ["(", -3, ")", "*","(", 2, ")" ]) self.assertEqual(tok, ["(", -3, ")", "*", "(", 2, ")"])
def test_str2tokens_poly(self): def test_str2tokens_poly(self):
exp = "2x + 4" exp = "2x + 4"
post = str2in_tokens(exp) post = str2in_tokens(exp)
self.assertEqual(post, [2, "*", Polynom([0, 1]), '+', 4]) self.assertEqual(post, [2, "*", Polynom([0, 1]), '+', 4])
def test_str2tokens_poly_double_x(self): def test_str2tokens_poly_double_x(self):
exp = "xx + 4" exp = "xx + 4"
post = str2in_tokens(exp) post = str2in_tokens(exp)
@ -58,12 +57,13 @@ class TestStr2tokens(unittest.TestCase):
def test_str2tokens_poly(self): def test_str2tokens_poly(self):
exp = "x(2+1) + 4" exp = "x(2+1) + 4"
post = str2in_tokens(exp) post = str2in_tokens(exp)
self.assertEqual(post, [Polynom([0, 1]), "*", "(", 2, "+", 1, ')', '+', 4]) self.assertEqual(post, [Polynom([0, 1]), "*",
"(", 2, "+", 1, ')', '+', 4])
def test_str2in_tokens_time_lack2(self): def test_str2in_tokens_time_lack2(self):
exp = "-3(2)" exp = "-3(2)"
tok = str2in_tokens(exp) tok = str2in_tokens(exp)
self.assertEqual(tok, [-3, "*","(", 2, ")" ]) self.assertEqual(tok, [-3, "*", "(", 2, ")"])
def test_str2tokens_error_float(self): def test_str2tokens_error_float(self):
exp = "1 + 1.3" exp = "1 + 1.3"
@ -74,7 +74,6 @@ class TestStr2tokens(unittest.TestCase):
self.assertRaises(ValueError, str2tokens, exp) self.assertRaises(ValueError, str2tokens, exp)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
@ -83,4 +82,3 @@ if __name__ == '__main__':
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del # cursor: 16 del