From 28d030378bbe41c5398c337a5a987929eb11353c Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Tue, 7 May 2024 16:01:06 +0200 Subject: [PATCH 1/3] test: add tests for trigonometric functionalities --- tests/test_gui.py | 19 ++++++++++++++++++- tests/test_model.py | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/test_gui.py b/tests/test_gui.py index 1211882..9192589 100644 --- a/tests/test_gui.py +++ b/tests/test_gui.py @@ -70,4 +70,21 @@ def test_expression_with_pow(self): self.assert_display("2**3") self.press_button("=") self.assert_display("8") - \ No newline at end of file + + def test_expression_with_cos(self): + # cos(0) = 1 + self.press_button("cos") + self.press_button("0") + self.press_button(")") + self.assert_display("cos(0)") + self.press_button("=") + self.assert_display("1") + + def test_expression_with_sin(self): + # sin(0) = 0 + self.press_button("sin") + self.press_button("0") + self.press_button(")") + self.assert_display("sin(0)") + self.press_button("=") + self.assert_display("0") diff --git a/tests/test_model.py b/tests/test_model.py index abf17d9..1acf17e 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -89,3 +89,24 @@ def test_expression_with_pow(self): self.calculator.digit(3) self.assertEqual("(1+1)**3", self.calculator.expression) self.assertEqual(8, self.calculator.compute_result()) + + def test_cosine(self): + # cos(0) = 1 + self.assertEqual("", self.calculator.expression) + self.calculator.cos() + self.assertEqual("cos(", self.calculator.expression) + self.calculator.digit(0) + self.calculator.close_parenthesis() + self.assertEqual("cos(0)", self.calculator.expression) + self.assertEqual(1, self.calculator.compute_result()) + + def test_sine(self): + # sin(0) = 0 + self.assertEqual("", self.calculator.expression) + self.calculator.sin() + self.assertEqual("sin(", self.calculator.expression) + self.calculator.digit(0) + self.calculator.close_parenthesis() + self.assertEqual("sin(0)", self.calculator.expression) + self.assertEqual(0, self.calculator.compute_result()) + \ No newline at end of file From c7e11cb50ce5f909bd53cd17cf624d5be45a6aaf Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Tue, 7 May 2024 16:07:57 +0200 Subject: [PATCH 2/3] feat: implement cos and sin --- calculator/__init__.py | 8 +++++++- calculator/ui/gui.py | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/calculator/__init__.py b/calculator/__init__.py index 59b3cca..99c07e6 100644 --- a/calculator/__init__.py +++ b/calculator/__init__.py @@ -52,10 +52,16 @@ def power(self): def logarithm(self): self._append("log") + + def cos(self): + self._append("cos") + + def sin(self): + self._append("sin") def compute_result(self) -> Number: try: - from math import sqrt + from math import sqrt, cos, sin result = eval(self.expression) if isinstance(result, Number): self.expression = str(result) diff --git a/calculator/ui/gui.py b/calculator/ui/gui.py index d4f4056..221ca9b 100644 --- a/calculator/ui/gui.py +++ b/calculator/ui/gui.py @@ -11,7 +11,7 @@ ['1', '2', '3', '-'], ['.', '0', '=', '+'], ['(', '√', '^', ')'], - ['log'], + ['log', 'cos', 'sin'], ] @@ -81,6 +81,10 @@ def on_button_press(self, button): case "log": self._calc.logarithm() self._calc.open_parenthesis() + case "cos": + self._calc.cos() + case "sin": + self._calc.sin() case _: self._calc.digit(button.text) self.display.text = self._calc.expression or "0" From d2ef2fbcc0776dd1c381ab218eba6460bb75223e Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Tue, 7 May 2024 16:13:27 +0200 Subject: [PATCH 3/3] fix: bug in how cos and sin were implemented --- calculator/ui/gui.py | 2 ++ tests/test_gui.py | 4 ++-- tests/test_model.py | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/calculator/ui/gui.py b/calculator/ui/gui.py index 221ca9b..79e1c1d 100644 --- a/calculator/ui/gui.py +++ b/calculator/ui/gui.py @@ -83,8 +83,10 @@ def on_button_press(self, button): self._calc.open_parenthesis() case "cos": self._calc.cos() + self._calc.open_parenthesis() case "sin": self._calc.sin() + self._calc.open_parenthesis() case _: self._calc.digit(button.text) self.display.text = self._calc.expression or "0" diff --git a/tests/test_gui.py b/tests/test_gui.py index 9192589..ae3d74a 100644 --- a/tests/test_gui.py +++ b/tests/test_gui.py @@ -78,7 +78,7 @@ def test_expression_with_cos(self): self.press_button(")") self.assert_display("cos(0)") self.press_button("=") - self.assert_display("1") + self.assert_display("1.0") def test_expression_with_sin(self): # sin(0) = 0 @@ -87,4 +87,4 @@ def test_expression_with_sin(self): self.press_button(")") self.assert_display("sin(0)") self.press_button("=") - self.assert_display("0") + self.assert_display("0.0") diff --git a/tests/test_model.py b/tests/test_model.py index 1acf17e..790984c 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -94,6 +94,7 @@ def test_cosine(self): # cos(0) = 1 self.assertEqual("", self.calculator.expression) self.calculator.cos() + self.calculator.open_parenthesis() self.assertEqual("cos(", self.calculator.expression) self.calculator.digit(0) self.calculator.close_parenthesis() @@ -104,6 +105,7 @@ def test_sine(self): # sin(0) = 0 self.assertEqual("", self.calculator.expression) self.calculator.sin() + self.calculator.open_parenthesis() self.assertEqual("sin(", self.calculator.expression) self.calculator.digit(0) self.calculator.close_parenthesis()