Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

challenge: add two integers challenge #54

Merged
merged 14 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions solutions/function_adding_two_integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This module provides a utility function for basic arithmetic operations.

Functions:
adding_two_integers(num1: int, num2: int) -> int:
Calculates the adding_two_integers of two integers.

Example:
aoyeoku marked this conversation as resolved.
Show resolved Hide resolved
To use the `adding_two_integers` function:

>>> result = adding_two_integers(5, 7)
12
>>> adding_two_integers(0, 0)
0

>>> adding_two_integers(-5, 5)
0

>>> adding_two_integers(100, 200)
300

"""

aoyeoku marked this conversation as resolved.
Show resolved Hide resolved

def adding_two_integers(num1: int, num2: int) -> int:
"""
Calculates the sum of two integers.

Args:
num1 (int): The first integer to be added.
num2 (int): The second integer to be added.

Returns:
int: The sum of num1 and num2.

Raises:
TypeError: If either of the inputs is not an integer.
"""
# Check if inputs are integers
if not isinstance(num1, int) or not isinstance(num2, int):
raise TypeError("Inputs must be integers.")

# Return the sum
return num1 + num2
118 changes: 118 additions & 0 deletions solutions/tests/test_function_adding_two_integers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Unit tests for the arithmetic module.

This script tests the `adding_two_integers` function to ensure it behaves as expected
under various conditions. It uses Python's built-in unittest framework.

Test Categories:
- Standard: Typical scenarios for the function.
- Edge Case: Boundary or extreme conditions to test function limits.
- Defensive: Scenarios where invalid or unexpected input is provided.
"""

import unittest

from ..function_adding_two_integers import adding_two_integers


class TestAddingTwoIntegersFunction(unittest.TestCase):
"""
Test cases for the `adding_two_integers` function.
"""

# Standard Tests
def test_positive_integers(self):
"""
Standard: Test with two positive integers.
"""
self.assertEqual(
adding_two_integers(3, 5), 8, "adding_two_integers of 3 and 5 should be 8."
)

def test_negative_integers(self):
"""
Standard: Test with two negative integers.
"""
self.assertEqual(
adding_two_integers(-3, -5),
-8,
"adding_two_integers of -3 and -5 should be -8.",
)

def test_mixed_integers(self):
"""
Standard: Test with a positive and a negative integer.
"""
self.assertEqual(
adding_two_integers(5, -3),
2,
"adding_two_integers of 5 and -3 should be 2.",
)

def test_with_zero(self):
"""
Standard: Test with zero as one of the integers.
"""
self.assertEqual(
adding_two_integers(0, 5), 5, "adding_two_integers of 0 and 5 should be 5."
)

# Edge Case Tests
def test_large_numbers(self):
"""
Edge Case: Test with very large integers.
"""
self.assertEqual(
adding_two_integers(10**9, 10**9), 2 * 10**9, "Adding large numbers failed."
)

def test_minimum_and_maximum_values(self):
"""
Edge Case: Test with minimum and maximum integer values.
"""
self.assertEqual(
adding_two_integers(-(2**31), 2**31 - 1),
-1,
"Adding minimum and maximum integers failed.",
)

def test_close_to_zero(self):
"""
Edge Case: Test with numbers close to zero.
"""
self.assertEqual(
adding_two_integers(1, -1), 0, "Adding numbers close to zero failed."
)

# Defensive Tests
def test_non_integer_inputs(self):
"""
Defensive: Test with non-integer inputs.
"""
with self.assertRaises(TypeError):
adding_two_integers("3", 5)

def test_missing_arguments(self):
"""
Defensive: Test with missing arguments.
"""
with self.assertRaises(TypeError):
adding_two_integers(None, 5)

def test_extra_arguments(self):
"""
Defensive: Test with extra arguments.
"""

def test_none_arguments(self):
"""
Defensive: Test with None as arguments.
"""
with self.assertRaises(TypeError):
adding_two_integers(None, 5)


if __name__ == "__main__":
unittest.main()
Loading