Skip to content

Commit

Permalink
Optimized code for review
Browse files Browse the repository at this point in the history
  • Loading branch information
Husayn01 committed Dec 31, 2024
1 parent f0db591 commit 01f645c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 69 deletions.
16 changes: 8 additions & 8 deletions solutions/prime_number_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"""


def is_prime(number: int) -> bool:
def prime_number_checker(number: int) -> bool:
"""
Checks if a number is prime.
Expand All @@ -24,19 +24,19 @@ def is_prime(number: int) -> bool:
TypeError: If the input is not an integer.
Examples:
>>> is_prime(2)
>>> prime_number_checker(2)
True
>>> is_prime(3)
>>> prime_number_checker(3)
True
>>> is_prime(4)
>>> prime_number_checker(4)
False
>>> is_prime(13)
>>> prime_number_checker(13)
True
>>> is_prime(1)
>>> prime_number_checker(1)
False
>>> is_prime(0)
>>> prime_number_checker(0)
False
>>> is_prime(-5)
>>> prime_number_checker(-5)
False
"""
if not isinstance(number, int):
Expand Down
147 changes: 86 additions & 61 deletions solutions/tests/test_prime_number_checker.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,112 @@
"""
Unit test module for checking the correctness of the is_prime function.
Unit test module for checking the correctness of the prime_number_checker function.
This module contains test cases for various scenarios such as:
- Valid prime numbers
- Valid non-prime numbers
- Edge cases
- Invalid input types
Tests are designed to ensure that the is_prime function handles all relevant cases correctly.
Tests are designed to ensure that the prime_number_checker
function handles all relevant cases correctly.
Created on 2024-12-30
Author: Hussaini Ahmed
"""

import unittest

from solutions.prime_number_checker import is_prime
from solutions.prime_number_checker import prime_number_checker


class TestIsPrime(unittest.TestCase):
class PrimeNumberCheckerTests(unittest.TestCase):
"""
Test case class for the is_prime function. This class includes various test methods to validate
the correctness of the is_prime function. The test cases cover prime numbers, non-prime numbers,
edge cases, negative numbers, and invalid input types.
Methods:
- test_prime_numbers: Tests known prime numbers.
- test_non_prime_numbers: Tests known non-prime numbers.
- test_negative_numbers: Tests negative numbers (which should all be non-prime).
- test_edge_cases: Tests edge cases like small and consecutive numbers.
- test_invalid_input: Tests invalid input types and ensures TypeError is raised.
Test suite for verifying prime number checking functionality.
Tests cover prime numbers, non-prime numbers, edge cases,
and input validation.
"""

def test_prime_numbers(self):
"""
Test method to validate the correctness of is_prime for known prime numbers.
"""
self.assertTrue(is_prime(2)) # 2 is prime
self.assertTrue(is_prime(3)) # 3 is prime
self.assertTrue(is_prime(13)) # 13 is prime
self.assertTrue(is_prime(97)) # 97 is prime
self.assertTrue(is_prime(7919)) # Large prime number

def test_non_prime_numbers(self):
"""
Test method to validate the correctness of is_prime for known non-prime numbers.
"""
self.assertFalse(is_prime(0)) # 0 is not prime
self.assertFalse(is_prime(1)) # 1 is not prime
self.assertFalse(is_prime(4)) # 4 is not prime
self.assertFalse(is_prime(100)) # 100 is not prime
self.assertFalse(is_prime(12345)) # 12345 is not prime

def test_negative_numbers(self):
"""
Test method to validate that all negative numbers are correctly identified as non-prime.
"""
self.assertFalse(is_prime(-1)) # -1 is not prime
self.assertFalse(is_prime(-10)) # -10 is not prime
self.assertFalse(is_prime(-97)) # -97 is not prime

def test_edge_cases(self):
"""
Test method to validate edge cases, such as the smallest numbers and consecutive numbers.
"""
self.assertFalse(is_prime(0)) # 0 is not prime
self.assertFalse(is_prime(1)) # 1 is not prime
self.assertTrue(is_prime(2)) # 2 is the smallest prime
self.assertTrue(is_prime(3)) # 3 is a prime number

def test_invalid_input(self):
"""
Test method to validate that invalid input types raise a TypeError.
This includes inputs like strings, floats, None, lists, and dictionaries.
"""
# Prime Number Tests
def test_two_is_prime(self):
"""Verify that 2 is correctly identified as the smallest prime number."""
self.assertTrue(prime_number_checker(2))

def test_ninety_seven_is_prime(self):
"""Verify that 97 is correctly identified as a prime number."""
self.assertTrue(prime_number_checker(97))

def test_large_prime_number(self):
"""Verify that a large prime number (7919) is correctly identified."""
self.assertTrue(prime_number_checker(7919))

# Non-Prime Number Tests
def test_zero_is_not_prime(self):
"""Verify that 0 is correctly identified as not prime."""
self.assertFalse(prime_number_checker(0))

def test_hundred_is_not_prime(self):
"""Verify that 100 is correctly identified as not prime."""
self.assertFalse(prime_number_checker(100))

def test_large_non_prime(self):
"""Verify that a large non-prime number (12345) is correctly identified."""
self.assertFalse(prime_number_checker(12345))

# Boundary Cases
def test_first_prime_after_one(self):
"""Verify the boundary between non-prime 1 and prime 2."""
self.assertTrue(prime_number_checker(2))

def test_first_even_non_prime(self):
"""Verify the first even non-prime number (4)."""
self.assertFalse(prime_number_checker(4))

def test_smallest_two_digit_prime(self):
"""Verify the first two-digit prime number (11)."""
self.assertTrue(prime_number_checker(11))

def test_largest_two_digit_prime(self):
"""Verify the largest two-digit prime number (97)."""
self.assertTrue(prime_number_checker(97))

def test_consecutive_numbers_around_prime(self):
"""Verify behavior with consecutive numbers around a prime."""
self.assertFalse(prime_number_checker(8))
self.assertTrue(prime_number_checker(7))
self.assertFalse(prime_number_checker(6))

# Negative Number Tests
def test_negative_one_is_not_prime(self):
"""Verify that -1 is correctly identified as not prime."""
self.assertFalse(prime_number_checker(-1))

def test_negative_prime_equivalent_is_not_prime(self):
"""Verify that negative of a prime number (-7) is not prime."""
self.assertFalse(prime_number_checker(-7))

# Input Validation Tests
def test_string_input_raises_type_error(self):
"""Verify that string input raises TypeError."""
with self.assertRaises(TypeError):
is_prime("string") # Invalid input: string
prime_number_checker("2")

def test_float_input_raises_type_error(self):
"""Verify that float input raises TypeError."""
with self.assertRaises(TypeError):
is_prime(3.14) # Invalid input: float
prime_number_checker(3.14)

def test_none_input_raises_type_error(self):
"""Verify that None input raises TypeError."""
with self.assertRaises(TypeError):
is_prime(None) # Invalid input: None
prime_number_checker(None)

def test_list_input_raises_type_error(self):
"""Verify that list input raises TypeError."""
with self.assertRaises(TypeError):
is_prime([2, 3, 5]) # Invalid input: list
prime_number_checker([2, 3, 5])

def test_dict_input_raises_type_error(self):
"""Verify that dictionary input raises TypeError."""
with self.assertRaises(TypeError):
is_prime({"number": 7}) # Invalid input: dictionary
prime_number_checker({"number": 7})


if __name__ == "__main__":
Expand Down

0 comments on commit 01f645c

Please sign in to comment.