Skip to content

Commit

Permalink
my first changes to factorial calculator challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
linahKhayri committed Dec 29, 2024
1 parent 2586570 commit 74e48ba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion solutions/factorial_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ def factorial_calculator(num: int | float) -> int:
if num == 1: # Base case 2
return 1 # turn-around 2

# Recursive case: factorial(n) = n * factorial(n-1)
# Recursive case: factorial(num) = num * factorial(num-1)
# breaking-down num | Build-up by multiplying
return num * factorial_calculator(num - 1)
39 changes: 34 additions & 5 deletions solutions/tests/test_factorial_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
# -*- coding: utf-8 -*-
"""
Test module for factorial_calculator function.
Contains comprehensive tests to ensure correctness
of the factorial function.
Contains comprehensive tests to ensure the correctness
of the factorial_calculator function.
is designed to work only for positive integers and whole floats,
which are transformed to integers before calculation
Test categories:
- Standard cases: Positive integers
- Edge cases: zero factorial, one factorial, whole floats, large integers
- Defensive tests: negative inputs, none-whole floats, non-integer input, none input
- Defensive tests: negative inputs, non-whole floats, non-integer input
Created on 29-12-2024
Author: Linah Khayri
Expand Down Expand Up @@ -53,13 +55,40 @@ def test_factorial_12(self):

# Edge cases
def test_factorial_0(self):
"""It should return 1 as a base case of this function and special factorial case"""
"""Test for factorial of 0 (special case). Factorial of 0 is always 1 by definition."""
self.assertEqual(factorial_calculator(0), 1)

def test_factorial_1(self):
"""It should return 1 as a base case of this function and special factorial case"""
"""Test for factorial of 1 (special case). Factorial of 1 is always 1 by definition."""
self.assertEqual(factorial_calculator(1), 1)

def test_factorial_whole_float(self):
"""It should handle whole floats and return the factorial as an integer"""
self.assertEqual(factorial_calculator(7.0), 5040)

def test_factorial_large_integer(self):
"""It should handle large integers"""
self.assertEqual(
factorial_calculator(50),
30414093201713378043612608166064768844377641568960512000000000000,
)

def test_factorial_large_whole_float(self):
"""It should handle large whole floats"""
self.assertEqual(factorial_calculator(20.0), 2432902008176640000)

# Defensive tests:
def test_negative_factorial_calculator(self):
"""should raise an error if the input num is a negative number"""
with self.assertRaises(AssertionError):
factorial_calculator(-5)

def test_fractional_float_factorial_calculator(self):
"""should raise an error if the input num is not a whole float"""
with self.assertRaises(AssertionError):
factorial_calculator(3.5)

def test_non_numeric_factorial_calculator(self):
"""should raise an error if the input num is a negative number"""
with self.assertRaises(AssertionError):
factorial_calculator("cat")

0 comments on commit 74e48ba

Please sign in to comment.