Skip to content

Commit 94f0950

Browse files
committed
TypeError handling and added Unit test
1 parent 3ecad36 commit 94f0950

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

maths/ceil.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
33
"""
4-
4+
import unittest
55

66
def ceil(x: float) -> int:
77
"""
@@ -14,11 +14,36 @@ def ceil(x: float) -> int:
1414
>>> all(ceil(n) == math.ceil(n) for n
1515
... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
1616
True
17+
18+
>>> ceil("not_a_number") # Add a test case with non-numeric input
19+
Traceback (most recent call last):
20+
...
21+
ValueError: Input must be a float or integer
1722
"""
23+
if not isinstance(x, (int, float)):
24+
raise ValueError("Input must be a float or integer")
25+
1826
return int(x) if x - int(x) <= 0 else int(x) + 1
1927

2028

29+
class TestCeil(unittest.TestCase):
30+
31+
def test_ceil_float(self):
32+
self.assertEqual(ceil(1.5), 2)
33+
34+
def test_ceil_integer(self):
35+
self.assertEqual(ceil(5), 5)
36+
37+
def test_ceil_negative(self):
38+
self.assertEqual(ceil(-1.5), -1)
39+
40+
def test_ceil_non_numeric(self):
41+
with self.assertRaises(ValueError) as context:
42+
ceil("not_a_number")
43+
self.assertEqual("Input must be a float or integer", str(context.exception))
44+
45+
2146
if __name__ == "__main__":
2247
import doctest
23-
24-
doctest.testmod()
48+
unittest.main()
49+
doctest.testmod()

0 commit comments

Comments
 (0)