summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorYash Aggarwal <Aggarwal.yash2011@gmail.com>2019-06-01 07:21:27 (GMT)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>2019-06-01 07:21:27 (GMT)
commit4a686504eb2bbf69adf78077458508a7ba131667 (patch)
tree1fed04a5328f196e158ee5e22f3cf62044d9756c /Lib
parent5ac0b988fd5f1428efe35329c531c7b5c74d37f6 (diff)
downloadcpython-4a686504eb2bbf69adf78077458508a7ba131667.zip
cpython-4a686504eb2bbf69adf78077458508a7ba131667.tar.gz
cpython-4a686504eb2bbf69adf78077458508a7ba131667.tar.bz2
bpo-35431: Implemented math.comb (GH-11414)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_math.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 853a0e6..9da7f7c 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -1862,6 +1862,57 @@ class IsCloseTests(unittest.TestCase):
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 cases
+ 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))
+
+ # 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 k or n are negative numbers
+ self.assertRaises(ValueError, comb, -1, 1)
+ self.assertRaises(ValueError, comb, -10*10, 1)
+ self.assertRaises(ValueError, comb, 1, -1)
+ self.assertRaises(ValueError, comb, 1, -10*10)
+
+ # Raises value error if k is greater than n
+ self.assertRaises(ValueError, comb, 1, 10**10)
+ self.assertRaises(ValueError, comb, 0, 1)
+
+
+
def test_main():
from doctest import DocFileSuite