Add math helper module
Currently with GCD and LCD functions, along with unit tests.
This commit is contained in:
0
src/corelibs/math_handling/__init__.py
Normal file
0
src/corelibs/math_handling/__init__.py
Normal file
35
src/corelibs/math_handling/math_helpers.py
Normal file
35
src/corelibs/math_handling/math_helpers.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""
|
||||
Various math helpers
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
def gcd(a: int, b: int):
|
||||
"""
|
||||
Calculate: Greatest Common Divisor
|
||||
|
||||
Arguments:
|
||||
a {int} -- _description_
|
||||
b {int} -- _description_
|
||||
|
||||
Returns:
|
||||
_type_ -- _description_
|
||||
"""
|
||||
return math.gcd(a, b)
|
||||
|
||||
|
||||
def lcd(a: int, b: int):
|
||||
"""
|
||||
Calculate: Least Common Denominator
|
||||
|
||||
Arguments:
|
||||
a {int} -- _description_
|
||||
b {int} -- _description_
|
||||
|
||||
Returns:
|
||||
_type_ -- _description_
|
||||
"""
|
||||
return math.lcm(a, b)
|
||||
|
||||
# __END__
|
||||
0
tests/unit/math_handling/__init__.py
Normal file
0
tests/unit/math_handling/__init__.py
Normal file
121
tests/unit/math_handling/test_math_helpers.py
Normal file
121
tests/unit/math_handling/test_math_helpers.py
Normal file
@@ -0,0 +1,121 @@
|
||||
"""
|
||||
Unit tests for math_helpers module
|
||||
"""
|
||||
|
||||
from corelibs.math_handling.math_helpers import gcd, lcd
|
||||
|
||||
|
||||
class TestGcd:
|
||||
"""Test cases for the gcd (Greatest Common Divisor) function"""
|
||||
|
||||
def test_gcd_basic_positive_numbers(self):
|
||||
"""Test GCD with basic positive numbers"""
|
||||
assert gcd(12, 8) == 4
|
||||
assert gcd(15, 10) == 5
|
||||
assert gcd(21, 14) == 7
|
||||
|
||||
def test_gcd_coprime_numbers(self):
|
||||
"""Test GCD with coprime numbers (GCD should be 1)"""
|
||||
assert gcd(13, 7) == 1
|
||||
assert gcd(17, 19) == 1
|
||||
assert gcd(25, 49) == 1
|
||||
|
||||
def test_gcd_same_numbers(self):
|
||||
"""Test GCD with same numbers"""
|
||||
assert gcd(5, 5) == 5
|
||||
assert gcd(100, 100) == 100
|
||||
|
||||
def test_gcd_with_zero(self):
|
||||
"""Test GCD when one or both numbers are zero"""
|
||||
assert gcd(0, 5) == 5
|
||||
assert gcd(5, 0) == 5
|
||||
assert gcd(0, 0) == 0
|
||||
|
||||
def test_gcd_with_one(self):
|
||||
"""Test GCD when one number is 1"""
|
||||
assert gcd(1, 5) == 1
|
||||
assert gcd(100, 1) == 1
|
||||
|
||||
def test_gcd_large_numbers(self):
|
||||
"""Test GCD with large numbers"""
|
||||
assert gcd(1000000, 500000) == 500000
|
||||
assert gcd(123456, 789012) == 12
|
||||
|
||||
def test_gcd_reversed_order(self):
|
||||
"""Test GCD is commutative (order doesn't matter)"""
|
||||
assert gcd(12, 8) == gcd(8, 12)
|
||||
assert gcd(100, 35) == gcd(35, 100)
|
||||
|
||||
def test_gcd_negative_numbers(self):
|
||||
"""Test GCD with negative numbers"""
|
||||
assert gcd(-12, 8) == 4
|
||||
assert gcd(12, -8) == 4
|
||||
assert gcd(-12, -8) == 4
|
||||
|
||||
def test_gcd_multiples(self):
|
||||
"""Test GCD when one number is a multiple of the other"""
|
||||
assert gcd(10, 5) == 5
|
||||
assert gcd(100, 25) == 25
|
||||
assert gcd(7, 21) == 7
|
||||
|
||||
|
||||
class TestLcd:
|
||||
"""Test cases for the lcd (Least Common Denominator/Multiple) function"""
|
||||
|
||||
def test_lcd_basic_positive_numbers(self):
|
||||
"""Test LCD with basic positive numbers"""
|
||||
assert lcd(4, 6) == 12
|
||||
assert lcd(3, 5) == 15
|
||||
assert lcd(12, 8) == 24
|
||||
|
||||
def test_lcd_coprime_numbers(self):
|
||||
"""Test LCD with coprime numbers (should be their product)"""
|
||||
assert lcd(7, 13) == 91
|
||||
assert lcd(11, 13) == 143
|
||||
assert lcd(5, 7) == 35
|
||||
|
||||
def test_lcd_same_numbers(self):
|
||||
"""Test LCD with same numbers"""
|
||||
assert lcd(5, 5) == 5
|
||||
assert lcd(100, 100) == 100
|
||||
|
||||
def test_lcd_with_one(self):
|
||||
"""Test LCD when one number is 1"""
|
||||
assert lcd(1, 5) == 5
|
||||
assert lcd(100, 1) == 100
|
||||
|
||||
def test_lcd_with_zero(self):
|
||||
"""Test LCD when one or both numbers are zero"""
|
||||
assert lcd(0, 5) == 0
|
||||
assert lcd(5, 0) == 0
|
||||
assert lcd(0, 0) == 0
|
||||
|
||||
def test_lcd_large_numbers(self):
|
||||
"""Test LCD with large numbers"""
|
||||
assert lcd(100, 150) == 300
|
||||
assert lcd(1000, 500) == 1000
|
||||
|
||||
def test_lcd_reversed_order(self):
|
||||
"""Test LCD is commutative (order doesn't matter)"""
|
||||
assert lcd(4, 6) == lcd(6, 4)
|
||||
assert lcd(12, 18) == lcd(18, 12)
|
||||
|
||||
def test_lcd_negative_numbers(self):
|
||||
"""Test LCD with negative numbers"""
|
||||
assert lcd(-4, 6) == 12
|
||||
assert lcd(4, -6) == 12
|
||||
assert lcd(-4, -6) == 12
|
||||
|
||||
def test_lcd_multiples(self):
|
||||
"""Test LCD when one number is a multiple of the other"""
|
||||
assert lcd(5, 10) == 10
|
||||
assert lcd(3, 9) == 9
|
||||
assert lcd(25, 100) == 100
|
||||
|
||||
def test_lcd_gcd_relationship(self):
|
||||
"""Test the mathematical relationship between LCD and GCD: lcd(a,b) * gcd(a,b) = a * b"""
|
||||
test_cases = [(12, 8), (15, 10), (21, 14), (100, 35)]
|
||||
for a, b in test_cases:
|
||||
assert lcd(a, b) * gcd(a, b) == a * b
|
||||
|
||||
# __END__
|
||||
Reference in New Issue
Block a user