def test_mixed_number_literal(self): arr_mixed_numbers = [MixedNumber(x, x % 100, x) for x in range(1, 9999)] exp = ' '.join( ['{0}_{1}/{2}'.format(x.whole, x.fraction.numerator, x.fraction.denominator) for x in arr_mixed_numbers] ) tokens = tokenize_expr(exp) self.assertEqual(tokens, arr_mixed_numbers)
def test_complex_expression(self): exp = ' 12 +34/56 * /7_8/9 - ' tokens = tokenize_expr(exp) self.assertEqual(tokens, [ 12, InfixOperator('+'), Fraction(34, 56), InfixOperator('*'), InfixOperator('/'), MixedNumber(7, 8, 9), InfixOperator('-'), ])
def test_mix_div_x(self): self.assertEqual(MixedNumber(1, 2, 3), MixedNumber(2, 1, 2) / MixedNumber(1, 1, 2))
def test_simple_exp_out_mix(self): self.assertEqual(MixedNumber(1, 7, 8), expression_eval('1/2 * 3_3/4')) self.assertEqual(MixedNumber(3, 1, 2), expression_eval('2_3/8 + 9/8'))
def test_single_expressions_improper_frac(self): self.assertEqual(MixedNumber(2, 1, 3), expression_eval('7/3'))
def test_frac_mul_x_doub_neg(self): self.assertEqual(MixedNumber(4, 3, 4), MixedNumber(9, -1, 2) * Fraction(-1, 2))
def test_frac_mul_identity(self): self.assertEqual(MixedNumber(1, 2, 3), MixedNumber(1, 2, 3) * Fraction(1, 1))
def test_int_mul_commutive(self): self.assertEqual(MixedNumber(4, 1, 2) * 2, 2 * MixedNumber(4, 1, 2))
def test_frac_add_x_out_int(self): self.assertEqual(2, MixedNumber(1, 1, 2) + Fraction(1, 2))
def test_frac_add_x_doub_neg(self): self.assertEqual(MixedNumber(2, -1, 6), MixedNumber(1, -1, 2) + Fraction(-2, 3))
def test_frac_add_x_neg(self): self.assertEqual(MixedNumber(1, 1, 10), MixedNumber(1, 1, 2) + Fraction(-2, 5))
def test_frac_add_x(self): self.assertEqual(MixedNumber(2, 1, 6), MixedNumber(1, 1, 2) + Fraction(2, 3))
def test_frac_add_zero(self): self.assertEqual(MixedNumber(1, 1, 2), MixedNumber(1, 1, 2) + Fraction(0, 1))
def test_int_add_commutative(self): self.assertEqual(MixedNumber(1, 1, 2) + 5, 5 + MixedNumber(1, 1, 2))
def test_mix_div_x_doub_neg(self): self.assertEqual(MixedNumber(1, 2, 3), MixedNumber(2, -1, 2) / MixedNumber(1, -1, 2))
def test_int_add_zero(self): self.assertEqual(MixedNumber(1, 1, 2), MixedNumber(1, 1, 2) + 0)
def test_int_mul_x_doub_neg(self): self.assertEqual(MixedNumber(17, 1, 2), MixedNumber(2, -1, 2) * -7)
def test_mix_add_x(self): self.assertEqual(MixedNumber(4, 2, 3), MixedNumber(1, 1, 3) + MixedNumber(3, 1, 3))
def test_frac_mul_zero(self): self.assertEqual(0, MixedNumber(1, 2, 3) * Fraction(0, 1))
def test_mix_add_x_out_int(self): self.assertEqual(3, MixedNumber(1, 1, 2) + MixedNumber(1, 1, 2))
def test_frac_mul_x(self): self.assertEqual(MixedNumber(4, 3, 4), MixedNumber(9, 1, 2) * Fraction(1, 2))
def test_mix_add_x_neg(self): self.assertEqual(Fraction(-1, 6), MixedNumber(1, 1, 2) + MixedNumber(1, -2, 3))
def test_frac_mul_commutative(self): self.assertEqual( MixedNumber(1, 1, 2) * MixedNumber(1, 1, 3), MixedNumber(1, 1, 3) * MixedNumber(1, 1, 2))
def test_mix_add_x_doub_neg(self): self.assertEqual(MixedNumber(4, -2, 3), MixedNumber(1, -1, 3) + MixedNumber(3, -1, 3))
def test_int_mul_zero(self): self.assertEqual(0, MixedNumber(1, 2, 3) * 0)
def test_int_mul_identity(self): self.assertEqual(MixedNumber(1, 2, 3), MixedNumber(1, 2, 3) * 1)
def test_single_expr_mix(self): self.assertEqual(MixedNumber(2, 3, 4), expression_eval('2_3/4'))
def test_int_mul_x(self): self.assertEqual(MixedNumber(17, 1, 2), MixedNumber(2, 1, 2) * 7)
def test_complex_exp_out_mix(self): self.assertEqual(MixedNumber(5, 5, 6), expression_eval('1/3 + 1_1/2 * 2 + 1_2/3 / 2/3'))
def test_mix_div_out_identity(self): self.assertEqual(1, MixedNumber(1, 1, 2) / MixedNumber(1, 1, 2))