diff --git a/Lib/test/test_math_integer_aug.py b/Lib/test/test_math_integer_aug.py new file mode 100644 index 00000000000..2b3593b7f5a --- /dev/null +++ b/Lib/test/test_math_integer_aug.py @@ -0,0 +1,125 @@ +import math +import sys +import unittest + +import math.integer as intmath + + +class IndexLike: + + def __init__(self, value): + self.value = value + + def __index__(self): + return self.value + + +class BadIndex: + + def __index__(self): + raise RuntimeError('bad index') + + +def py_factorial(n): + result = 1 + for value in range(2, n + 1): + result *= value + return result + + +def py_perm(n, k=None): + if k is None: + k = n + if k > n: + return 0 + result = 1 + for value in range(n - k + 1, n + 1): + result *= value + return result + + +def py_comb(n, k): + if k > n: + return 0 + k = min(k, n - k) + if k < 0: + return 0 + numerator = denominator = 1 + for value in range(1, k + 1): + numerator *= n - k + value + denominator *= value + return numerator // denominator + + +class IntegerSubmoduleAugmentedTests(unittest.TestCase): + + def test_module_identity_and_alias_metadata(self): + self.assertIs(math.integer, intmath) + self.assertIs(sys.modules['math.integer'], intmath) + self.assertEqual(intmath.__name__, 'math.integer') + for name in ('comb', 'factorial', 'gcd', 'isqrt', 'lcm', 'perm'): + with self.subTest(name=name): + self.assertIs(getattr(math, name), getattr(intmath, name)) + self.assertEqual(getattr(intmath, name).__module__, 'math.integer') + + def test_gcd_lcm_variadic_and_index_edges(self): + self.assertEqual(intmath.gcd(), 0) + self.assertEqual(intmath.gcd(-6), 6) + self.assertEqual(intmath.gcd(6, 10, 15), 1) + self.assertEqual(intmath.gcd(1, IndexLike(99), IndexLike(101)), 1) + with self.assertRaises(RuntimeError): + intmath.gcd(1, BadIndex()) + + self.assertEqual(intmath.lcm(), 1) + self.assertEqual(intmath.lcm(-6), 6) + self.assertEqual(intmath.lcm(0, IndexLike(5), IndexLike(7)), 0) + self.assertEqual(intmath.lcm(21, 6, 14), 42) + with self.assertRaises(RuntimeError): + intmath.lcm(0, BadIndex()) + + def test_isqrt_factorial_boundary_paths(self): + for value in (0, 1, 2, 15, 16, 2**63 - 1, 2**64, (1 << 200) + 12345): + with self.subTest(value=value): + root = intmath.isqrt(IndexLike(value)) + self.assertLessEqual(root * root, value) + self.assertLess(value, (root + 1) * (root + 1)) + with self.assertRaises(ValueError): + intmath.isqrt(IndexLike(-1)) + with self.assertRaises(RuntimeError): + intmath.isqrt(BadIndex()) + + for value in (0, 1, 5, 20, 25, 80, 130, 300): + with self.subTest(value=value): + self.assertEqual(intmath.factorial(IndexLike(value)), py_factorial(value)) + with self.assertRaises(ValueError): + intmath.factorial(-1) + with self.assertRaises(RuntimeError): + intmath.factorial(BadIndex()) + + def test_perm_comb_small_large_and_error_paths(self): + perm_cases = [ + (5, None), (3, 5), (5, 2), (200, 3), (30, 22), + (2**70, 2), (2**70 + 9, 3), + ] + for n, k in perm_cases: + with self.subTest(kind='perm', n=n, k=k): + args = (IndexLike(n),) if k is None else (IndexLike(n), IndexLike(k)) + self.assertEqual(intmath.perm(*args), py_perm(n, k)) + with self.assertRaises(ValueError): + intmath.perm(5, -1) + with self.assertRaises(RuntimeError): + intmath.perm(IndexLike(5), BadIndex()) + + comb_cases = [ + (3, 5), (5, 2), (200, 3), (80, 40), + (2**70, 2), (2**70 + 9, 3), + ] + for n, k in comb_cases: + with self.subTest(kind='comb', n=n, k=k): + self.assertEqual(intmath.comb(IndexLike(n), IndexLike(k)), py_comb(n, k)) + with self.assertRaises(ValueError): + intmath.comb(5, -1) + with self.assertRaises(OverflowError): + intmath.comb(2**70, 2**69) + with self.assertRaises(RuntimeError): + intmath.comb(IndexLike(5), BadIndex()) diff --git a/Lib/test/test_math_integer_p2p_aug.py b/Lib/test/test_math_integer_p2p_aug.py new file mode 100644 index 00000000000..9927bb13c55 --- /dev/null +++ b/Lib/test/test_math_integer_p2p_aug.py @@ -0,0 +1,21 @@ +import statistics +import unittest +from fractions import Fraction + + +class NumericBackwardsCompatibilityAugmentedTests(unittest.TestCase): + + def test_fraction_and_statistics_unmodified(self): + value = Fraction(355, 113) + self.assertEqual(value.limit_denominator(100), Fraction(311, 99)) + self.assertTrue(Fraction(12, 4).is_integer()) + data = [Fraction(1, 3), Fraction(2, 3), Fraction(4, 3)] + self.assertEqual(statistics.mean(data), Fraction(7, 9)) + self.assertEqual(statistics.median(data), Fraction(2, 3)) + + def test_complex_arithmetic_unmodified(self): + z = complex(3, 4) + self.assertEqual(z.conjugate(), complex(3, -4)) + self.assertEqual(z * complex(0, 1), complex(-4, 3)) + with self.assertRaises(ZeroDivisionError): + z / 0