Skip to content

Commit

Permalink
Merge pull request #154 from MIT-Emerging-Talent/feature/is-positive-…
Browse files Browse the repository at this point in the history
…challenge

Add is_positive function with unit tests
  • Loading branch information
RandomProjects-db authored Jan 12, 2025
2 parents 85b90a1 + a6eaadf commit 0909ef0
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
44 changes: 44 additions & 0 deletions solutions/is_positive.py
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
100 changes: 100 additions & 0 deletions solutions/tests/test_is_positive.py
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()

0 comments on commit 0909ef0

Please sign in to comment.