Skip to content

Commit

Permalink
Merge pull request #41 from MIT-Emerging-Talent/add_two_numbers
Browse files Browse the repository at this point in the history
Adding the Add_numbers function
  • Loading branch information
fevziismailsahin authored Jan 13, 2025
2 parents 6047d24 + 4483867 commit 333d883
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
39 changes: 39 additions & 0 deletions solutions/add_numbers.py
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
64 changes: 64 additions & 0 deletions solutions/tests/test_add_numbers.py
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()

0 comments on commit 333d883

Please sign in to comment.