def test_postfix_eval(self): self.assertEqual(postfix_eval("7 2 9 2 - 3 * * +"), 49) self.assertAlmostEqual(postfix_eval("7 2 ^ 7 * 1 3 / ^"), 7, 11) self.assertEqual(postfix_eval("2 ~ 2 ^"), 4) self.assertRaises(ZeroDivisionError, postfix_eval, "5 0 /") self.assertRaises(SyntaxError, postfix_eval, "5 +") self.assertRaises(SyntaxError, postfix_eval, "5 2 7 +") self.assertRaises(SyntaxError, postfix_eval, "5 b +")
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_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_expression(self): """Tests the evaluation of a correctly formatted postfix expression to a known value""" test_expr = '5 1 2 + 4 ^ + 3 -' expected_val = 83 output_val = ee.postfix_eval(test_expr) self.assertEqual(expected_val, output_val)
def test_expr_6(self): """Tests the evaluation of a correctly formatted postfix expression to a known value - example 6""" test_expr = '5.9 5.3 - 7.2 * 1.4 2 ^ +' expected_val = 6.28 output_val = ee.postfix_eval(test_expr) self.assertAlmostEqual(expected_val, output_val, 5)
def test_expr_5(self): """Tests the evaluation of a correctly formatted postfix expression to a known value - example 5""" test_expr = '6 9 + 4 2 ^ +' expected_val = 31 output_val = ee.postfix_eval(test_expr) self.assertAlmostEqual(expected_val, output_val, 5)
def test_expr_4(self): """Tests the evaluation of a correctly formatted postfix expression to a known value - example 4""" test_expr = '2 20 * 2 / 3 4 + 3 2 ^ * + 6 - 15 +' expected_val = 92 output_val = ee.postfix_eval(test_expr) self.assertAlmostEqual(expected_val, output_val, 2)
def test_expr_3(self): """Tests the evaluation of a correctly formatted postfix expression to a known value - example 3""" test_expr = '7 6 * 2 6 2 / - 4 1 - +' expected_val = 2 output_val = ee.postfix_eval(test_expr) self.assertAlmostEqual(expected_val, output_val, 6)
def test_expr_2(self): """Tests the evaluation of a correctly formatted postfix expression to a known value - example 2""" test_expr = '6 4 + 2 7 3 / 1 * + 4 7 ^ 2 - 8 * +' expected_val = 131060.333 output_val = ee.postfix_eval(test_expr) self.assertAlmostEqual(expected_val, output_val, 2)
def test_postfix_eval(self): self.assertEqual(postfix_eval("3 ~ 2 ^"), 9) self.assertEqual(postfix_eval("3 2 ^ ~ 9 +"), 0) self.assertEqual(postfix_eval("4 1 ~ ^ 4 *"), 1.0) self.assertRaises(ZeroDivisionError, postfix_eval, "5 0 /") self.assertEqual(postfix_eval("1 2 * 3 4 / +"), 2.75) self.assertEqual(postfix_eval("15 7 1 1 + - / 3 * 2 1 1 + + -"), 5) self.assertEqual(postfix_eval("5 3 - 2 ^ 4 2 - 2 ^ + 1 2 / ^"), 2.8284271247461903) self.assertEqual(postfix_eval("3 4 6 3 / + *"), 18) self.assertRaises(SyntaxError, postfix_eval, "5 + +")
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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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)
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)