2014-02-25 15:39:03 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
2014-11-09 11:06:31 +00:00
|
|
|
from pymath.render import tex, txt,p2i
|
2014-02-25 15:39:03 +00:00
|
|
|
from pymath.fraction import Fraction
|
2015-03-07 14:11:08 +00:00
|
|
|
from pymath.polynom import Polynom
|
2014-12-02 13:33:45 +00:00
|
|
|
from pymath.operator import op
|
2014-02-25 15:39:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestTexRender(unittest.TestCase):
|
2014-05-28 18:42:19 +00:00
|
|
|
"""Testing functions from pymath.renders.tex"""
|
2014-02-25 15:39:03 +00:00
|
|
|
|
|
|
|
def test_type_render_int(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(tex([2]), "2")
|
2014-02-25 15:39:03 +00:00
|
|
|
|
|
|
|
def test_type_render_str(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(tex(["a"]), "a")
|
2014-02-25 15:39:03 +00:00
|
|
|
|
|
|
|
def test_type_render_fraction(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(tex([Fraction(1,2)]), "\\frac{ 1 }{ 2 }")
|
2014-02-25 15:39:03 +00:00
|
|
|
|
2015-03-07 14:11:08 +00:00
|
|
|
def test_type_render_poly(self):
|
|
|
|
P = Polynom([1,2,3])
|
|
|
|
self.assertEqual(tex([P]), "3 x^{ 2 } + 2 x + 1")
|
|
|
|
|
2014-02-25 15:39:03 +00:00
|
|
|
def test_mult_interger(self):
|
2014-12-02 13:33:45 +00:00
|
|
|
exps = [ [2, 3, op.get_op("*", 2)], [2, -3, op.get_op("*", 2)], [-2, 3, op.get_op("*", 2)]]
|
2014-02-25 15:39:03 +00:00
|
|
|
wanted_render = [ "2 \\times 3", "2 \\times ( -3 )", "-2 \\times 3"]
|
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = tex(e)
|
2014-02-25 15:39:03 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
|
|
|
def test_mult_letter(self):
|
2014-12-02 13:33:45 +00:00
|
|
|
exps = [ [2, "a", op.get_op("*", 2)], ["a", 3, op.get_op("*", 2)], [-2, "a", op.get_op("*", 2)], ["a", -2, op.get_op("*", 2)]]
|
2014-02-26 11:48:41 +00:00
|
|
|
wanted_render = [ "2 a", "a \\times 3", "-2 a", "a \\times ( -2 )"]
|
2014-02-25 15:39:03 +00:00
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = tex(e)
|
2014-02-25 15:39:03 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
|
|
|
def test_mult_fraction(self):
|
2015-03-07 14:11:08 +00:00
|
|
|
exps = [ [2, Fraction(1,2), op.mul], [Fraction(1,2), 3, op.mul]]
|
2014-02-26 11:48:41 +00:00
|
|
|
wanted_render = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 2 } \\times 3"]
|
2014-02-25 15:39:03 +00:00
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = tex(e)
|
2014-02-25 15:39:03 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
2015-03-07 14:11:08 +00:00
|
|
|
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",
|
2015-03-07 19:04:16 +00:00
|
|
|
"( 3 x^{ 2 } + 2 x + 1 ) ( 6 x^{ 2 } + 5 x + 4 )",
|
2015-03-07 14:11:08 +00:00
|
|
|
]
|
|
|
|
for (i,e) in enumerate(exps):
|
|
|
|
rend = tex(e)
|
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
|
|
|
def test_parentheses_int(self):
|
2014-11-09 11:06:31 +00:00
|
|
|
exps = [\
|
2015-03-07 14:11:08 +00:00
|
|
|
[ 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],\
|
2015-03-07 17:23:44 +00:00
|
|
|
[ 2, 3, 4, op.add, op.sub],\
|
2014-11-09 11:06:31 +00:00
|
|
|
]
|
|
|
|
wanted_render = [\
|
|
|
|
'( 2 + 3 ) \\times 4',\
|
|
|
|
'2 \\times 3 + 4',\
|
|
|
|
'2 + 3 \\times 4',\
|
|
|
|
'2 + 3 + 4',\
|
2015-03-07 17:23:44 +00:00
|
|
|
'2 - ( 3 + 4 )',\
|
2014-11-09 11:06:31 +00:00
|
|
|
]
|
|
|
|
for (i,e) in enumerate(exps):
|
|
|
|
rend = tex(e)
|
|
|
|
self.assertEqual(rend, wanted_render[i])
|
2014-02-25 15:39:03 +00:00
|
|
|
|
2015-03-07 14:11:08 +00:00
|
|
|
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 )' ,\
|
2015-03-07 17:23:44 +00:00
|
|
|
'6 x^{ 2 } + 5 x + 4 + 3 x^{ 2 } + 2 x + 1' ,\
|
|
|
|
'6 x^{ 2 } + 5 x + 4 - ( 3 x^{ 2 } + 2 x + 1 )' ,\
|
2015-03-07 14:11:08 +00:00
|
|
|
]
|
|
|
|
for (i,e) in enumerate(exps):
|
|
|
|
rend = tex(e)
|
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
2014-02-25 15:39:03 +00:00
|
|
|
def test_slash(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TesttxtRender(unittest.TestCase):
|
2014-05-28 18:42:19 +00:00
|
|
|
"""Testing functions from pymath.renders.txt"""
|
2014-02-25 15:39:03 +00:00
|
|
|
|
2014-02-25 15:58:51 +00:00
|
|
|
def test_type_render_int(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(txt([2]), "2")
|
2014-02-25 15:58:51 +00:00
|
|
|
|
|
|
|
def test_type_render_str(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(txt(["a"]), "a")
|
2014-02-25 15:58:51 +00:00
|
|
|
|
|
|
|
def test_type_render_fraction(self):
|
2014-05-28 18:42:19 +00:00
|
|
|
self.assertEqual(txt([Fraction(1,2)]), "1 / 2")
|
2014-02-25 15:58:51 +00:00
|
|
|
|
|
|
|
def test_mult_interger(self):
|
2014-12-02 13:33:45 +00:00
|
|
|
exps = [ [2, 3, op.get_op("*", 2)], \
|
|
|
|
[2, -3, op.get_op("*", 2)], \
|
|
|
|
[-2, 3, op.get_op("*", 2)]]
|
2014-02-25 15:58:51 +00:00
|
|
|
wanted_render = [ "2 * 3", "2 * ( -3 )", "-2 * 3"]
|
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = txt(e)
|
2014-02-25 15:58:51 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
|
|
|
def test_mult_letter(self):
|
2015-03-07 19:04:16 +00:00
|
|
|
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",
|
|
|
|
]
|
2014-02-25 15:58:51 +00:00
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = txt(e)
|
2014-02-25 15:58:51 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
|
|
|
def test_mult_fraction(self):
|
2014-12-02 13:33:45 +00:00
|
|
|
exps = [ [2, Fraction(1,2), op.get_op("*", 2)], \
|
|
|
|
[Fraction(1,2), 3, op.get_op("*", 2)]]
|
2014-02-25 15:58:51 +00:00
|
|
|
wanted_render = [ "2 * 1 / 2", "1 / 2 * 3"]
|
|
|
|
for (i,e) in enumerate(exps):
|
2014-05-28 18:42:19 +00:00
|
|
|
rend = txt(e)
|
2014-02-25 15:58:51 +00:00
|
|
|
self.assertEqual(rend, wanted_render[i])
|
|
|
|
|
2014-11-09 11:06:31 +00:00
|
|
|
def test_parentheses(self):
|
2014-12-02 13:33:45 +00:00
|
|
|
mul = op.get_op("*", 2)
|
|
|
|
add = op.get_op("+", 2)
|
2014-11-09 11:06:31 +00:00
|
|
|
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])
|
2014-02-25 15:58:51 +00:00
|
|
|
|
|
|
|
def test_slash(self):
|
|
|
|
pass
|
|
|
|
|
2014-02-25 15:39:03 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|
|
|
|
|