Пример #1
0
 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 +"))
Пример #2
0
    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)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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)
Пример #15
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)
Пример #16
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)
Пример #17
0
 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)
Пример #18
0
 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)
Пример #19
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)
Пример #20
0
 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)
Пример #21
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)
Пример #22
0
 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)
Пример #23
0
 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)
Пример #24
0
 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)
Пример #25
0
 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)
Пример #26
0
 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)
Пример #27
0
 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)
Пример #28
0
 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)
Пример #29
0
 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")
Пример #30
0
 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)