Exemplo n.º 1
0
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter, self.multiplier, self.divider)
class TestCalculator(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()

    def test_add_given_two_values_should_return_the_correct_value(self):
        result = self.calculator.add(10, 10)

        self.assertEqual(first=result,
                         second=20,
                         msg='Invalid sum operation performed.')

    def test_subtract_given_two_values_should_return_the_correct_value(self):
        result = self.calculator.subtract(10, 10)

        self.assertEqual(first=result,
                         second=0,
                         msg='Invalid subtract operation performed.')

    def test_multiply_given_two_values_should_return_the_correct_value(self):
        result = self.calculator.multiply(10, 10)

        self.assertEqual(first=result,
                         second=100,
                         msg='Invalid multiply operation performed.')

    def test_divide_given_two_values_should_return_the_correct_value(self):
        result = self.calculator.divide(10, 10)

        self.assertEqual(first=result,
                         second=1,
                         msg='Invalid divide operation performed.')
Exemplo n.º 3
0
    def calculate(self, quantities=['energy'], keep_files=False):
        """ Run calculation """
        Calculator.write_input(self, self.qmconf)
        
        # set enviroment.
        os.environ['GAUSS_SCRDIR'] = os.path.abspath(self.calc_dir)
        #os.environ['GAUSS_EXEDIR'] = self.program_path # needed for g09.
        
        # Run calculation.
        self.write_input() # write input

        command = self.program_path + "/g16 " + self.label + '.com'
        
        os.chdir(self.calc_dir) # move to working dir
        output = os.popen(command).read() # run calculation
        os.chdir('..') # get out of working dir
        
        # extract results from quantities.  
        results = Calculator.read_results(self, self.qmconf, output, quantities)

        # write output if keep_files = True
        if keep_files:
            with open(self.label + ".out", 'w') as f:
                f.write(output)

        self.clean(keep_files)

        return results
Exemplo n.º 4
0
 def test_tokenize_unary_minus_start_of_expression(self):
     calculator = Calculator()
     expected = [
         OperatorToken('u-'),
         ValueToken(Decimal(3))
     ]
     self.assertListEqual(expected, calculator.tokenize("- 3"))
Exemplo n.º 5
0
 def test_tokenize_simple_expression_packed(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2)),
         OperatorToken('*'),
         ValueToken(Decimal(3))
     ]
     self.assertListEqual(expected, calculator.tokenize("2*3"))
Exemplo n.º 6
0
 def test_tokenize_value_parentheses(self):
     calculator = Calculator()
     expected = [
         LeftParenthesesToken(),
         ValueToken(Decimal(3)),
         RightParenthesesToken()
     ]
     self.assertListEqual(expected, calculator.tokenize("(3)"))
Exemplo n.º 7
0
 def test_rpn_division(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(6)),
         ValueToken(Decimal(3)),
         OperatorToken('/')
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("6 / 3")))
Exemplo n.º 8
0
 def test_rpn_operator_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2)),
         ValueToken(Decimal(4)),
         OperatorToken('+'),
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("(2 + 4)")))
Exemplo n.º 9
0
 def test_evaluate_operators(self):
     calculator = Calculator()
     self.assertEqual(Decimal(3), calculator.evaluate("+3"))
     self.assertEqual(Decimal(-3), calculator.evaluate("-3"))
     self.assertEqual(Decimal(6), calculator.evaluate("2 * 3"))
     self.assertEqual(Decimal(2), calculator.evaluate("6 / 3"))
     self.assertEqual(Decimal(5), calculator.evaluate("2 + 3"))
     self.assertEqual(Decimal(3), calculator.evaluate("7 - 4"))
Exemplo n.º 10
0
def evaluate():
    data = request.get_json()
    expression = data['expression'];

    calculator = Calculator(converter)
    result = calculator.evaluate(expression)

    return jsonify({'result': result})
Exemplo n.º 11
0
 def setUp(self):
     """Set up for calc tests so don't have to rewrite"""
     self.adder = Adder()
     self.subtracter = Subtracter()
     self.multiplier = Multiplier()
     self.divider = Divider()
     # Line is too long, but whatever.
     self.calculator = Calculator(self.adder, self.subtracter, self.multiplier, self.divider)
Exemplo n.º 12
0
    def setUp(self):
        """setup for calculator tests"""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
    def setUp(self):
        """Test case that setups the other test cases from range -10 to 10"""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 14
0
    def setUp(self):
        """Initializes the calculator"""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 15
0
    def setUp(self):
        """Provide a method to run each time before any test method is run."""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 16
0
    def setUp(self):
        """Initialize calculator with new operator objects."""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 17
0
 def test_tokenize_unary_plus_after_operator(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(3)),
         OperatorToken('*'),
         OperatorToken('u+'),
         ValueToken(Decimal(2))
     ]
     self.assertListEqual(expected, calculator.tokenize("3 * +2"))
Exemplo n.º 18
0
    def setUp(self):
        """Method doc string."""
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 19
0
 def test_tokenize_expression_no_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2)),
         OperatorToken('*'),
         ValueToken(Decimal(3)),
         OperatorToken('+'),
         ValueToken(Decimal(4)),
     ]
     self.assertListEqual(expected, calculator.tokenize("2 * 3 + 4"))
Exemplo n.º 20
0
    def setUp(self):
        """ 1 time setup with modules """

        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 21
0
 def test_rpn_operator_precedence_with_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(5)),
         ValueToken(Decimal(3)),
         OperatorToken('+'),
         ValueToken(Decimal(4)),
         OperatorToken('*')
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("(5 + 3) * 4")))
Exemplo n.º 22
0
 def test_rpn_multiple_parentheses_decimal(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal("3.5")),
         ValueToken(Decimal("2.7")),
         ValueToken(Decimal("4.8")),
         OperatorToken('+'),
         OperatorToken('*')
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("(3.5 * ((2.7) + (4.8)))")))
Exemplo n.º 23
0
 def test_rpn_multiple_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(3)),
         ValueToken(Decimal(2)),
         ValueToken(Decimal(4)),
         OperatorToken('+'),
         OperatorToken('*')
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("(3 * ((2) + (4)))")))
Exemplo n.º 24
0
 def test_tokenize_unary_plus_start_of_expression_inside_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2)),
         OperatorToken('*'),
         LeftParenthesesToken(),
         OperatorToken('u+'),
         ValueToken(Decimal(3)),
         RightParenthesesToken()
     ]
     self.assertListEqual(expected, calculator.tokenize("2 * (+3)"))
Exemplo n.º 25
0
    def setUp(self):
        """
        This method initializes variables
        """
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)
Exemplo n.º 26
0
    def setUp(self):
        """
        Fill the calculator object with proper adder, subtracter,
        multiplier, and divider classes.
        """
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(
            self.adder, self.subtracter, self.multiplier, self.divider)
Exemplo n.º 27
0
 def test_tokenize_expression(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2)),
         OperatorToken('*'),
         LeftParenthesesToken(),
         ValueToken(Decimal(3)),
         OperatorToken('+'),
         ValueToken(Decimal(4)),
         RightParenthesesToken()
     ]
     self.assertListEqual(expected, calculator.tokenize("2 * (3 + 4)"))
Exemplo n.º 28
0
 def test_calculate(self):
     calculator = Calculator(operation="add", number1=10, number2=7)
     calculator.calculate()
     self.assertEqual(calculator.result, 17)
     calculator = Calculator(operation="subtract", number1=8, number2=7)
     calculator.calculate()
     self.assertEqual(calculator.result, 1)
class TestCases(unittest.TestCase):
	def setUp(self):
		self.calculator = Calculator()

	def test_instantiation(self):
		self.assertIsInstance(self.calculator, Calculator)

	def test_addition(self):
		self.assertEqual(5, self.calculator.addition(2, 3))

	def test_addition_list(self):
		self.assertEqual(11, self.calculator.addition([2, 3, 6]))

	def test_subtraction(self):
		self.assertEqual(3, self.calculator.subtraction(6, 3))

	def test_multiplication(self):
		self.assertEqual(12, self.calculator.multiplication(3, 4))

	def test_division(self):
		self.assertEqual(3.5, self.calculator.division(7, 2))

	def test_exponentiation(self):
		self.assertEqual(27, self.calculator.exponentiation(3, 3))

	def test_nthroot(self):
		self.assertEqual(4, self.calculator.nthroot(16, 2))

	def test_logarithm(self):
		self.assertEqual(3, self.calculator.logarithm(8, 2))
Exemplo n.º 30
0
    def setUp(self): #Run before test method is run
        """
        Setup
        """
        
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, 
                                     self.subtracter, 
                                     self.multiplier, 
                                     self.divider)
Exemplo n.º 31
0
def main():
    try:
        args = _argparser.parse()

        calculator = Calculator(operation=args.operation,
                                number1=args.number1,
                                number2=args.number2)
        calculator.calculate()

        print("Result: ", calculator.result)

    except Exception as e:
        print(e)
    else:
        print('Success!')
    finally:
        print('Cleaning up.')
def main():

    adder = Adder()
    subtracter = Subtracter()
    multiplier = Multiplier()
    divider = Divider()

    calculator = Calculator(adder, subtracter, multiplier, divider)
    return calculator
Exemplo n.º 33
0
 def test_evaluate(self):
     calculator = Calculator()
     self.assertEqual(Decimal(4), calculator.evaluate("4"))
     self.assertEqual(Decimal(21), calculator.evaluate("7 * 3"))
     self.assertEqual(Decimal(11), calculator.evaluate("2 * 4 + 3"))
     self.assertEqual(Decimal(45), calculator.evaluate("(3 * (2 + 5)) + 6 * (4)"))
     self.assertEqual(Decimal("25.92"), calculator.evaluate("2.7 * (3.2 + 6.4)"))
     self.assertEqual(Decimal(1), calculator.evaluate("-2 * -4 + -7"))
Exemplo n.º 34
0
def main():
    """Sets up the UI for the calculator
    """
    window = Tk()
    window.title("Laskin")
    calculator = Calculator()
    result_var = DoubleVar()

    ui = UI(window, calculator, result_var, note_service)
    ui.start()
    window.mainloop()
Exemplo n.º 35
0
    def test_module2(self):
        calculator = Calculator(Adder(), Subtracter(), Multiplier(), Divider())

        calculator.enter_number(5)

        with self.assertRaises(InsufficientOperands):
            calculator.multiply()
Exemplo n.º 36
0
    def calculate(self, quantities=['energy'], keep_files=False):
        """Run xTB calculation and return dict of results"""
        Calculator.write_input(self, self.qmconf)

        self.write_input() # write input file

        command = self.input_cmd() # command to run xTB
        
        os.chdir(self.calc_dir) # move into working dir.
        output = os.popen(command).read() # run calculation.
        os.chdir('..') # get out of working dir.
        
        # extract results from quantities.
        results = Calculator.read_results(self, self.qmconf, output, quantities)

        if keep_files:
            with open(self.label + ".out", 'w') as f:
                f.write(output)

        self.clean(keep_files)

        return results
Exemplo n.º 37
0
    def calculate(self, quantities=['energy'], keep_files=False):
        """ Run calculation """
        Calculator.write_input(self, self.qmconf)

        self.write_input()  # write input

        command = self.program_path + " " + self.label + '.inp' + ' 2>' + self.scr_dir + '/' + self.label + '.log'

        os.chdir(self.calc_dir)  # create working dir
        output = os.popen(command).read()  # run calculation
        os.chdir('..')  # get out of working dir

        # extract results from quantities.
        results = Calculator.read_results(self, self.qmconf, output,
                                          quantities)

        if keep_files:
            with open(self.label + ".out", 'w') as f:
                f.write(output)

        self.clean(keep_files)

        return results
class MyTestCase(unittest.TestCase):
    def setUp(self) -> None:
        self.calculator = Calculator()

    # Unit test 1
    def test_instantiate_calculator(self):
        self.assertIsInstance(self.calculator, Calculator)

    # Unit test 2
    def test_subtraction(self):
        test_data = CsvReader( BASE_DIR + "/data/UnitTestSubtraction.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.subtract(row['Value 1'], row['Value 2']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 3
    def test_addition(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestAddition.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.add(row['Value 1'], row['Value 2']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 4
    def test_multiplication(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestMultiplication.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.multiply(row['Value 1'], row['Value 2']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 5
    def test_division(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestDivision.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(round(self.calculator.divide(row['Value 1'], row['Value 2'])), round(result))
            self.assertEqual(round(self.calculator.result), round(result))

    # Unit test 6
    def test_square(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestSquare.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(self.calculator.square(row['Value 1']), result)
            self.assertEqual(self.calculator.result, result)

    # Unit test 7
    def test_square_root(self):
        test_data = CsvReader(BASE_DIR + "/data/UnitTestSquareRoot.csv").data
        for row in test_data:
            result = float(row['Result'])
            self.assertEqual(round(self.calculator.squareroot(row['Value 1'])), round(result))
            self.assertEqual(round(self.calculator.result), round(result))
Exemplo n.º 39
0
 def result():
     first_term = request.form.get("first_term", type=int)
     second_term = request.form.get("second_term", type=int)
     operation = request.form.get("operation")
     c = Calculator(first_term, second_term)
     if operation == 'Add':
         res = c.add()
     elif operation == 'Subtract':
         res = c.subtract()
     elif operation == 'Multiply':
         res = c.multiply()
     elif operation == 'Divide':
         res = c.divide()
     else:
         res = 'INVALID CHOICE'
     return render_template('result.html', result=res)
Exemplo n.º 40
0
class CalculatorTests(TestCase):
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)

    def test_insufficient_operands(self):
        self.calculator.enter_number(0)

        with self.assertRaises(InsufficientOperands):
            self.calculator.add()

    def test_adder_call(self):
        self.adder.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()

        self.adder.calc.assert_called_with(1, 2)

    def test_subtracter_call(self):
        self.subtracter.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.subtract()

        self.subtracter.calc.assert_called_with(1, 2)

    def test_multiplier_call(self):
        self.multiplier.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.multiply()

        self.multiplier.calc.assert_called_with(1, 2)

    def test_divider_call(self):
        self.divider.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.divide()

        self.divider.calc.assert_called_with(1, 2)

    def test_combine_call_1(self):
        self.adder.calc = MagicMock(return_value=33)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()
        self.calculator.enter_number(1)
        self.calculator.add()

        self.adder.calc.assert_called_with(33, 1)

    def test_combine_call_2(self):
        self.adder.calc = MagicMock(return_value=56)
        self.subtracter.calc = MagicMock(return_value=0)
        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()
        self.calculator.enter_number(3)
        self.calculator.subtract()

        self.subtracter.calc.assert_called_with(56, 3)
Exemplo n.º 41
0
class CalculatorTests(TestCase):
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)

    def test_insufficient_operands(self):
        self.calculator.enter_number(0)

        with self.assertRaises(InsufficientOperands):
            self.calculator.add()

    def test_adder_call(self):
        self.adder.calc = MagicMock(return_value=(2 + 1))

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        result = self.calculator.add()

        self.adder.calc.assert_called_with(2, 1)
        self.assertEqual(result, (2 + 1))

    def test_subtracter_call(self):
        self.subtracter.calc = MagicMock(return_value=(2 - 1))

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        result = self.calculator.subtract()

        self.subtracter.calc.assert_called_with(2, 1)
        self.assertEqual(result, (2 - 1))

    def test_multiplier_call(self):
        self.multiplier.calc = MagicMock(return_value=(2 * 1))

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        result = self.calculator.multiply()

        self.multiplier.calc.assert_called_with(2, 1)
        self.assertEqual(result, (2 * 1))

    def test_divider_call(self):
        self.divider.calc = MagicMock(return_value=(2 / 1))

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        result = self.calculator.divide()

        self.divider.calc.assert_called_with(2, 1)
        self.assertEqual(result, (2 / 1))
Exemplo n.º 42
0
class CalculatorTests(TestCase):
    """This runs all of the tests"""
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)

    def test_insufficient_operands(self):
        self.calculator.enter_number(0)

        with self.assertRaises(InsufficientOperands):
            self.calculator.add()

    def test_adder_call(self):
        self.adder.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()

        self.adder.calc.assert_called_with(1, 2)

    def test_subtracter_call(self):
        self.subtracter.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.subtract()

        self.subtracter.calc.assert_called_with(1, 2)

    def test_multiplier_call(self):
        self.multiplier.calc = MagicMock(return_value=0)

        self.calculator.enter_number(2)
        self.calculator.enter_number(4)
        self.calculator.multiply()

        self.multiplier.calc.assert_called_with(2, 4)

    def test_divider_call(self):
        self.divider.calc = MagicMock(return_value=0)

        self.calculator.enter_number(2)
        self.calculator.enter_number(4)
        self.calculator.divide()

        self.divider.calc.assert_called_with(2, 4)
Exemplo n.º 43
0
    def __init__(self, qmconf=None, label='xtb', **kwargs):

        Calculator.__init__(self, qmconf, label, **kwargs)
Exemplo n.º 44
0
class CalculatorTests(TestCase):
    def setUp(self):
        self.adder = Adder()
        self.subtracter = Subtracter()
        self.multiplier = Multiplier()
        self.divider = Divider()

        self.calculator = Calculator(self.adder, self.subtracter,
                                     self.multiplier, self.divider)

    def test_insufficient_operands(self):
        self.calculator.enter_number(0)

        with self.assertRaises(InsufficientOperands):
            self.calculator.add()

    def test_adder_call(self):
        self.adder.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.add()

        self.adder.calc.assert_called_with(1, 2)

    def test_subtracter_call(self):
        self.subtracter.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.subtract()

        self.subtracter.calc.assert_called_with(1, 2)

    def test_multiplier_call(self):
        self.multiplier.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.multiply()

        self.multiplier.calc.assert_called_with(1, 2)

    def test_divider_call(self):
        self.divider.calc = MagicMock(return_value=0)

        self.calculator.enter_number(1)
        self.calculator.enter_number(2)
        self.calculator.divide()

        self.divider.calc.assert_called_with(1, 2)

    def test_sequential_operations(self):
        # This test ensures that two operations can be performed in sequence
        # This requires that the first operation store its result so that the
        # second operation can use it

        # First, mock up calc methods since we aren't relying on their
        # correctness
        self.adder.calc = MagicMock(return_value=5)
        self.subtracter.calc = MagicMock(return_value=4)

        # Next, enter numbers and perform first operation
        self.calculator.enter_number(2)
        self.calculator.enter_number(3)
        self.calculator.add()
        # Now, the stack should contain only [5], which is the return value of
        # our adder
        self.assertEqual([5], self.calculator.stack)

        # Next, enter single number and perform subtraction
        self.calculator.enter_number(1)
        self.calculator.subtract()
        # Now, the stack should contain only [4], which is the return value of
        # our subtracter
        self.assertEqual([4], self.calculator.stack)
Exemplo n.º 45
0
import sys
from calculator.calculator import Calculator

__author__ = 'Frank van Heeswijk'

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: calculator_application \"<expression>\"")
        sys.exit(1)
    calculator = Calculator()
    expression = " ".join(sys.argv[1:])
    result = calculator.evaluate(expression)
    print(result)
Exemplo n.º 46
0
 def test_rpn_value_parentheses(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(2))
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("(2)")))
Exemplo n.º 47
0
 def test_rpn_value_parentheses_missing_left(self):
     calculator = Calculator()
     self.assertRaises(RuntimeError, calculator.to_rpn, calculator.tokenize("(2"))
Exemplo n.º 48
0
 def test_tokenize_value(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal(3))
     ]
     self.assertListEqual(expected, calculator.tokenize("3"))
Exemplo n.º 49
0
 def test_evaluate_operator_precedences(self):
     calculator = Calculator()
     self.assertEqual(Decimal(-14), calculator.evaluate("-3 * 5 + +1"))
     self.assertEqual(Decimal("6.5"), calculator.evaluate("8 / -16 - -7"))
     self.assertEqual(Decimal(30), calculator.evaluate("5 * 3 * 8 / 4 / 2 * 6 / 3"))
     self.assertEqual(Decimal(-3), calculator.evaluate("2 + 3 + 4 - 5 - 8 + 6 + 4 - 9"))
Exemplo n.º 50
0
 def test_rpn_decimal(self):
     calculator = Calculator()
     expected = [
         ValueToken(Decimal("6.2"))
     ]
     self.assertListEqual(expected, calculator.to_rpn(calculator.tokenize("6.2")))