Add test with polynom in render

This commit is contained in:
Lafrite 2015-03-07 15:11:08 +01:00
parent 4296242afd
commit c832ae5742

View File

@ -6,6 +6,7 @@ import unittest
from pymath.render import tex, txt,p2i from pymath.render import tex, txt,p2i
from pymath.fraction import Fraction from pymath.fraction import Fraction
from pymath.polynom import Polynom
from pymath.operator import op from pymath.operator import op
@ -22,6 +23,10 @@ class TestTexRender(unittest.TestCase):
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):
P = Polynom([1,2,3])
self.assertEqual(tex([P]), "3 x^{ 2 } + 2 x + 1")
def test_mult_interger(self): def test_mult_interger(self):
exps = [ [2, 3, op.get_op("*", 2)], [2, -3, op.get_op("*", 2)], [-2, 3, op.get_op("*", 2)]] exps = [ [2, 3, op.get_op("*", 2)], [2, -3, op.get_op("*", 2)], [-2, 3, op.get_op("*", 2)]]
wanted_render = [ "2 \\times 3", "2 \\times ( -3 )", "-2 \\times 3"] wanted_render = [ "2 \\times 3", "2 \\times ( -3 )", "-2 \\times 3"]
@ -37,20 +42,31 @@ class TestTexRender(unittest.TestCase):
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.get_op("*", 2)], [Fraction(1,2), 3, op.get_op("*", 2)]] 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 = [ "2 \\times \\frac{ 1 }{ 2 }", "\\frac{ 1 }{ 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_parentheses(self): def test_mult_poly(self):
mul = op.get_op("*", 2) exps = [[2, Polynom([1,2,3]), op.mul],
add = op.get_op("+", 2) [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 ) ( 3 x^{ 2 } + 2 x + 1 )",
]
for (i,e) in enumerate(exps):
rend = tex(e)
self.assertEqual(rend, wanted_render[i])
def test_parentheses_int(self):
exps = [\ exps = [\
[ 2, 3, add, 4, mul],\ [ 2, 3, op.add, 4, op.mul],\
[ 2, 3, mul, 4, add],\ [ 2, 3, op.mul, 4, op.add],\
[ 2, 3, 4, mul, add],\ [ 2, 3, 4, op.mul, op.add],\
[ 2, 3, 4, add, add],\ [ 2, 3, 4, op.add, op.add],\
] ]
wanted_render = [\ wanted_render = [\
'( 2 + 3 ) \\times 4',\ '( 2 + 3 ) \\times 4',\
@ -62,6 +78,27 @@ class TestTexRender(unittest.TestCase):
rend = tex(e) rend = tex(e)
self.assertEqual(rend, wanted_render[i]) 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): def test_slash(self):
pass pass