def test_division(self): e = Equation() e.create_division(-1, Element('1/2')) self.assertEqual(str(e), '1/2 : 1/2') e = Equation(Element('2/3')) e.create_division(-1, Element('1/2')) self.assertEqual(str(e), '1/3 : 1/2') e = Equation(Element('5/3')) e.create_division(-1, Element(2)) self.assertEqual(str(e), '3 1/3 : 2')
def test_multiplication(self): e = Equation() e.create_multiplication(-1, Element(2)) self.assertEqual(str(e), '1/2 * 2') e = Equation() e.create_multiplication(-1, Element(-2)) self.assertEqual(str(e), '(-1/2) * (-2)') e = Equation(1) e.create_multiplication(-1, Element(3, 2)) self.assertEqual(str(e), '2/3 * 1 1/2') e = Equation(1) e.create_multiplication(-1, Element('-3/2')) self.assertEqual(str(e), '(-2/3) * (-1 1/2)')
def __init__(self, out=1): if type(out) is Element: self.tab = [out] else: self.tab = [Element(abs(out))] if out < 0: self.tab += ['-']
def complicate(self, m, n, x, y): z = Element(x, y) if n == 0: self.create_sum(m, z) elif n == 1: self.create_sub(m, z) elif n == 2: self.create_negation(m) elif n == 3: self.create_multiplication(m, z) elif n == 4: self.create_division(m, z)
def test_complication(self): e = Equation(1) for m, n, x, y in [[0, 4, 1, 5], [1, 3, 1, 5], [1, 2, 1, 4]]: e.complicate(m, n, x, y) self.assertEqual(str(e), '1/5 : ((-1) * 1/5)') e = Equation(1) tab = [[0, 3, 1, 5], [1, 0, 1, 9], [0, 4, 1, 6], [1, 0, 1, 2], [0, 1, 1, 9], [5, 0, 1, 5], [3, 3, 1, 9], [1, 0, 1, 3]] for m, n, x, y in tab: e.complicate(m, n, x, y) e.tab = e.tab[0][0] e.tab[1] = Element(1) self.assertEqual(str(e), '17/18 - (-2/9 + 1/3) : 1') self.assertEqual(str(e), '17/18 - (-2/9 + 1/3)') e.tab = e.tab[0] print(e)
def test_operations(self): e0, e1 = Element(1), Element(2) self.assertEqual(e1.a, 2) self.assertEqual(e1.b, 1) e2 = e0 / e1 self.assertEqual(e2, Element(1, 2)) e0, e1 = Element(-1), Element(2) e2 = e0 / e1 self.assertEqual(e2, -Element(1, 2)) e0, e1 = Element('2/3'), Element('4/3') self.assertEqual(e0 / e1, Element('1/2')) e0, e2 = Element(1), Element('1/2') e1 = e0 / e2 self.assertEqual(e1, Element(2)) e0, e2 = Element(1), Element(2) self.assertEqual(e2, Element(2)) e0 / e2 self.assertEqual(e2, Element(2))
def test_str(self): x = Element(1, 2) s = str(x) self.assertEqual(s, '1/2')
def test_division(self): self.assertEqual(Element('2/3') / Element('5/7'), Element('14/15'))
def test_multiply(self): self.assertEqual(Element('2/3') * Element('5/7'), Element('10/21'))
def test_parse(self): self.assertEqual(Element('1/2'), Element(1, 2)) self.assertEqual(Element('-1/2'), -Element(1, 2)) self.assertEqual(Element('3'), Element(3, 1)) self.assertEqual(Element('-3'), -Element(3, 1)) self.assertEqual(Element('0.5'), Element(1, 2)) self.assertEqual(Element('1.5'), Element(3, 2))
def test_equal(self): self.assertEqual(Element(1, 2), Element(1, 2)) self.assertEqual(Element(0, 2), Element(0, 3)) self.assertEqual(Element(0, 2), -Element(0, 3)) self.assertEqual(Element(1, 2), Element(2, 4)) self.assertEqual(Element(-1, 2), -Element(1, 2))
def test_subtract(self): self.assertEqual(str(Element(1, 2) - Element(1, 3)), '1/6') self.assertEqual(str(Element(1, 3) - Element(1, 2)), '-1/6')
def test_negate(self): x, y = Element(1, 2, Sign.positive), Element(1, 2, Sign.negative) self.assertEqual(str(-x), '-1/2') self.assertEqual(str(-y), '1/2') self.assertEqual(str(-(-x)), '1/2')
def test_add(self): x, y = Element(1, 2), Element(1, 3) z = x + y self.assertEqual(z.a, 5) self.assertEqual(z.b, 6)