From 94f0950e3464da5798ca1399dd7d740947fabdae Mon Sep 17 00:00:00 2001 From: mahiuddin-dev Date: Sun, 15 Oct 2023 13:49:58 +0600 Subject: [PATCH 1/2] TypeError handling and added Unit test --- maths/ceil.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/maths/ceil.py b/maths/ceil.py index 909e02b3f780..3f263b3567de 100644 --- a/maths/ceil.py +++ b/maths/ceil.py @@ -1,7 +1,7 @@ """ https://en.wikipedia.org/wiki/Floor_and_ceiling_functions """ - +import unittest def ceil(x: float) -> int: """ @@ -14,11 +14,36 @@ def ceil(x: float) -> int: >>> all(ceil(n) == math.ceil(n) for n ... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000)) True + + >>> ceil("not_a_number") # Add a test case with non-numeric input + Traceback (most recent call last): + ... + ValueError: Input must be a float or integer """ + if not isinstance(x, (int, float)): + raise ValueError("Input must be a float or integer") + return int(x) if x - int(x) <= 0 else int(x) + 1 +class TestCeil(unittest.TestCase): + + def test_ceil_float(self): + self.assertEqual(ceil(1.5), 2) + + def test_ceil_integer(self): + self.assertEqual(ceil(5), 5) + + def test_ceil_negative(self): + self.assertEqual(ceil(-1.5), -1) + + def test_ceil_non_numeric(self): + with self.assertRaises(ValueError) as context: + ceil("not_a_number") + self.assertEqual("Input must be a float or integer", str(context.exception)) + + if __name__ == "__main__": import doctest - - doctest.testmod() + unittest.main() + doctest.testmod() \ No newline at end of file From a544a426a137e2457d32ece43872f9b6c1dde8e1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 07:54:04 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/ceil.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/ceil.py b/maths/ceil.py index 3f263b3567de..57f24998e1e6 100644 --- a/maths/ceil.py +++ b/maths/ceil.py @@ -3,6 +3,7 @@ """ import unittest + def ceil(x: float) -> int: """ Return the ceiling of x as an Integral. @@ -22,12 +23,11 @@ def ceil(x: float) -> int: """ if not isinstance(x, (int, float)): raise ValueError("Input must be a float or integer") - + return int(x) if x - int(x) <= 0 else int(x) + 1 class TestCeil(unittest.TestCase): - def test_ceil_float(self): self.assertEqual(ceil(1.5), 2) @@ -45,5 +45,6 @@ def test_ceil_non_numeric(self): if __name__ == "__main__": import doctest + unittest.main() - doctest.testmod() \ No newline at end of file + doctest.testmod()