def test_infix_to_postfix(self): #print(exp_eval.infix_to_postfix("1 + 2 + 3")) #print(exp_eval.infix_to_postfix("1 + 2 * 3 + 4")) #print(exp_eval.infix_to_postfix("2 ^ 3")) self.assertEqual(exp_eval.infix_to_postfix("1 + 2 * 3"), ("1 2 3 * +")) self.assertEqual(exp_eval.infix_to_postfix("1 + 2 ^ 3 + 4"), ("1 2 3 ^ + 4 +"))
def test_postfix_eval(self): #print("evaluation") self.assertEqual( exp_eval.postfix_eval(exp_eval.infix_to_postfix('1 + 2 + 3')), 6) self.assertEqual( exp_eval.postfix_eval(exp_eval.infix_to_postfix("1 + 2 * 3 + 4")), 11) self.assertEqual( exp_eval.postfix_eval(exp_eval.infix_to_postfix("1 + 2 ^ 3 / 3")), 3) with self.assertRaises(ValueError): exp_eval.postfix_eval(exp_eval.infix_to_postfix("1 + 2 * 3 / 0"))
def test_expr_6(self): """Test a valid infix expression to a known postfix output - example 6""" test_expr = '( 5.9 - 5.3 ) * 7.2 + 1.4 ^ 2' expected_expr = '5.9 5.3 - 7.2 * 1.4 2 ^ +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_expr_5(self): """Test a valid infix expression to a known postfix output - example 5""" test_expr = '6 + 9 + 4 ^ 2' expected_expr = '6 9 + 4 2 ^ +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_expr_4(self): """Test a valid infix expression to a known postfix output - example 4""" test_expr = '2 * 20 / 2 + ( 3 + 4 ) * 3 ^ 2 - 6 + 15' expected_expr = '2 20 * 2 / 3 4 + 3 2 ^ * + 6 - 15 +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_expr_3(self): """Test a valid infix expression to a known postfix output - example 3""" test_expr = '7 * 6 - 2 ( 6 / 2 ) + ( 4 - 1 )' expected_expr = '7 6 * 2 6 2 / - 4 1 - +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_expr_2(self): """Test a valid infix expression to a known postfix output - example 2""" test_expr = '6 + 4 + 2 ( 7 / 3 * 1 ) + ( 4 ^ 7 - 2 ) * 8' expected_expr = '6 4 + 2 7 3 / 1 * + 4 7 ^ 2 - 8 * +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_expression_given(self): """Test a valid infix expression to a known postfix output""" test_expr = '3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3' expected_expr = '3 4 2 * 1 5 - 2 3 ^ ^ / +' output_expr = ee.infix_to_postfix(test_expr) self.assertEqual(expected_expr, output_expr)
def test_exp_eval1(self): '''A method to test the infix_to_postfix, infix_valid, postfix_eval, and the postfix_valid function. ''' infix = '( ( 5 - 3 ) ^ 2 + ( 4 - 2 ) ^ 2 ) ^ ( 1 / 2 )' postfix = '5 3 - 2 ^ 4 2 - 2 ^ + 1 2 / ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertAlmostEqual(postfix_eval(postfix), 2.82842712475, 11)
def test_exp_eval23(self): infix = '2 * 3 ^ 2' postfix = '2 3 2 ^ *' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 18)
def test_exp_eval14(self): infix = '~ ( ~ 3 ) ^ 2 ^ 2 + 9' postfix = '3 ~ 2 2 ^ ^ ~ 9 +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), -72)
def test_exp_eval12(self): infix = '1' postfix = '1' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 1)
def test_exp_eval13(self): infix = '4 ^ 2 ^ 2' postfix = '4 2 2 ^ ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 256)
def test_exp_eval10(self): infix = '~ 3 ^ 2 + 9' postfix = '3 2 ^ ~ 9 +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 0)
def test_exp_eval8(self): infix = '~ 3 * 3 + 9' postfix = '3 ~ 3 * 9 +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 0)
def test_exp_eval6(self): infix = '( ( 2 * ( 3 + 4 ) ) / 5 )' postfix = '2 3 4 + * 5 /' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 2.8)
def test_exp_eval4(self): infix = '5 * 3 ^ ( 4 - 2 )' postfix = '5 3 4 2 - ^ *' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 45)
def test_exp_eval2(self): infix = '( ( 15 / ( 7 - ( 1 + 1 ) ) ) * 3 ) - ( 2 + ( 1 + 1 ) )' postfix = '15 7 1 1 + - / 3 * 2 1 1 + + -' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 5.0)
def test_exp_eval3(self): infix = '10 + 3 * 5 / ( 16 - 4 )' postfix = '10 3 5 * 16 4 - / +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 11.25)
def test_exp_eval15(self): infix = '~ 3 ^ 2' postfix = '3 2 ^ ~' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), -9)
def test_exp_eval5(self): infix = '( ( 1 * 2 ) + ( 3 / 4 ) )' postfix = '1 2 * 3 4 / +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 2.75)
def test_exp_eval17(self): infix = '( ~ 3 ) ^ 2 ^ 2' postfix = '3 ~ 2 2 ^ ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 81)
def test_exp_eval7(self): infix = '( 3 * ( 4 + 6 / 3 ) )' postfix = '3 4 6 3 / + *' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 18)
def test_exp_eval19(self): infix = '( ~ ~ 3 ) ^ 3' postfix = '3 ~ ~ 3 ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 27)
def test_exp_eval9(self): infix = '( ~ 3 ) ^ 2 + 9' postfix = '3 ~ 2 ^ 9 +' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 18)
def test_exp_eval20(self): infix = '~ ~ 3 ^ 3' postfix = '3 3 ^ ~ ~' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 27)
def test_exp_eval11(self): infix = '4 ^ ( ~ 1 ) * 4' postfix = '4 1 ~ ^ 4 *' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 1)
def test_exp_eval21(self): infix = '2 ^ 3' postfix = '2 3 ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 8)
def test_infix_to_postfix(self): self.assertEqual(infix_to_postfix("( ~ 3 ) ^ 2"), "3 ~ 2 ^") self.assertEqual(infix_to_postfix("~ 3 ^ 2"), "3 2 ^ ~") self.assertEqual(infix_to_postfix("4 ^ ( ~ 1 ) * 4"), "4 1 ~ ^ 4 *") self.assertEqual(infix_to_postfix("~ 3 * 3 + 9"), "3 ~ 3 * 9 +") self.assertEqual(infix_to_postfix("( 3 * ( 4 + 6 / 3 ) )"), "3 4 6 3 / + *") self.assertEqual( infix_to_postfix("( ( 5 - 3 ) ^ 2 + ( 4 - 2 ) ^ 2 ) ^ ( 1 / 2 )"), "5 3 - 2 ^ 4 2 - 2 ^ + 1 2 / ^") self.assertEqual( infix_to_postfix( "( ( 15 / ( 7 - ( 1 + 1 ) ) ) * 3 ) - ( 2 + ( 1 + 1 ) )"), "15 7 1 1 + - / 3 * 2 1 1 + + -") self.assertEqual(infix_to_postfix("10 + 3 * 5 / ( 16 - 4 )"), "10 3 5 * 16 4 - / +") self.assertEqual(infix_to_postfix("5 * 3 ^ ( 4 - 2 )"), "5 3 4 2 - ^ *") self.assertEqual(infix_to_postfix("( ( 1 * 2 ) + ( 3 / 4 ) )"), "1 2 * 3 4 / +") self.assertEqual(infix_to_postfix("( ( 2 * ( 3 + 4 ) ) / 5 )"), "2 3 4 + * 5 /") self.assertEqual(infix_to_postfix("~ 1"), "1 ~") self.assertEqual(infix_to_postfix("~ ~ 1"), "1 ~ ~") self.assertEqual(infix_to_postfix("1"), "1")
def test_exp_eval22(self): infix = '3 ^ 2' postfix = '3 2 ^' self.assertAlmostEqual(postfix_eval(postfix), postfix_eval(infix_to_postfix(infix))) self.assertEqual(postfix_eval(postfix), 9)