summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_math.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-06-01 19:09:02 (GMT)
committerGitHub <noreply@github.com>2019-06-01 19:09:02 (GMT)
commit2b843ac0ae745026ce39514573c5d075137bef65 (patch)
tree8e176372e55d171590b4c798d6deaf9311cbef8c /Lib/test/test_math.py
parent9843bc110dc4241ba7cb05f3d3ef74ac6c77caf2 (diff)
downloadcpython-2b843ac0ae745026ce39514573c5d075137bef65.zip
cpython-2b843ac0ae745026ce39514573c5d075137bef65.tar.gz
cpython-2b843ac0ae745026ce39514573c5d075137bef65.tar.bz2
bpo-35431: Refactor math.comb() implementation. (GH-13725)
* Fixed some bugs. * Added support for index-likes objects. * Improved error messages. * Cleaned up and optimized the code. * Added more tests.
Diffstat (limited to 'Lib/test/test_math.py')
-rw-r--r--Lib/test/test_math.py29
1 files changed, 22 insertions, 7 deletions
diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 9da7f7c..e27092e 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -1893,9 +1893,11 @@ class IsCloseTests(unittest.TestCase):
# 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, decimal.Decimal(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, decimal.Decimal(10.0), 1)
+ self.assertRaises(TypeError, comb, "10", 1)
self.assertRaises(TypeError, comb, 10)
self.assertRaises(TypeError, comb, 10, 1, 3)
@@ -1903,15 +1905,28 @@ class IsCloseTests(unittest.TestCase):
# 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, -2**1000, 1)
self.assertRaises(ValueError, comb, 1, -1)
- self.assertRaises(ValueError, comb, 1, -10*10)
+ self.assertRaises(ValueError, comb, 1, -2**1000)
# Raises value error if k is greater than n
- self.assertRaises(ValueError, comb, 1, 10**10)
- self.assertRaises(ValueError, comb, 0, 1)
-
-
+ self.assertRaises(ValueError, comb, 1, 2)
+ self.assertRaises(ValueError, comb, 1, 2**1000)
+
+ n = 2**1000
+ self.assertEqual(comb(n, 0), 1)
+ self.assertEqual(comb(n, 1), n)
+ self.assertEqual(comb(n, 2), n * (n-1) // 2)
+ self.assertEqual(comb(n, n), 1)
+ self.assertEqual(comb(n, n-1), n)
+ self.assertEqual(comb(n, n-2), n * (n-1) // 2)
+ self.assertRaises((OverflowError, MemoryError), comb, n, n//2)
+
+ for n, k in (True, True), (True, False), (False, False):
+ self.assertEqual(comb(n, k), 1)
+ self.assertIs(type(comb(n, k)), int)
+ self.assertEqual(comb(MyIndexable(5), MyIndexable(2)), 10)
+ self.assertIs(type(comb(MyIndexable(5), MyIndexable(2))), int)
def test_main():