Skip to content

Commit

Permalink
Merge testsuites for math.combinations
Browse files Browse the repository at this point in the history
This is the result of merging python#11018 and python#11414
  • Loading branch information
KellerFuchs committed Jan 28, 2019
1 parent b94095b commit 4a73acd
Showing 1 changed file with 5 additions and 51 deletions.
56 changes: 5 additions & 51 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ def testCombinationsValueErrors(self):
self.assertRaises(ValueError, math.combinations, 0, neg)
self.assertRaises(ValueError, math.combinations, neg, 0)

def testCombinationsOverflow(self):
"""math.combinations raises OverflowError on inputs too large for C longs."""
# minimum(k, n - k) > LLONG_MAX)
self.assertRaises(OverflowError, math.combinations, 10**400, 10**200)

def testCombinationsTypeErrors(self):
"""Test math.combinations raises TypeError on non-int inputs."""
for non_int in [-1e100, -1.0, math.pi, decimal.Decimal(5.2), "5"]:
Expand Down Expand Up @@ -1751,57 +1756,6 @@ def test_fractions(self):
self.assertAllClose(fraction_examples, rel_tol=1e-8)
self.assertAllNotClose(fraction_examples, rel_tol=1e-9)

def testComb(self):
comb = math.comb
factorial = math.factorial
# Test if factorial defintion is satisfied
for n in range(100):
for k in range(n + 1):
self.assertEqual(comb(n, k), factorial(n)
// (factorial(k) * factorial(n - k)))

# Test for Pascal's identity
for n in range(1, 100):
for k in range(1, n):
self.assertEqual(comb(n, k), comb(n - 1, k - 1) + comb(n - 1, k))

# Test corner case
for n in range(100):
self.assertEqual(comb(n, 0), 1)
self.assertEqual(comb(n, n), 1)

for n in range(1, 100):
self.assertEqual(comb(n, 1), n)
self.assertEqual(comb(n, n - 1), n)

# Test Symmetry
for n in range(100):
for k in range(n // 2):
self.assertEqual(comb(n, k), comb(n, n - k))

# Test OverflowError (For current implementation occurs when
# minimum(k, n - k) > LLONG_MAX)
self.assertRaises(OverflowError, comb, 10**400, 10**200)

# Raises TypeError if any argument is non-integer or argument count is
# not 2
self.assertRaises(TypeError, comb, 10, 1.0)
self.assertRaises(TypeError, comb, 10, "1")
self.assertRaises(TypeError, comb, "10", 1)
self.assertRaises(TypeError, comb, 10.0, 1)

self.assertRaises(TypeError, comb, 10)
self.assertRaises(TypeError, comb, 10, 1, 3)
self.assertRaises(TypeError, comb)

# Raises Value error if not 0 <= k <= n
self.assertRaises(ValueError, comb, -1, 1)
self.assertRaises(ValueError, comb, 1, -1)
self.assertRaises(ValueError, comb, 1, 2)

# ValueError instead of OverflowError if k is negative and overflowing
self.assertRaises(ValueError, comb, 10**400, -10**200)


def test_main():
from doctest import DocFileSuite
Expand Down

0 comments on commit 4a73acd

Please sign in to comment.