Пример #1
0
 def arith(self, op):
     b = self.stack.pop()
     a = self.stack.pop() if (op != ArithOp.UNM
                              and op != ArithOp.BNOT) else b
     result = Arithmetic.arith(a, op, b)
     assert (result is not None)
     self.stack.push(result)
Пример #2
0
    def arith(self, op):
        b = self.stack.pop()
        a = self.stack.pop() if (op != ArithOp.UNM and op != ArithOp.BNOT) else b
        result = Arithmetic.arith(a, op, b)
        if result is None:
            name = Arithmetic.operators[op].metamethod
            metamethod = self.get_metamethod(a, b, name)
            result = self.call_metamethod(a, metamethod, b) if metamethod else None

        if result is None:
            raise Exception('arith error')
        self.stack.push(result)
Пример #3
0
    def arith(self, op):
        b = self.stack.pop()
        a = self.stack.pop() if (op != ArithOp.UNM
                                 and op != ArithOp.BNOT) else b
        result = Arithmetic.arith(a, op, b)
        if result is None:
            name = Arithmetic.operators[op].metamethod
            metamethod = self.get_metamethod(a, b, name)
            result = self.call_metamethod(a, metamethod,
                                          b) if metamethod else None

        assert (result is not None)
        self.stack.push(result)
Пример #4
0
class Arithmetic_Test(unittest.TestCase):

    # Creating class object
    easy_calc = Arithmetic()

    # Creating test method for the find_sqrt function
    def test_find_sqrt(self):
        self.assertEqual(self.easy_calc.find_sqrt(25), 5)

    # Creating test method for the find_sqrt function
    def test_find_ceil(self):
        self.assertEqual(self.easy_calc.find_ceil(1.4), 2)

# # Testing method
# # calc = Artithmetic()
# # print(calc.find_sqrt(25))
# print(calc.find_ceil(1.451231))
Пример #5
0
    def test_solve_L(self):
        """Are L ordered problems solved correctly?"""
        self.assertFalse(Arithmetic('1 2 3', '6 L').solve() == self.L_NP)
        self.assertFalse(Arithmetic('1 2 3', '5 L').solve() == self.L_NP)
        self.assertFalse(Arithmetic('1 2 3', '9 L').solve() == self.L_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '36 L').solve() == self.L_NP)
        self.assertFalse(
            Arithmetic('1 2 3 4 5 6', '71 L').solve() == self.L_NP)

        # Case from submission.
        self.assertTrue(
            Arithmetic('1 2 3 4 5 1 2 3 4 5', '100 L').solve() == self.L_NP)
        # Failed case from submission.
        self.assertFalse(
            Arithmetic(
                '1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5',
                '2134 L').solve() == self.L_NP)
        # Reaallllyy long case.
        self.assertFalse(
            Arithmetic(
                '3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3',
                '2016 L').solve() == self.L_NP)
Пример #6
0
 def test_ouput_is_valid(self):
     """Is the program output valid?"""
     self.assertEqual(Arithmetic('1 2 3', '7 N').solve(), 'N 1 + 2 * 3')
     self.assertEqual(Arithmetic('1 2 3', '9 L').solve(), 'L 1 + 2 * 3')
     self.assertEqual(Arithmetic('1 2 3', '100 N').solve(), self.N_NP)
Пример #7
0
    def test_solve_N(self):
        """Are N ordered problems solved correctly?"""
        self.assertFalse(Arithmetic('1 2 3', '6 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3', '5 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3', '7 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '10 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '9 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '11 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '15 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '14 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4', '24 N').solve() == self.N_NP)
        self.assertFalse(Arithmetic('1 2 3 4 5', '30 N').solve() == self.N_NP)
        self.assertFalse(
            Arithmetic('1 2 3 4 5 6', '33 N').solve() == self.N_NP)
        self.assertFalse(
            Arithmetic('1 2 3 4 5 6', '127 N').solve() == self.N_NP)

        self.assertFalse(Arithmetic('2 11 8 15', '45 N').solve() == self.N_NP)

        # Case from submission.
        self.assertTrue(Arithmetic('1 2 3 4 5', '10 N').solve() == self.N_NP)
Пример #8
0
 def test_parse_order(self):
     """Is the order parsed correctly?"""
     self.assertEqual(Arithmetic('1 2 3', '7 N').order, 'N')
     self.assertEqual(Arithmetic('1 2 3', '9 L').order, 'L')
     self.assertEqual(Arithmetic('1 2 3', '9 L').order, 'L')
Пример #9
0
 def test_parse_target(self):
     """Is the target number parsed correctly?"""
     self.assertEqual(Arithmetic('1 2 3', '7 N').target, 7)
     self.assertEqual(Arithmetic('1 2 3', '7    N').target, 7)
Пример #10
0
 def test_parse_input(self):
     """Is input parsed correctly?"""
     self.assertEqual(Arithmetic('1 2 3', '7 N').nums, [1, 2, 3])
     self.assertEqual(Arithmetic('  2  6   1 ', '8 N').nums, [2, 6, 1])