forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #41 from MIT-Emerging-Talent/add_two_numbers
Adding the Add_numbers function
- Loading branch information
Showing
2 changed files
with
103 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,39 @@ | ||
# add_numbers.py | ||
|
||
""" | ||
This module provides a function to add two numbers. | ||
Function: | ||
- add_numbers: float: The sum of the two numbers. | ||
Created on: January 6, 2025 | ||
@author: Melat Assefa | ||
""" | ||
|
||
|
||
def add_numbers(a: float, b: float) -> float: | ||
""" | ||
Calculate the sum of two numbers. | ||
Parameters: | ||
a (float): The first number to add. It can be any real number. | ||
b (float): The second number to add. It can be any real number. | ||
Returns: | ||
float: The sum of the two numbers. | ||
Raises: | ||
TypeError: If either of the arguments is not a number. | ||
Examples: | ||
>>> add_numbers(2, 3) | ||
5 | ||
>>> add_numbers(-1, 1) | ||
0 | ||
>>> add_numbers(0.5, 0.5) | ||
1.0 | ||
""" | ||
|
||
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | ||
raise TypeError("Both arguments must be numbers.") | ||
|
||
return a + b |
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,64 @@ | ||
# tests/test_add_two_numbers.py | ||
|
||
""" | ||
Unit tests for the add_two_numbers function. | ||
This test suite includes: | ||
- Regular cases: Typical inputs that the function is expected to handle. | ||
- Edge cases: Inputs that are at the boundary of what the function should handle. | ||
- Error cases: Inputs that should raise exception due to invalid input. | ||
Created on: January 6, 2025 | ||
@author: Melat Assefa | ||
""" | ||
|
||
import unittest | ||
from solutions.add_numbers import add_numbers | ||
|
||
|
||
class TestAddNumbers(unittest.TestCase): | ||
"""Unit tests for the add_numbers function.""" | ||
|
||
# Regular Cases | ||
def test_positive_numbers(self): | ||
"""Test adding two positive numbers.""" | ||
self.assertEqual(add_numbers(2, 3), 5) | ||
|
||
def test_negative_numbers(self): | ||
"""Test adding two negative numbers.""" | ||
self.assertEqual(add_numbers(-2, -3), -5) | ||
|
||
def test_mixed_sign_numbers(self): | ||
"""Test adding a positive and a negative number.""" | ||
self.assertEqual(add_numbers(-1, 1), 0) | ||
|
||
def test_floats(self): | ||
"""Test adding two floating-point numbers.""" | ||
self.assertEqual(add_numbers(0.5, 0.5), 1.0) | ||
|
||
# Edge Cases | ||
def test_add_zero_to_number(self): | ||
"""Test adding zero to a number.""" | ||
self.assertEqual(add_numbers(0, 5), 5) | ||
|
||
def test_add_number_to_zero(self): | ||
"""Test adding a number to zero.""" | ||
self.assertEqual(add_numbers(5, 0), 5) | ||
|
||
def test_large_numbers(self): | ||
"""Test adding very large numbers.""" | ||
self.assertEqual(add_numbers(1e308, 1e308), 2e308) | ||
|
||
# Defensive Cases | ||
def test_type_error_with_string_first(self): | ||
"""Test that a TypeError is raised for non-numeric input as first argument.""" | ||
with self.assertRaises(TypeError): | ||
add_numbers("a", 1) | ||
|
||
def test_type_error_with_string_second(self): | ||
"""Test that a TypeError is raised for non-numeric input as second argument.""" | ||
with self.assertRaises(TypeError): | ||
add_numbers(1, "b") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |