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

Multiply two numbers #139

Merged
merged 9 commits into from
Jan 12, 2025
45 changes: 45 additions & 0 deletions solutions/multiply_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
A module for multiplying two integers.

This module provides the multiply_two_numbers function, which multiplies
two integers and returns their product.

Module Contents:
- multiply_two_numbers: A function to multiply two integers.

Created: 2025-01-04
Author: Manezhah Mohmand
"""


def multiply_two_numbers(num_one: int, num_two: int) -> int:
"""
Multiplies two integers and returns the product.

Parameters:
num_one (int): The first integer.
num_two (int): The second integer.

Returns:
int: The product of the two integers.

Raises:
TypeError: If either num_one or num_two is not an integer.

Examples:
>>> multiply_two_numbers(2, 3)
6
>>> multiply_two_numbers(-4, 5)
-20
>>> multiply_two_numbers(-2, -3)
6
>>> multiply_two_numbers(0, 10)
0
"""
# input validation for defensive test
if not isinstance(num_one, int) or not isinstance(num_two, int):
raise TypeError("Both arguments must be integers.")
return num_one * num_two
64 changes: 64 additions & 0 deletions solutions/tests/test_multiply_two_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Test module for the multiply_two_numbers function.

This module contains unit tests for the multiply_two_numbers function,
including standard cases, boundary cases, and defensive tests.

Created: 2025-01-04
Author: Manezhah Mohmand
"""

import unittest

from solutions.multiply_two_numbers import multiply_two_numbers


class TestMultiplyTwoNumbers(unittest.TestCase):
"""
Unit tests for the `multiply_two_numbers` function.

This test suite covers:
- Standard cases (e.g., positive and negative integers).
- Boundary cases (e.g., zero).
- Defensive tests for invalid inputs.
"""

# Standard cases
def test_two_positive_numbers(self):
"""It should return the product of two positive integers."""
self.assertEqual(multiply_two_numbers(2, 3), 6)

def test_two_negative_numbers(self):
"""It should return the product of two negative integers."""
self.assertEqual(multiply_two_numbers(-2, -3), 6)

def test_positive_and_negative_number(self):
"""It should return the product of a positive and a negative integer."""
self.assertEqual(multiply_two_numbers(-2, 3), -6)

# Boundary cases
def test_zero_with_positive_number(self):
"""It should return 0 when one number is 0 and the other is positive."""
self.assertEqual(multiply_two_numbers(0, 5), 0)

def test_zero_with_negative_number(self):
"""It should return 0 when one number is 0 and the other is negative."""
self.assertEqual(multiply_two_numbers(0, -7), 0)

# Defensive tests
def test_non_integer_input(self):
"""It should raise a TypeError when the inputs are not integers."""
with self.assertRaises(TypeError):
multiply_two_numbers(2.5, 3)

def test_string_input(self):
"""It should raise a TypeError when one or both inputs are strings."""
with self.assertRaises(TypeError):
multiply_two_numbers("2", "3")


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