def testMultiplyNumber(self): self.assertEqual(Fraction(2, 3), Fraction(1, 3) * 2) self.assertEqual(Fraction(-2, 3), Fraction(1, 3) * (-2)) self.assertEqual(Fraction(2, 3), 2 * Fraction(1, 3)) self.assertEqual(Fraction(-2, 3), -2 * Fraction(1, 3)) fraction = Fraction(1, 3) fraction *= 2 self.assertEqual(Fraction(2, 3), fraction)
def testStrVariable(self): self.assertEqual( str(VariableBuilder().name('x').value(Fraction(1, 2)).build()), '+(1/2)*x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(-1, 2)).build()), '-(1/2)*x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(1, 1)).build()), '+x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(-1, 1)).build()), '-x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(-4, 2)).build()), '-2*x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(4, 2)).build()), '+2*x') self.assertEqual( str(VariableBuilder().name('x').value(Fraction(0, 2)).build()), '+0*x')
def testReprVariable(self): self.assertEqual( repr(VariableBuilder().name('x').value(Fraction(1, 2)).build()), 'Variable(\'x\', Fraction(1, 2))')
def value(self, value_arg): if isinstance(value_arg, Fraction): self._value = Fraction(value_arg._num, value_arg._den) else: self._value = Fraction(value_arg, 1) return self
def __init__(self): self._value = Fraction(1, 1)
def fraction(self, num, den): self._value = Fraction(num, den) return self
def __init__(self): self._name = 'x' self._value = Fraction(1, 1)
def testAddFractions(self): self.assertEqual(Fraction(2, 3), Fraction(1, 3) + Fraction(1, 3)) self.assertEqual(Fraction(2, 3), Fraction(4, 3) - Fraction(2, 3)) fraction = Fraction(1, 3) fraction += Fraction(1, 3) self.assertEqual(Fraction(2, 3), fraction)
def testFractionRepr(self): self.assertEqual(repr(Fraction(1, 2)), "Fraction(1, 2)") self.assertEqual(repr(Fraction(-1, 2)), "Fraction(-1, 2)") self.assertEqual(repr(Fraction(3, 3)), "Fraction(1, 1)") self.assertEqual(repr(Fraction(6, 3)), "Fraction(2, 1)")
def testFractionStr(self): self.assertEqual(str(Fraction(1, 2)), "1/2") self.assertEqual(str(Fraction(-1, 2)), "-1/2") self.assertEqual(str(Fraction(3, 3)), "1") self.assertEqual(str(Fraction(6, 3)), "2")
def testDivideFraction(self): self.assertEqual(Fraction(4, 9), Fraction(1, 3) / Fraction(3, 4)) fraction = Fraction(1, 3) fraction /= Fraction(2, 3) self.assertEqual(Fraction(1, 2), fraction)
def testDivideNumber(self): self.assertEqual(Fraction(2, 3), Fraction(4, 3) / 2) self.assertEqual(Fraction(3, 1), 2 / Fraction(2, 3)) fraction = Fraction(4, 3) fraction /= 2 self.assertEqual(Fraction(2, 3), fraction)
def testMultiplyFractions(self): self.assertEqual(Fraction(2, 9), Fraction(1, 3) * Fraction(2, 3)) fraction = Fraction(1, 3) fraction *= Fraction(2, 3) self.assertEqual(Fraction(2, 9), fraction)
def testAddFractionNumber(self): self.assertEqual(Fraction(4, 3), Fraction(1, 3) + 1) self.assertEqual(Fraction(4, 3), 1 + Fraction(1, 3)) self.assertEqual(Fraction(2, 3), 1 - Fraction(1, 3)) self.assertEqual(Fraction(1, 3), Fraction(4, 3) - 1) fraction = Fraction(1, 3) fraction += 1 self.assertEqual(Fraction(4, 3), fraction) fraction -= 1 self.assertEqual(Fraction(1, 3), fraction)