forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from MIT-Emerging-Talent/feature/is-positive-…
…challenge Add is_positive function with unit tests
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
A module for checking if a number is positive. | ||
Module contents: | ||
- is_positive: Determines if a given number is positive. | ||
Author: [Your name] | ||
Created: 2025-01-08 | ||
""" | ||
|
||
|
||
def is_positive(n: float) -> bool: | ||
"""Determines if a given number is positive. | ||
A number is considered positive if it is greater than 0. | ||
Raises: | ||
TypeError: If the input is not a real number (int or float). | ||
Parameters: | ||
n: float or int, the number to check. | ||
Returns: | ||
bool: True if the number is greater than 0, False otherwise. | ||
Examples: | ||
>>> is_positive(5) | ||
True | ||
>>> is_positive(-3) | ||
False | ||
>>> is_positive(0) | ||
False | ||
>>> is_positive(3.14) | ||
True | ||
>>> is_positive(-2.5) | ||
False | ||
""" | ||
if isinstance(n, bool): | ||
raise TypeError("Input must be a real number (int or float)") | ||
if not isinstance(n, (int, float)): | ||
raise TypeError("Input must be a real number (int or float)") | ||
return n > 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Test module for is_positive function. | ||
Module contents: | ||
- TestIsPositive: Unit test for the is_positive function. | ||
Test categories: | ||
- Standard cases: positive numbers, negative numbers, zero | ||
- Edge cases: very large numbers, very small numbers, floating points | ||
- Defensive tests: wrong input types | ||
""" | ||
|
||
import unittest | ||
|
||
from ..is_positive import is_positive | ||
|
||
|
||
class TestIsPositive(unittest.TestCase): | ||
"""Test suite for the is_positive function.""" | ||
|
||
# Standard test cases | ||
def test_basic_positive_integer(self): | ||
"""It should return True for basic positive integer.""" | ||
self.assertTrue(is_positive(5)) | ||
|
||
def test_basic_negative_integer(self): | ||
"""It should return False for basic negative integer.""" | ||
self.assertFalse(is_positive(-5)) | ||
|
||
def test_positive_large_integer(self): | ||
"""It should return True for large positive integer.""" | ||
self.assertTrue(is_positive(1000000)) | ||
|
||
def test_negative_large_integer(self): | ||
"""It should return False for large negative integer.""" | ||
self.assertFalse(is_positive(-1000000)) | ||
|
||
def test_zero(self): | ||
"""It should return False for zero.""" | ||
self.assertFalse(is_positive(0)) | ||
|
||
# Edge cases | ||
def test_very_large_positive_number(self): | ||
"""It should handle very large positive numbers correctly.""" | ||
self.assertTrue(is_positive(1e308)) | ||
|
||
def test_very_large_negative_number(self): | ||
"""It should handle very large negative numbers correctly.""" | ||
self.assertFalse(is_positive(-1e308)) | ||
|
||
def test_very_small_positive_number(self): | ||
"""It should handle very small positive numbers correctly.""" | ||
self.assertTrue(is_positive(1e-308)) | ||
|
||
def test_very_small_negative_number(self): | ||
"""It should handle very small negative numbers correctly.""" | ||
self.assertFalse(is_positive(-1e-308)) | ||
|
||
# Floating point tests | ||
def test_positive_float(self): | ||
"""It should return True for positive float.""" | ||
self.assertTrue(is_positive(0.1)) | ||
self.assertTrue(is_positive(3.14)) | ||
|
||
def test_negative_float(self): | ||
"""It should return False for negative float.""" | ||
self.assertFalse(is_positive(-0.1)) | ||
self.assertFalse(is_positive(-3.14)) | ||
|
||
# Defensive tests | ||
def test_string_input(self): | ||
"""It should raise TypeError for string input.""" | ||
with self.assertRaises(TypeError): | ||
is_positive("5") | ||
|
||
def test_none_input(self): | ||
"""It should raise TypeError for None input.""" | ||
with self.assertRaises(TypeError): | ||
is_positive(None) | ||
|
||
def test_boolean_true_input(self): | ||
"""It should raise TypeError for boolean True input.""" | ||
with self.assertRaises(TypeError): | ||
is_positive(True) | ||
|
||
def test_boolean_false_input(self): | ||
"""It should raise TypeError for boolean False input.""" | ||
with self.assertRaises(TypeError): | ||
is_positive(False) | ||
|
||
def test_complex_number_input(self): | ||
"""It should raise TypeError for complex number input.""" | ||
with self.assertRaises(TypeError): | ||
is_positive(1 + 2j) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |