forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #139 from MIT-Emerging-Talent/multiply-two-numbers
Multiply two numbers
- Loading branch information
Showing
2 changed files
with
109 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,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 |
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 @@ | ||
#!/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() |