def test_plus_minus_feature(self): """ Test case to check Plus and Minus feature """ argument_1 = Digit(4) argument_2 = Digit(2) minus = Minus() plus = Plus() minus_result = minus(argument_1, argument_2) self.assertEqual( plus(minus_result, argument_2).digit, argument_1.digit)
def test_divider_multiplier_feature(self): """ Test case to check Divider and Multiplier feature """ argument_1 = Digit(4) argument_2 = Digit(2) divider = Divider() multiplier = Multiplier() division_result = divider(argument_1, argument_2) self.assertEqual( multiplier(division_result, argument_2).digit, argument_1.digit)
def test_power(self): """ Test case to check Power operator """ argument_1 = Digit(3) argument_2 = Digit(2) operator = Power() self.assertEqual( operator(argument_1, argument_2).digit, argument_1.digit**argument_2.digit) self.assertEqual( operator(argument_2, argument_1).digit, argument_2.digit**argument_1.digit)
def test_multiple(self): """ Test case to check Multiplier operator """ argument_1 = Digit(3) argument_2 = Digit(2) operator = Multiplier() self.assertEqual( operator(argument_1, argument_2).digit, operator(argument_1, argument_2).digit) self.assertEqual( operator(argument_1, argument_2).digit, argument_1.digit * argument_2.digit)
def test_plus(self): """ Test case to check Plus operator """ argument_1 = Digit(1) argument_2 = Digit(2) operator = Plus() self.assertEqual( operator(argument_1, argument_2).digit, operator(argument_2, argument_1).digit) self.assertEqual( operator(argument_2, argument_1).digit, argument_1.digit + argument_2.digit)
def test_initialize_digit_from_normalized_string(self): """ Test case to check creating digit from normalized string """ digit = Digit('1e-4') self.assertEqual(digit.digit, 1 * 10**-4)
def test_initialize_digit_from_float(self): """ Test case to check creating digit from float """ digit = Digit(1.4) self.assertEqual(digit.digit, 1.4)
def test_initialize_digit_from_sting(self): """ Test case to check creating digit from string """ digit = Digit('1.4') self.assertEqual(digit.digit, 1.4)
def test_division(self): """ Test case to check Divider operator """ argument_1 = Digit(4) argument_2 = Digit(2) operator = Divider() self.assertNotEqual( operator(argument_1, argument_2).digit, operator(argument_2, argument_1).digit) self.assertEqual( operator(argument_1, argument_2).digit, argument_1.digit / argument_2.digit) self.assertEqual( operator(argument_2, argument_1).digit, argument_2.digit / argument_1.digit)
def test_minus(self): """ Test case to check Minus operator """ argument_1 = Digit(1) argument_2 = Digit(2) operator = Minus() self.assertNotEqual( operator(argument_1, argument_2).digit, operator(argument_2, argument_1).digit) self.assertEqual( operator(argument_1, argument_2).digit, argument_1.digit - argument_2.digit) self.assertEqual( operator(argument_2, argument_1).digit, argument_2.digit - argument_1.digit)
def test_digit(self, actual: str, expected_digit: float): """ Test case to check convert single number to RPN :param actual: number as string type :param expected_digit: the digit that expected after conversion in RPN """ actual_rpn = ReversePolishNotationConverter.convert(actual) expected_rpn = ReversePolishNotation() self.fill_rpn_with_data(expected_rpn, [Digit(expected_digit)]) self.assert_rpn_equal(actual_rpn, expected_rpn)
def read_digit(state) -> Digit: """ Method to read a digit from self._infix_notation :param state: expression in Reverse Polish Notation Format :return: Instance of Digit class """ digit = "" while not state.expression_in_infix_notation.empty() and \ ReversePolishNotationConverter.is_part_of_digit(state.expression_in_infix_notation.top()): digit += state.expression_in_infix_notation.get() return Digit(digit)
def read_digit(self, character: str) -> Digit: digit = character while not self._infix_notation.empty() and self.is_part_of_digit(self._infix_notation.top()): digit += self._infix_notation.get() return Digit(digit)