Skip to content

TypeError handling and added Unit test #10497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions maths/ceil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
"""
import unittest


def ceil(x: float) -> int:
Expand All @@ -14,11 +15,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

unittest.main()
doctest.testmod()