From 2490e1a5bd105fe545168f5642109dc13b0a6d0d Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 30 Nov 2019 16:27:45 +0100 Subject: [PATCH] calculator: use commonly defined operations --- calculator/calculator/__init__.py | 1 + calculator/calculator/core/__init__.py | 1 + calculator/calculator/core/operations.py | 35 ++++++++++++++++++++ calculator/calculator/eval/test_evaluator.py | 24 +++++++++++--- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 calculator/calculator/core/__init__.py create mode 100644 calculator/calculator/core/operations.py diff --git a/calculator/calculator/__init__.py b/calculator/calculator/__init__.py index 14294ba..ee7f935 100644 --- a/calculator/calculator/__init__.py +++ b/calculator/calculator/__init__.py @@ -1,2 +1,3 @@ +import calculator.core # isort:skip import calculator.ast import calculator.eval diff --git a/calculator/calculator/core/__init__.py b/calculator/calculator/core/__init__.py new file mode 100644 index 0000000..a70d7cf --- /dev/null +++ b/calculator/calculator/core/__init__.py @@ -0,0 +1 @@ +import calculator.core.operations diff --git a/calculator/calculator/core/operations.py b/calculator/calculator/core/operations.py new file mode 100644 index 0000000..e6a718f --- /dev/null +++ b/calculator/calculator/core/operations.py @@ -0,0 +1,35 @@ +def plus(x: int, y: int) -> int: + return x + y + + +def minus(x: int, y: int) -> int: + return x - y + + +def times(x: int, y: int) -> int: + return x * y + + +def int_div(x: int, y: int) -> int: + return x // y + + +def pow(x: int, y: int) -> int: + return x ** y + + +def identity(x: int) -> int: + return x + + +def negate(x: int) -> int: + return -x + + +STR_TO_BIN = { + "+": plus, + "-": minus, + "*": times, + "/": int_div, + "^": pow, +} diff --git a/calculator/calculator/eval/test_evaluator.py b/calculator/calculator/eval/test_evaluator.py index 6aa866f..91035c6 100644 --- a/calculator/calculator/eval/test_evaluator.py +++ b/calculator/calculator/eval/test_evaluator.py @@ -1,5 +1,6 @@ import pytest from calculator.ast import BinOp, Constant, UnaryOp +from calculator.core import operations from .evaluator import Evaluator @@ -15,20 +16,35 @@ def test_evaluate_constant(vis): def test_evaluate_unaryop_identity(vis): - op = UnaryOp(lambda x: x, Constant(42)) + op = UnaryOp(operations.identity, Constant(42)) assert vis.visit_and_get_value(op) == 42 def test_evaluate_unaryop_negate(vis): - op = UnaryOp(lambda x: -x, Constant(-42)) + op = UnaryOp(operations.negate, Constant(-42)) assert vis.visit_and_get_value(op) == 42 def test_evaluate_binop_plus(vis): - op = BinOp(lambda x, y: x + y, Constant(12), Constant(27)) + op = BinOp(operations.plus, Constant(12), Constant(27)) assert vis.visit_and_get_value(op) == 39 def test_evaluate_binop_minus(vis): - op = BinOp(lambda x, y: x - y, Constant(42), Constant(51)) + op = BinOp(operations.minus, Constant(42), Constant(51)) assert vis.visit_and_get_value(op) == -9 + + +def test_evaluate_binop_times(vis): + op = BinOp(operations.times, Constant(6), Constant(9)) + assert vis.visit_and_get_value(op) == 54 + + +def test_evaluate_binop_int_div(vis): + op = BinOp(operations.int_div, Constant(5), Constant(2)) + assert vis.visit_and_get_value(op) == 2 + + +def test_evaluate_binop_pow(vis): + op = BinOp(operations.pow, Constant(3), Constant(4)) + assert vis.visit_and_get_value(op) == 81