move tests
This commit is contained in:
0
pymath/calculus/test/__init__.py
Normal file
0
pymath/calculus/test/__init__.py
Normal file
4
pymath/calculus/test/mass_test.py
Normal file
4
pymath/calculus/test/mass_test.py
Normal file
File diff suppressed because one or more lines are too long
32
pymath/calculus/test/test_arithmetic.py
Normal file
32
pymath/calculus/test/test_arithmetic.py
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
from pymath.calculus import arithmetic
|
||||
|
||||
def test_gcd_commu():
|
||||
assert arithmetic.gcd(3, 15) == arithmetic.gcd(15,3)
|
||||
|
||||
def test_gcd1():
|
||||
assert arithmetic.gcd(3, 15) == 3
|
||||
|
||||
def test_gcd2():
|
||||
assert arithmetic.gcd(14, 21) == 7
|
||||
|
||||
def test_gcd_prem():
|
||||
assert arithmetic.gcd(14, 19) == 1
|
||||
|
||||
def test_gcd_neg():
|
||||
assert arithmetic.gcd(3, -15) == 3
|
||||
assert arithmetic.gcd(-3, -15) == -3
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
80
pymath/calculus/test/test_expression.py
Normal file
80
pymath/calculus/test/test_expression.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
""" Testing Expression """
|
||||
|
||||
|
||||
from pymath.calculus.expression import Expression
|
||||
from pymath.calculus.fraction import Fraction
|
||||
from pymath.calculus.generic import first_elem
|
||||
from pymath.calculus.render import txt, tex
|
||||
|
||||
|
||||
def test_init_from_str():
|
||||
exp = Expression("2 + 3")
|
||||
assert exp.postfix_tokens == [2, 3, "+"]
|
||||
|
||||
def test_init_from_exp():
|
||||
pass
|
||||
|
||||
def test_init_list():
|
||||
exp = Expression([2, 3, "+"])
|
||||
assert exp.postfix_tokens == [2, 3, "+"]
|
||||
|
||||
def test_init_one_element_int_from_str():
|
||||
exp = Expression("1")
|
||||
|
||||
def test_init_one_element_int_from_list():
|
||||
exp = Expression([1])
|
||||
|
||||
#def test_init_one_element_str_from_str():
|
||||
# exp = Expression("x")
|
||||
#
|
||||
#def test_init_one_element_str_from_list():
|
||||
# exp = Expression(["x"])
|
||||
|
||||
def test_simplify_exp():
|
||||
exp = Expression("1 + 2 * 3")
|
||||
simplified = exp.simplify()
|
||||
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())
|
||||
|
||||
def test_add_exp():
|
||||
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, '-', '-']
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
95
pymath/calculus/test/test_fraction.py
Normal file
95
pymath/calculus/test/test_fraction.py
Normal file
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.fraction import Fraction
|
||||
|
||||
class TestFraction(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.Fraction"""
|
||||
|
||||
def setUp(self):
|
||||
self.listFrom = [Fraction(1,3), 1]
|
||||
self.listAgainst = [ Fraction(1,3), \
|
||||
Fraction(2,3), \
|
||||
Fraction(4,5), \
|
||||
Fraction(-1, 3), \
|
||||
Fraction(1,-3), \
|
||||
1,
|
||||
]
|
||||
|
||||
def test_add(self):
|
||||
ans = [[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 (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 + f2
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_sub(self):
|
||||
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] \
|
||||
]
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 - f2
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_neg(self):
|
||||
ans = [ Fraction(-1,3), \
|
||||
Fraction(-2,3), \
|
||||
Fraction(-4,5), \
|
||||
Fraction(1, 3), \
|
||||
Fraction(1,3), \
|
||||
-1
|
||||
]
|
||||
for (j, f) in enumerate(self.listAgainst):
|
||||
res = -f
|
||||
self.assertEqual(res, ans[j])
|
||||
|
||||
def test_mul(self):
|
||||
ans = [[Fraction(1, 9), Fraction(2,9), Fraction(4, 15), Fraction(-1,9), Fraction(-1,9), Fraction(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 (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 * f2
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_truediv(self):
|
||||
ans = [[1, Fraction(1,2), Fraction(5, 12), -1, -1, Fraction(1,3)], \
|
||||
[3, Fraction(3,2), Fraction(5,4), -3, -3, 1] \
|
||||
]
|
||||
|
||||
for (i, f1) in enumerate(self.listFrom):
|
||||
for (j, f2) in enumerate(self.listAgainst):
|
||||
res = f1 / f2
|
||||
self.assertEqual(res, ans[i][j])
|
||||
|
||||
def test_lt(self):
|
||||
pass
|
||||
|
||||
def test_le(self):
|
||||
pass
|
||||
|
||||
def test_tex(self):
|
||||
f = Fraction(2, 3)
|
||||
ans = "\\frac{ 2 }{ 3 }"
|
||||
self.assertEqual(f.__tex__(), ans)
|
||||
|
||||
def test_txt(self):
|
||||
f = Fraction(2, 3)
|
||||
ans = "2 / 3"
|
||||
self.assertEqual(f.__txt__(), ans)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
76
pymath/calculus/test/test_generic.py
Normal file
76
pymath/calculus/test/test_generic.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath import generic
|
||||
|
||||
class TestGeneric(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.generic"""
|
||||
|
||||
def test_flatten_list1(self):
|
||||
l = [1, [2,3], [[4,5], 6], 7]
|
||||
flat_l = generic.flatten_list(l)
|
||||
|
||||
true_flat = list(range(1,8))
|
||||
|
||||
self.assertEqual(flat_l, true_flat)
|
||||
|
||||
def test_flatten_list2(self):
|
||||
l = list(range(10))
|
||||
flat_l = generic.flatten_list(l)
|
||||
|
||||
true_flat = list(range(10))
|
||||
|
||||
self.assertEqual(flat_l, true_flat)
|
||||
|
||||
def test_first_elem_simple_iter(self):
|
||||
""" For simple iterable """
|
||||
l = range(10)
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(0,first)
|
||||
|
||||
s = "plopplop"
|
||||
first = generic.first_elem(s)
|
||||
self.assertAlmostEqual("p", first)
|
||||
|
||||
def test_first_elem_iter_in_iter(self):
|
||||
""" Interable in iterable """
|
||||
l = [[1,2],[4, 5, [6,7,8]], 9]
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(first, 1)
|
||||
|
||||
l = [[[1]]]
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(first, 1)
|
||||
|
||||
l = ["abc"]
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(first, "a")
|
||||
|
||||
l = ["abc",[4, 5, [6,7,8]], 9]
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(first, "a")
|
||||
|
||||
l = [["abc",1],[4, 5, [6,7,8]], 9]
|
||||
first = generic.first_elem(l)
|
||||
|
||||
self.assertAlmostEqual(first, "a")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
88
pymath/calculus/test/test_operator.py
Normal file
88
pymath/calculus/test/test_operator.py
Normal file
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
from pymath.calculus.operator import op
|
||||
|
||||
|
||||
# Test de op.add
|
||||
|
||||
def test_add_render_tex():
|
||||
assert op.add.__tex__('1','2') == '1 + 2'
|
||||
assert op.add.__tex__('1','-2') == '1 - 2'
|
||||
|
||||
def test_add_render_txt():
|
||||
assert op.add.__txt__('1','2') == '1 + 2'
|
||||
assert op.add.__txt__('1','-2') == '1 - 2'
|
||||
|
||||
# Test de op.sub
|
||||
|
||||
def test_sub_render_tex():
|
||||
assert op.sub.__tex__('1','2') == '1 - 2'
|
||||
assert op.sub.__tex__('1','-2') == '1 - ( -2 )'
|
||||
|
||||
def test_sub_render_txt():
|
||||
assert op.sub.__txt__('1','2') == '1 - 2'
|
||||
assert op.sub.__txt__('1','-2') == '1 - ( -2 )'
|
||||
|
||||
# Test de op.sub1
|
||||
|
||||
def test_sub1_render():
|
||||
assert op.sub1.__tex__('1') == '- 1'
|
||||
assert op.sub1.__tex__('-1') == '- ( -1 )'
|
||||
assert op.sub1.__txt__('1') == '- 1'
|
||||
assert op.sub1.__txt__('-1') == '- ( -1 )'
|
||||
|
||||
# Test de op.mul
|
||||
def test_mul_render_tex():
|
||||
assert op.mul.__tex__('1','2') == '1 \\times 2'
|
||||
assert op.mul.__tex__('1','-2') == '1 \\times ( -2 )'
|
||||
|
||||
def test_mul_render_txt():
|
||||
assert op.mul.__txt__('1','2') == '1 * 2'
|
||||
assert op.mul.__txt__('1','-2') == '1 * ( -2 )'
|
||||
|
||||
def test_mul_is_visible():
|
||||
assert op.mul.is_visible(2,3) == True
|
||||
assert op.mul.is_visible(2,-3) == True
|
||||
assert op.mul.is_visible(-2,3) == True
|
||||
assert op.mul.is_visible('a',2) == True
|
||||
assert op.mul.is_visible('(2a + 1)', 2) == True
|
||||
assert op.mul.is_visible(2, '(-2)') == True
|
||||
assert op.mul.is_visible(2, '2a') == True
|
||||
assert op.mul.is_visible(2, '(-2a)') == True
|
||||
assert op.mul.is_visible(2, '(-2abc)') == True
|
||||
|
||||
assert op.mul.is_visible(2,'a') == 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(2, '(-2x + 1)(3x + 2)') == False
|
||||
|
||||
# Test de op.div
|
||||
def test_div_render_tex():
|
||||
assert op.div.__tex__('1','2') == '\\frac{ 1 }{ 2 }'
|
||||
assert op.div.__tex__('1','-2') == '\\frac{ 1 }{ -2 }'
|
||||
|
||||
def test_div_render_txt():
|
||||
assert op.div.__txt__('1','2') == '1 / 2'
|
||||
assert op.div.__txt__('1','-2') == '1 / ( -2 )'
|
||||
|
||||
# Test de op.pw
|
||||
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 }'
|
||||
|
||||
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 '
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
189
pymath/calculus/test/test_polynom.py
Normal file
189
pymath/calculus/test/test_polynom.py
Normal file
@@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.polynom import Polynom
|
||||
from pymath.calculus.fraction import Fraction
|
||||
from pymath.calculus.expression import Expression
|
||||
from pymath.calculus.render import txt
|
||||
|
||||
|
||||
class TestPolynom(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.polynom"""
|
||||
|
||||
def setup(self):
|
||||
Expression.set_render(txt)
|
||||
|
||||
def test_init(self):
|
||||
p = Polynom([1, 2, 3], "x")
|
||||
|
||||
def test_init_multi(self):
|
||||
p = Polynom([1, [2, 3], 4], "x")
|
||||
|
||||
#def test_init_arith(self):
|
||||
# p = Polynom([1, [2, 3, "+"], 4], "x")
|
||||
|
||||
#def test_init_arith_2(self):
|
||||
# p = Polynom([1, [[2, 3, "*"],3], 4], "x")
|
||||
|
||||
def test_deg(self):
|
||||
pass
|
||||
|
||||
def test_eval_base(self):
|
||||
p = Polynom([1, 2])
|
||||
self.assertEqual(p(3), 7)
|
||||
|
||||
def test_eval_const(self):
|
||||
p = Polynom([1])
|
||||
self.assertEqual(p(3), 1)
|
||||
|
||||
def test_eval_const_neg(self):
|
||||
p = Polynom([-1])
|
||||
self.assertEqual(p(3), -1)
|
||||
|
||||
def test_eval_poly(self):
|
||||
p = Polynom([1, 2])
|
||||
self.assertEqual(p("h+1"), Polynom([3,2], "h"))
|
||||
|
||||
#def test_print(self):
|
||||
# p = Polynom([1,2,3])
|
||||
# ans = "1 + 2 x + 3 x^2"
|
||||
# self.assertEqual(ans, str(p))
|
||||
|
||||
#def test_print_monom(self):
|
||||
# p = Polynom([0,2])
|
||||
# ans = "2 x"
|
||||
# self.assertEqual(ans, str(p))
|
||||
|
||||
#def test_print_0_coef(self):
|
||||
# p = Polynom([0,1,3])
|
||||
# ans = "x + 3 x^2"
|
||||
# self.assertEqual(ans, str(p))
|
||||
|
||||
#def test_print_multi_coef(self):
|
||||
# p = Polynom([1,[2, -2],3])
|
||||
# ans = "1 + 2 x - 2 x + 3 x^2"
|
||||
# self.assertEqual(ans, str(p))
|
||||
|
||||
def test_postfix(self):
|
||||
p = Polynom([1,2,3])
|
||||
#ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"]
|
||||
ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_monom(self):
|
||||
p = Polynom([0,2])
|
||||
ans = [2, "x", "*"]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_0_coef(self):
|
||||
p = Polynom([0,2,0,4])
|
||||
#ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"]
|
||||
ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+']
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_1_coef(self):
|
||||
p = Polynom([0,1,1])
|
||||
#ans = ["x", "x", 2, "^", "+"]
|
||||
ans = ["x", 2, "^", "x", "+"]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
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
|
||||
p = Polynom([-1,-2,-3])
|
||||
#ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"]
|
||||
ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-']
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_multi_coef(self):
|
||||
p = Polynom([1,[2, 3],4])
|
||||
#ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
||||
ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_arithm_coef(self):
|
||||
p = Polynom([1,Expression([2, 3, "+"]),4])
|
||||
#ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"]
|
||||
ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+']
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_reduce_nilpo(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
self.assertEqual(p, p.reduce())
|
||||
|
||||
def test_reduce(self):
|
||||
p = Polynom([1, [2, 3], 4])
|
||||
ans = '4 x^{ 2 } + 5 x + 1'
|
||||
self.assertEqual(str(p.reduce()), ans)
|
||||
|
||||
def test_add_int(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
q = p + 2
|
||||
ans = '3 x^{ 2 } + 2 x + 3'
|
||||
self.assertEqual(str(q), ans)
|
||||
|
||||
def test_add_frac(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
f = Fraction(1, 2)
|
||||
q = p + f
|
||||
ans = '3 x^{ 2 } + 2 x + \\frac{ 3 }{ 2 }'
|
||||
self.assertEqual(str(q),ans)
|
||||
|
||||
def test_add_poly(self):
|
||||
p = Polynom([1, 0, 3])
|
||||
q = Polynom([0, 2, 3])
|
||||
r = p + q
|
||||
ans = '6 x^{ 2 } + 2 x + 1'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_sub_int(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
q = p - 2
|
||||
ans = '3 x^{ 2 } + 2 x - 1'
|
||||
self.assertEqual(str(q),ans )
|
||||
|
||||
def test_sub_frac(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
f = Fraction(1, 2)
|
||||
q = p - f
|
||||
ans = '3 x^{ 2 } + 2 x + \\frac{ 1 }{ 2 }'
|
||||
self.assertEqual(str(q), ans)
|
||||
|
||||
def test_sub_poly(self):
|
||||
p = Polynom([1, 0, 2])
|
||||
q = Polynom([0, 2, 3])
|
||||
r = p - q
|
||||
ans = '- x^{ 2 } - 2 x + 1'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_pow_monome(self):
|
||||
p = Polynom([0,-2])
|
||||
r = p**3
|
||||
ans = '- 8 x^{ 3 }'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
def test_pow2_monome(self):
|
||||
p = Polynom([0,-2])
|
||||
r = p^3
|
||||
ans = '- 8 x^{ 3 }'
|
||||
self.assertEqual(str(r), ans)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
31
pymath/calculus/test/test_polynomDeg2.py
Normal file
31
pymath/calculus/test/test_polynomDeg2.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.polynomDeg2 import Polynom_deg2
|
||||
|
||||
|
||||
|
||||
class TestPolynomDeg2(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.polynomDeg2"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
109
pymath/calculus/test/test_random_expression.py
Normal file
109
pymath/calculus/test/test_random_expression.py
Normal file
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.random_expression import RdExpression
|
||||
|
||||
|
||||
class TestRandomExpression(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.random_expression"""
|
||||
|
||||
def test_only_form(self):
|
||||
form = "{a} + 2"
|
||||
rdExp = RdExpression(form)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a'})
|
||||
self.assertEqual(rdExp._2replaced, {'a'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
|
||||
|
||||
def test_only_form_calc(self):
|
||||
form = "{a+b} + 2"
|
||||
rdExp = RdExpression(form)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a', 'b'})
|
||||
self.assertEqual(rdExp._2replaced, {'a+b'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a+b'})
|
||||
|
||||
def test_only_form_cond(self):
|
||||
form = "{a} + 2"
|
||||
cond = ["{a} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a'})
|
||||
self.assertEqual(rdExp._2replaced, {'a'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
|
||||
|
||||
self.assertEqual(rdExp._gene_varia['a'], 3)
|
||||
|
||||
def test_only_form_conds(self):
|
||||
form = "{a} + 2"
|
||||
cond = ["{a} in list(range(5))", "{a} % 2 == 1"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a'})
|
||||
self.assertEqual(rdExp._2replaced, {'a'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a'})
|
||||
|
||||
self.assertTrue(rdExp._gene_varia['a'] in list(range(5)))
|
||||
self.assertTrue(rdExp._gene_varia['a'] % 2 == 1)
|
||||
|
||||
def test_only_form_calc_cond(self):
|
||||
form = "{a*3} * {b}"
|
||||
cond = ["{a} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a', 'b'})
|
||||
self.assertEqual(rdExp._2replaced, {'a', 'b', 'a*3'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'a', 'b', 'a*3'})
|
||||
|
||||
self.assertEqual(rdExp._gene_varia['a'], 3)
|
||||
|
||||
|
||||
def test_only_form_calc_cond_calc(self):
|
||||
form = "{a*3} * {b}"
|
||||
cond = ["{a+b} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
self.assertEqual(rdExp._letters, {'a', 'b'})
|
||||
self.assertEqual(rdExp._2replaced, {'b', 'a*3', 'a+b'})
|
||||
|
||||
rdExp()
|
||||
self.assertEqual(set(rdExp._gene_varia.keys()), {'a', 'b'})
|
||||
self.assertEqual(set(rdExp._gene_2replaced.keys()), {'b', 'a*3', 'a+b'})
|
||||
|
||||
self.assertEqual((rdExp._gene_varia['a'] + rdExp._gene_varia['b']), 3)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
310
pymath/calculus/test/test_render.py
Normal file
310
pymath/calculus/test/test_render.py
Normal file
@@ -0,0 +1,310 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.render import tex, txt,p2i
|
||||
from pymath.calculus.fraction import Fraction
|
||||
from pymath.calculus.polynom import Polynom
|
||||
from pymath.calculus.operator import op
|
||||
from pymath.calculus.expression import Expression
|
||||
|
||||
from itertools import permutations
|
||||
|
||||
|
||||
def mass_poly_test(operation, rg= 5):
|
||||
""" @todo
|
||||
|
||||
:op: the operation
|
||||
:rg: number of potential values for coefs
|
||||
:returns: @todo
|
||||
"""
|
||||
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)]
|
||||
l_p = [Polynom(i) for i in coefs_p]
|
||||
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)]
|
||||
|
||||
|
||||
class TestTexRender(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.renders.tex"""
|
||||
|
||||
def test_type_render_int(self):
|
||||
self.assertEqual(tex([2]), "2")
|
||||
|
||||
def test_type_render_str(self):
|
||||
self.assertEqual(tex(["a"]), "a")
|
||||
|
||||
def test_type_render_fraction(self):
|
||||
self.assertEqual(tex([Fraction(1,2)]), "\\frac{ 1 }{ 2 }")
|
||||
|
||||
def test_type_render_poly(self):
|
||||
P = Polynom([1,2,3])
|
||||
self.assertEqual(tex([P]), "3 x^{ 2 } + 2 x + 1")
|
||||
|
||||
def test_add_interger(self):
|
||||
exps = [ [2, 3, op.add ],
|
||||
[2, -3, op.add ],
|
||||
[-2, 3, op.add ],
|
||||
]
|
||||
wanted_render = [ "2 + 3",
|
||||
"2 - 3",
|
||||
"-2 + 3",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mass_add(self):
|
||||
permu = mass_poly_test("+",5)
|
||||
from .mass_test import POLY_ADD_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(tex(v), POLY_ADD_VALID_RESULTS[i])
|
||||
|
||||
def test_mass_sub(self):
|
||||
permu = mass_poly_test("-",5)
|
||||
from .mass_test import POLY_SUB_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(tex(v), POLY_SUB_VALID_RESULTS[i])
|
||||
|
||||
def test_mass_mul(self):
|
||||
permu = mass_poly_test("*",5)
|
||||
from .mass_test import TEX_POLY_MUL_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(tex(v), TEX_POLY_MUL_VALID_RESULTS[i])
|
||||
|
||||
def test_add_letter(self):
|
||||
exps = [[2, "a", op.add ],
|
||||
["a", 3, op.add ],
|
||||
[-2, "a", op.add ],
|
||||
["a", -2, op.add ],
|
||||
]
|
||||
wanted_render = [ "2 + a",
|
||||
"a + 3",
|
||||
"-2 + a",
|
||||
"a - 2",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_add_fraction(self):
|
||||
exps = [[2, Fraction(1,2), op.add],
|
||||
[Fraction(1,2), 3, op.add],
|
||||
]
|
||||
wanted_render = [ "2 + \\frac{ 1 }{ 2 }",
|
||||
"\\frac{ 1 }{ 2 } + 3",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_add_poly(self):
|
||||
exps = [[2, Polynom([1,2,3]), op.mul],
|
||||
[Polynom([1,2,3]), 2, op.mul],
|
||||
[Polynom([1,2,3]), Polynom([4,5,6]), op.mul],
|
||||
]
|
||||
wanted_render = [ "2 ( 3 x^{ 2 } + 2 x + 1 )",
|
||||
"( 3 x^{ 2 } + 2 x + 1 ) \\times 2",
|
||||
"( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_interger(self):
|
||||
exps = [[2, 3, op.mul],
|
||||
[2, -3, op.mul],
|
||||
[-2, 3, op.mul],
|
||||
]
|
||||
wanted_render = [ "2 \\times 3",
|
||||
"2 \\times ( -3 )",
|
||||
"-2 \\times 3",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_letter(self):
|
||||
exps = [[2, "a", op.mul],
|
||||
["a", 3, op.mul],
|
||||
[-2, "a", op.mul],
|
||||
["a", -2, op.mul],
|
||||
]
|
||||
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_fraction(self):
|
||||
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"]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_poly(self):
|
||||
exps = [[2, Polynom([1,2,3]), op.mul],
|
||||
[Polynom([1,2,3]), 2, op.mul],
|
||||
[Polynom([1,2,3]), Polynom([4,5,6]), op.mul],
|
||||
]
|
||||
wanted_render = [ "2 ( 3 x^{ 2 } + 2 x + 1 )",
|
||||
"( 3 x^{ 2 } + 2 x + 1 ) \\times 2",
|
||||
"( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_parentheses_int(self):
|
||||
exps = [\
|
||||
[ 2, 3, op.add, 4, op.mul],\
|
||||
[ 2, 3, op.mul, 4, op.add],\
|
||||
[ 2, 3, 4, op.mul, op.add],\
|
||||
[ 2, 3, 4, op.add, op.add],\
|
||||
[ 2, 3, 4, op.add, op.sub],\
|
||||
]
|
||||
wanted_render = [\
|
||||
'( 2 + 3 ) \\times 4',\
|
||||
'2 \\times 3 + 4',\
|
||||
'2 + 3 \\times 4',\
|
||||
'2 + 3 + 4',\
|
||||
'2 - ( 3 + 4 )',\
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_parentheses_poly(self):
|
||||
P = Polynom([1,2,3])
|
||||
Q = Polynom([4,5,6])
|
||||
exps = [\
|
||||
[ 2, P, op.add],\
|
||||
[ 2, P, op.sub],\
|
||||
[ 2, P, P, op.mul, op.sub],\
|
||||
[ Q, P, op.add],\
|
||||
[ Q, P, op.sub],\
|
||||
]
|
||||
wanted_render = [\
|
||||
'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 )' ,\
|
||||
'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):
|
||||
rend = tex(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_slash(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
class TesttxtRender(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.renders.txt"""
|
||||
|
||||
def test_type_render_int(self):
|
||||
self.assertEqual(txt([2]), "2")
|
||||
|
||||
def test_type_render_str(self):
|
||||
self.assertEqual(txt(["a"]), "a")
|
||||
|
||||
def test_type_render_fraction(self):
|
||||
self.assertEqual(txt([Fraction(1,2)]), "1 / 2")
|
||||
|
||||
def test_mult_interger(self):
|
||||
exps = [ [2, 3, op.mul], \
|
||||
[2, -3, op.mul], \
|
||||
[-2, 3, op.mul]]
|
||||
wanted_render = [ "2 * 3", "2 * ( -3 )", "-2 * 3"]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = txt(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_letter(self):
|
||||
exps = [ [2, "a", op.mul], \
|
||||
["a", 3, op.mul], \
|
||||
[-2, "a", op.mul], \
|
||||
["a", -2, op.mul],
|
||||
["a", -2, op.mul, -2, op.mul],
|
||||
["a", -2, op.mul, "a", op.mul],
|
||||
]
|
||||
wanted_render = [ "2 a",
|
||||
"a * 3",
|
||||
"-2 a",
|
||||
"a * ( -2 )",
|
||||
"a * ( -2 ) * ( -2 )",
|
||||
"a * ( -2 ) a",
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = txt(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_mult_fraction(self):
|
||||
exps = [ [2, Fraction(1,2), op.mul], \
|
||||
[Fraction(1,2), 3, op.mul]]
|
||||
wanted_render = [ "2 * 1 / 2", "1 / 2 * 3"]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = txt(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_parentheses(self):
|
||||
mul = op.get_op("*", 2)
|
||||
add = op.get_op("+", 2)
|
||||
exps = [\
|
||||
[ 2, 3, add, 4, mul],\
|
||||
[ 2, 3, mul, 4, add],\
|
||||
[ 2, 3, 4, mul, add],\
|
||||
[ 2, 3, 4, add, add],\
|
||||
]
|
||||
wanted_render = [\
|
||||
'( 2 + 3 ) * 4',\
|
||||
'2 * 3 + 4',\
|
||||
'2 + 3 * 4',\
|
||||
'2 + 3 + 4',\
|
||||
]
|
||||
for (i,e) in enumerate(exps):
|
||||
rend = txt(e)
|
||||
self.assertEqual(rend, wanted_render[i])
|
||||
|
||||
def test_slash(self):
|
||||
pass
|
||||
|
||||
def test_mass_add(self):
|
||||
permu = mass_poly_test("+",5)
|
||||
from .mass_test import POLY_ADD_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(txt(v), POLY_ADD_VALID_RESULTS[i])
|
||||
|
||||
def test_mass_sub(self):
|
||||
permu = mass_poly_test("-",5)
|
||||
from .mass_test import POLY_SUB_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(txt(v), POLY_SUB_VALID_RESULTS[i])
|
||||
|
||||
def test_mass_mul(self):
|
||||
permu = mass_poly_test("*",5)
|
||||
from .mass_test import TXT_POLY_MUL_VALID_RESULTS
|
||||
for (i,v) in enumerate(permu):
|
||||
self.assertEqual(txt(v), TXT_POLY_MUL_VALID_RESULTS[i])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
86
pymath/calculus/test/test_str2tokens.py
Normal file
86
pymath/calculus/test/test_str2tokens.py
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from pymath.calculus.str2tokens import str2tokens, str2in_tokens, in2post_fix
|
||||
from pymath.calculus.polynom import Polynom
|
||||
|
||||
class TestStr2tokens(unittest.TestCase):
|
||||
"""Testing functions from pymath.calculus.str2tokens"""
|
||||
|
||||
def test_str2intokens(self):
|
||||
ans = str2in_tokens("2+3*4")
|
||||
self.assertEqual(ans, [2, "+", 3, "*", 4])
|
||||
|
||||
ans = str2in_tokens("2*3+4")
|
||||
self.assertEqual(ans, [2, "*", 3, "+", 4])
|
||||
|
||||
|
||||
def test_in2post_fix(self):
|
||||
in_tokens = str2in_tokens("2+3*4")
|
||||
ans = in2post_fix(in_tokens)
|
||||
self.assertEqual(ans, [2, 3, 4, "*", "+"])
|
||||
|
||||
in_tokens = str2in_tokens("2*3+4")
|
||||
ans = in2post_fix(in_tokens)
|
||||
self.assertEqual(ans, [2, 3,"*", 4, "+"])
|
||||
|
||||
|
||||
# TODO: Ajouter des tests pour les cas particuliers... |sam. nov. 8 17:39:18 CET 2014
|
||||
def test_str2in_tokens_big_num(self):
|
||||
exp = "123 + 3"
|
||||
tok = str2in_tokens(exp)
|
||||
self.assertEqual(tok, [123, "+", 3])
|
||||
|
||||
def test_str2in_tokens_beg_minus(self):
|
||||
exp = "-123 + 3"
|
||||
tok = str2in_tokens(exp)
|
||||
self.assertEqual(tok, [-123, "+", 3])
|
||||
|
||||
def test_str2in_tokens_time_lack(self):
|
||||
exp = "(-3)(2)"
|
||||
tok = str2in_tokens(exp)
|
||||
self.assertEqual(tok, ["(", -3, ")", "*","(", 2, ")" ])
|
||||
|
||||
def test_str2tokens_poly(self):
|
||||
exp = "2x + 4"
|
||||
post = str2in_tokens(exp)
|
||||
self.assertEqual(post, [2, "*", Polynom([0, 1]), '+', 4])
|
||||
|
||||
|
||||
def test_str2tokens_poly_double_x(self):
|
||||
exp = "xx + 4"
|
||||
post = str2in_tokens(exp)
|
||||
self.assertEqual(post, [Polynom([0, 1]), "*", Polynom([0, 1]), '+', 4])
|
||||
|
||||
def test_str2tokens_poly(self):
|
||||
exp = "x(2+1) + 4"
|
||||
post = str2in_tokens(exp)
|
||||
self.assertEqual(post, [Polynom([0, 1]), "*", "(", 2, "+", 1, ')', '+', 4])
|
||||
|
||||
def test_str2in_tokens_time_lack2(self):
|
||||
exp = "-3(2)"
|
||||
tok = str2in_tokens(exp)
|
||||
self.assertEqual(tok, [-3, "*","(", 2, ")" ])
|
||||
|
||||
def test_str2tokens_error_float(self):
|
||||
exp = "1 + 1.3"
|
||||
self.assertRaises(ValueError, str2tokens, exp)
|
||||
|
||||
def test_str2tokens_error(self):
|
||||
exp = "1 + $"
|
||||
self.assertRaises(ValueError, str2tokens, exp)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
|
Reference in New Issue
Block a user