@@ -553,6 +553,11 @@ def testCombinationsValueErrors(self):
553553 self .assertRaises (ValueError , math .combinations , 0 , neg )
554554 self .assertRaises (ValueError , math .combinations , neg , 0 )
555555
556+ def testCombinationsOverflow (self ):
557+ """math.combinations raises OverflowError on inputs too large for C longs."""
558+ # minimum(k, n - k) > LLONG_MAX)
559+ self .assertRaises (OverflowError , math .combinations , 10 ** 400 , 10 ** 200 )
560+
556561 def testCombinationsTypeErrors (self ):
557562 """Test math.combinations raises TypeError on non-int inputs."""
558563 for non_int in [- 1e100 , - 1.0 , math .pi , decimal .Decimal (5.2 ), "5" ]:
@@ -1846,57 +1851,6 @@ def test_fractions(self):
18461851 self .assertAllClose (fraction_examples , rel_tol = 1e-8 )
18471852 self .assertAllNotClose (fraction_examples , rel_tol = 1e-9 )
18481853
1849- def testComb (self ):
1850- comb = math .comb
1851- factorial = math .factorial
1852- # Test if factorial defintion is satisfied
1853- for n in range (100 ):
1854- for k in range (n + 1 ):
1855- self .assertEqual (comb (n , k ), factorial (n )
1856- // (factorial (k ) * factorial (n - k )))
1857-
1858- # Test for Pascal's identity
1859- for n in range (1 , 100 ):
1860- for k in range (1 , n ):
1861- self .assertEqual (comb (n , k ), comb (n - 1 , k - 1 ) + comb (n - 1 , k ))
1862-
1863- # Test corner case
1864- for n in range (100 ):
1865- self .assertEqual (comb (n , 0 ), 1 )
1866- self .assertEqual (comb (n , n ), 1 )
1867-
1868- for n in range (1 , 100 ):
1869- self .assertEqual (comb (n , 1 ), n )
1870- self .assertEqual (comb (n , n - 1 ), n )
1871-
1872- # Test Symmetry
1873- for n in range (100 ):
1874- for k in range (n // 2 ):
1875- self .assertEqual (comb (n , k ), comb (n , n - k ))
1876-
1877- # Test OverflowError (For current implementation occurs when
1878- # minimum(k, n - k) > LLONG_MAX)
1879- self .assertRaises (OverflowError , comb , 10 ** 400 , 10 ** 200 )
1880-
1881- # Raises TypeError if any argument is non-integer or argument count is
1882- # not 2
1883- self .assertRaises (TypeError , comb , 10 , 1.0 )
1884- self .assertRaises (TypeError , comb , 10 , "1" )
1885- self .assertRaises (TypeError , comb , "10" , 1 )
1886- self .assertRaises (TypeError , comb , 10.0 , 1 )
1887-
1888- self .assertRaises (TypeError , comb , 10 )
1889- self .assertRaises (TypeError , comb , 10 , 1 , 3 )
1890- self .assertRaises (TypeError , comb )
1891-
1892- # Raises Value error if not 0 <= k <= n
1893- self .assertRaises (ValueError , comb , - 1 , 1 )
1894- self .assertRaises (ValueError , comb , 1 , - 1 )
1895- self .assertRaises (ValueError , comb , 1 , 2 )
1896-
1897- # ValueError instead of OverflowError if k is negative and overflowing
1898- self .assertRaises (ValueError , comb , 10 ** 400 , - 10 ** 200 )
1899-
19001854
19011855def test_main ():
19021856 from doctest import DocFileSuite
0 commit comments