Change names and pass tests
This commit is contained in:
0
mapytex/calculus/test/__init__.py
Normal file
0
mapytex/calculus/test/__init__.py
Normal file
1524
mapytex/calculus/test/mass_test.py
Normal file
1524
mapytex/calculus/test/mass_test.py
Normal file
File diff suppressed because it is too large
Load Diff
32
mapytex/calculus/test/test_arithmetic.py
Normal file
32
mapytex/calculus/test/test_arithmetic.py
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
from mapytex.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
|
||||
86
mapytex/calculus/test/test_expression.py
Normal file
86
mapytex/calculus/test/test_expression.py
Normal file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
""" Testing Expression """
|
||||
|
||||
|
||||
from mapytex.calculus.expression import Expression
|
||||
from mapytex.calculus.operator import op
|
||||
|
||||
|
||||
def test_init_from_str():
|
||||
exp = Expression("2 + 3")
|
||||
assert exp.postfix_tokens == [2, 3, op.add]
|
||||
|
||||
|
||||
def test_init_from_exp():
|
||||
pass
|
||||
|
||||
|
||||
def test_init_list():
|
||||
exp = Expression([2, 3, "+"])
|
||||
assert exp.postfix_tokens == [2, 3, op.add]
|
||||
|
||||
|
||||
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, op.sub, 4, 1, op.add, op.add]
|
||||
|
||||
|
||||
def test_mul_exp():
|
||||
e = Expression("12- 4")
|
||||
f = Expression("4 + 1")
|
||||
g = e * f
|
||||
assert g.postfix_tokens == [12, 4, op.sub, 4, 1, op.add, op.mul]
|
||||
|
||||
|
||||
def test_neg_exp():
|
||||
e = Expression("12- 4")
|
||||
g = -e
|
||||
assert g.postfix_tokens == [12, 4, op.sub, op.sub1]
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
104
mapytex/calculus/test/test_fraction.py
Normal file
104
mapytex/calculus/test/test_fraction.py
Normal file
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus.fraction import Fraction
|
||||
|
||||
|
||||
class TestFraction(unittest.TestCase):
|
||||
"""Testing functions from mapytex.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
|
||||
74
mapytex/calculus/test/test_generic.py
Normal file
74
mapytex/calculus/test/test_generic.py
Normal file
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus import generic
|
||||
|
||||
|
||||
class TestGeneric(unittest.TestCase):
|
||||
"""Testing functions from mapytex.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
|
||||
190
mapytex/calculus/test/test_polynom.py
Normal file
190
mapytex/calculus/test/test_polynom.py
Normal file
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus.polynom import Polynom
|
||||
from mapytex.calculus.fraction import Fraction
|
||||
from mapytex.calculus.expression import Expression
|
||||
from mapytex.calculus.render import txt
|
||||
from mapytex.calculus.operator import op
|
||||
|
||||
|
||||
class TestPolynom(unittest.TestCase):
|
||||
"""Testing functions from mapytex.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_postfix(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
ans = [3, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 1, op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_monom(self):
|
||||
p = Polynom([0, 2])
|
||||
ans = [2, "x", op.mul]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_0_coef(self):
|
||||
p = Polynom([0, 2, 0, 4])
|
||||
ans = [4, 'x', 3, op.pw, op.mul, 2, 'x', op.mul, op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_1_coef(self):
|
||||
p = Polynom([0, 1, 1])
|
||||
ans = ["x", 2, op.pw, "x", op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_neg_coef(self):
|
||||
p = Polynom([-1, -2, -3])
|
||||
ans = [-3, 'x', 2, op.pw, op.mul, -2, 'x', op.mul, op.add, -1, op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_multi_coef(self):
|
||||
p = Polynom([1, [2, 3], 4])
|
||||
ans = [4, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 3, 'x', op.mul, op.add, 1, op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_postfix_exp_coef(self):
|
||||
p = Polynom([1, Expression([2, 3, "+"]), 4])
|
||||
ans = [4, 'x', 2, op.pw, op.mul, 2, 3, op.add, 'x', op.mul, op.add, 1, op.add]
|
||||
self.assertEqual(ans, p.postfix_tokens)
|
||||
|
||||
def test_str(self):
|
||||
p = Polynom([1, 2, 3])
|
||||
ans = '3 x^{ 2 } + 2 x + 1'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_monom(self):
|
||||
p = Polynom([0, 2])
|
||||
ans = '2 x'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_0_coef(self):
|
||||
p = Polynom([0, 2, 0, 4])
|
||||
ans = '4 x^{ 3 } + 2 x'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_1_coef(self):
|
||||
p = Polynom([0, 1, 1])
|
||||
ans = 'x^{ 2 } + x'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_neg_coef(self):
|
||||
p = Polynom([-1, -2, -3])
|
||||
ans = '-3 x^{ 2 } - 2 x - 1'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_multi_coef(self):
|
||||
p = Polynom([1, [2, 3], 4])
|
||||
ans = '4 x^{ 2 } + 2 x + 3 x + 1'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
def test_str_exp_coef(self):
|
||||
p = Polynom([1, Expression([2, 3, "+"]), 4])
|
||||
ans = '4 x^{ 2 } + ( 2 + 3 ) x + 1'
|
||||
self.assertEqual(ans, str(p))
|
||||
|
||||
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
|
||||
23
mapytex/calculus/test/test_polynomDeg2.py
Normal file
23
mapytex/calculus/test/test_polynomDeg2.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus.polynomDeg2 import Polynom_deg2
|
||||
|
||||
|
||||
class TestPolynomDeg2(unittest.TestCase):
|
||||
"""Testing functions from mapytex.calculus.polynomDeg2"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
108
mapytex/calculus/test/test_random_expression.py
Normal file
108
mapytex/calculus/test/test_random_expression.py
Normal file
@@ -0,0 +1,108 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
from mapytex.calculus.random_expression import RdExpression
|
||||
|
||||
|
||||
def test_only_form():
|
||||
form = "{a} + 2"
|
||||
rdExp = RdExpression(form)
|
||||
|
||||
assert rdExp._letters == {'a'}
|
||||
assert rdExp._2replaced == {'a'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()) == {'a'}
|
||||
assert set(rdExp._gene_2replaced.keys()) == {'a'}
|
||||
|
||||
|
||||
def test_form_with_underscores():
|
||||
form = "_ + 2*_"
|
||||
rdExp = RdExpression(form)
|
||||
|
||||
assert rdExp._letters == {'A', 'B'}
|
||||
assert rdExp._2replaced == {'A', 'B'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()) == {'A', 'B'}
|
||||
assert set(rdExp._gene_2replaced.keys()) == {'A', 'B'}
|
||||
|
||||
|
||||
def test_only_form_calc():
|
||||
form = "{a+b} + 2"
|
||||
rdExp = RdExpression(form)
|
||||
|
||||
assert rdExp._letters, {'a' == 'b'}
|
||||
assert rdExp._2replaced == {'a+b'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
|
||||
assert set(rdExp._gene_2replaced.keys()) == {'a+b'}
|
||||
|
||||
|
||||
def test_only_form_cond():
|
||||
form = "{a} + 2"
|
||||
cond = ["{a} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
assert rdExp._letters == {'a'}
|
||||
assert rdExp._2replaced == {'a'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()) == {'a'}
|
||||
assert set(rdExp._gene_2replaced.keys()) == {'a'}
|
||||
|
||||
assert rdExp._gene_varia['a'] == 3
|
||||
|
||||
|
||||
def test_only_form_conds():
|
||||
form = "{a} + 2"
|
||||
cond = ["{a} in list(range(5))", "{a} % 2 == 1"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
assert rdExp._letters == {'a'}
|
||||
assert rdExp._2replaced == {'a'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()) == {'a'}
|
||||
assert set(rdExp._gene_2replaced.keys()) == {'a'}
|
||||
|
||||
assert rdExp._gene_varia['a'] in list(range(5))
|
||||
assert rdExp._gene_varia['a'] % 2 == 1
|
||||
|
||||
|
||||
def test_only_form_calc_cond():
|
||||
form = "{a*3} * {b}"
|
||||
cond = ["{a} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
assert rdExp._letters, {'a' == 'b'}
|
||||
assert rdExp._2replaced, {'a', 'b' == 'a*3'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
|
||||
assert set(rdExp._gene_2replaced.keys()), {'a', 'b' == 'a*3'}
|
||||
|
||||
assert rdExp._gene_varia['a'] == 3
|
||||
|
||||
|
||||
def test_only_form_calc_cond_calc():
|
||||
form = "{a*3} * {b}"
|
||||
cond = ["{a+b} == 3"]
|
||||
rdExp = RdExpression(form, cond)
|
||||
|
||||
assert rdExp._letters, {'a' == 'b'}
|
||||
assert rdExp._2replaced, {'b', 'a*3' == 'a+b'}
|
||||
|
||||
rdExp()
|
||||
assert set(rdExp._gene_varia.keys()), {'a' == 'b'}
|
||||
assert set(rdExp._gene_2replaced.keys()), {'b', 'a*3' == 'a+b'}
|
||||
|
||||
assert (rdExp._gene_varia['a'] + rdExp._gene_varia['b']) == 3
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
# cursor: 16 del
|
||||
310
mapytex/calculus/test/test_render.py
Normal file
310
mapytex/calculus/test/test_render.py
Normal file
@@ -0,0 +1,310 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus.render import tex, txt
|
||||
from mapytex.calculus.fraction import Fraction
|
||||
from mapytex.calculus.polynom import Polynom
|
||||
from mapytex.calculus.operator import op
|
||||
from mapytex.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 mapytex.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_float(self):
|
||||
self.assertEqual(tex([12.345]), "12.345")
|
||||
|
||||
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 mapytex.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_float(self):
|
||||
self.assertEqual(txt([12.345]), "12.345")
|
||||
|
||||
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
|
||||
83
mapytex/calculus/test/test_str2tokens.py
Normal file
83
mapytex/calculus/test/test_str2tokens.py
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
import unittest
|
||||
|
||||
from mapytex.calculus.str2tokens import str2tokens, str2in_tokens, in2post_fix
|
||||
from mapytex.calculus.polynom import Polynom
|
||||
from mapytex.calculus.operator import op
|
||||
|
||||
|
||||
class TestStr2tokens(unittest.TestCase):
|
||||
"""Testing functions from mapytex.calculus.str2tokens"""
|
||||
|
||||
def test_str2in_tokens(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])
|
||||
|
||||
from decimal import Decimal
|
||||
ans = str2in_tokens("2.34")
|
||||
self.assertEqual(ans, [Decimal('2.34')])
|
||||
|
||||
def test_in2post_fix(self):
|
||||
in_tokens = str2in_tokens("2+3*4")
|
||||
ans = in2post_fix(in_tokens)
|
||||
self.assertEqual(ans, [2, 3, 4, op.mul, op.add])
|
||||
|
||||
in_tokens = str2in_tokens("2*3+4")
|
||||
ans = in2post_fix(in_tokens)
|
||||
self.assertEqual(ans, [2, 3, op.mul, 4, op.add])
|
||||
|
||||
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_par(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(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