@@ -685,6 +685,7 @@ def msum(iterable):
685685 ([], 0.0 ),
686686 ([0.0 ], 0.0 ),
687687 ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1.0 , - 1e50 ], 1e-100 ),
688+ ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1 , - 1e50 ], 1e-100 ),
688689 ([2.0 ** 53 , - 0.5 , - 2.0 ** - 54 ], 2.0 ** 53 - 1.0 ),
689690 ([2.0 ** 53 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 2.0 ),
690691 ([2.0 ** 53 + 10.0 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 12.0 ),
@@ -733,9 +734,20 @@ def msum(iterable):
733734 self .assertEqual (msum (vals ), math .fsum (vals ))
734735
735736 self .assertEqual (math .fsum ([1.0 , math .inf ]), math .inf )
737+ self .assertTrue (math .isnan (math .fsum ([math .nan , 1.0 ])))
738+ self .assertEqual (math .fsum ([1e100 , FloatLike (1.0 ), - 1e100 , 1e-100 ,
739+ 1e50 , FloatLike (- 1.0 ), - 1e50 ]), 1e-100 )
736740 self .assertRaises (OverflowError , math .fsum , [1e+308 , 1e+308 ])
737741 self .assertRaises (ValueError , math .fsum , [math .inf , - math .inf ])
738742 self .assertRaises (TypeError , math .fsum , ['spam' ])
743+ self .assertRaises (TypeError , math .fsum , 1 )
744+ self .assertRaises (OverflowError , math .fsum , [10 ** 1000 ])
745+
746+ def bad_iter ():
747+ yield 1.0
748+ raise ZeroDivisionError
749+
750+ self .assertRaises (ZeroDivisionError , math .fsum , bad_iter ())
739751
740752 def testGcd (self ):
741753 gcd = math .gcd
@@ -797,6 +809,8 @@ def testHypot(self):
797809 # Test allowable types (those with __float__)
798810 self .assertEqual (hypot (12.0 , 5.0 ), 13.0 )
799811 self .assertEqual (hypot (12 , 5 ), 13 )
812+ self .assertEqual (hypot (1 , - 1 ), math .sqrt (2 ))
813+ self .assertEqual (hypot (1 , FloatLike (- 1. )), math .sqrt (2 ))
800814 self .assertEqual (hypot (Decimal (12 ), Decimal (5 )), 13 )
801815 self .assertEqual (hypot (Fraction (12 , 32 ), Fraction (5 , 32 )), Fraction (13 , 32 ))
802816 self .assertEqual (hypot (bool (1 ), bool (0 ), bool (1 ), bool (1 )), math .sqrt (3 ))
@@ -948,6 +962,10 @@ def testDist(self):
948962 # Test allowable types (those with __float__)
949963 self .assertEqual (dist ((14.0 , 1.0 ), (2.0 , - 4.0 )), 13.0 )
950964 self .assertEqual (dist ((14 , 1 ), (2 , - 4 )), 13 )
965+ self .assertEqual (dist ((FloatLike (14. ), 1 ), (2 , - 4 )), 13 )
966+ self .assertEqual (dist ((11 , 1 ), (FloatLike (- 1. ), - 4 )), 13 )
967+ self .assertEqual (dist ((14 , FloatLike (- 1. )), (2 , - 6 )), 13 )
968+ self .assertEqual (dist ((14 , - 1 ), (2 , - 6 )), 13 )
951969 self .assertEqual (dist ((D (14 ), D (1 )), (D (2 ), D (- 4 ))), D (13 ))
952970 self .assertEqual (dist ((F (14 , 32 ), F (1 , 32 )), (F (2 , 32 ), F (- 4 , 32 ))),
953971 F (13 , 32 ))
@@ -1005,6 +1023,12 @@ class T(tuple):
10051023 with self .assertRaises (TypeError ):
10061024 dist ([1 ], 2 )
10071025
1026+ class BadFloat :
1027+ __float__ = BadDescr ()
1028+
1029+ with self .assertRaises (ValueError ):
1030+ dist ([1 ], [BadFloat ()])
1031+
10081032 # Verify that the one dimensional case is equivalent to abs()
10091033 for i in range (20 ):
10101034 p , q = random .random (), random .random ()
@@ -1175,6 +1199,7 @@ def testLdexp(self):
11751199
11761200 def testLog (self ):
11771201 self .assertRaises (TypeError , math .log )
1202+ self .assertRaises (TypeError , math .log , 1 , 2 , 3 )
11781203 self .ftest ('log(1/e)' , math .log (1 / math .e ), - 1 )
11791204 self .ftest ('log(1)' , math .log (1 ), 0 )
11801205 self .ftest ('log(e)' , math .log (math .e ), 1 )
@@ -1245,6 +1270,8 @@ def testSumProd(self):
12451270 self .assertEqual (sumprod (iter ([10 , 20 , 30 ]), (1 , 2 , 3 )), 140 )
12461271 self .assertEqual (sumprod ([1.5 , 2.5 ], [3.5 , 4.5 ]), 16.5 )
12471272 self .assertEqual (sumprod ([], []), 0 )
1273+ self .assertEqual (sumprod ([- 1 ], [1. ]), - 1 )
1274+ self .assertEqual (sumprod ([1. ], [- 1 ]), - 1 )
12481275
12491276 # Type preservation and coercion
12501277 for v in [
@@ -1270,11 +1297,20 @@ def testSumProd(self):
12701297 self .assertRaises (TypeError , sumprod , [], [], []) # Three args
12711298 self .assertRaises (TypeError , sumprod , None , [10 ]) # Non-iterable
12721299 self .assertRaises (TypeError , sumprod , [10 ], None ) # Non-iterable
1300+ self .assertRaises (TypeError , sumprod , ['x' ], [1.0 ])
12731301
12741302 # Uneven lengths
12751303 self .assertRaises (ValueError , sumprod , [10 , 20 ], [30 ])
12761304 self .assertRaises (ValueError , sumprod , [10 ], [20 , 30 ])
12771305
1306+ # Overflows
1307+ self .assertEqual (sumprod ([10 ** 20 ], [1 ]), 10 ** 20 )
1308+ self .assertEqual (sumprod ([1 ], [10 ** 20 ]), 10 ** 20 )
1309+ self .assertEqual (sumprod ([10 ** 10 ], [10 ** 10 ]), 10 ** 20 )
1310+ self .assertEqual (sumprod ([10 ** 7 ]* 10 ** 5 , [10 ** 7 ]* 10 ** 5 ), 10 ** 19 )
1311+ self .assertRaises (OverflowError , sumprod , [10 ** 1000 ], [1.0 ])
1312+ self .assertRaises (OverflowError , sumprod , [1.0 ], [10 ** 1000 ])
1313+
12781314 # Error in iterator
12791315 def raise_after (n ):
12801316 for i in range (n ):
@@ -1285,6 +1321,11 @@ def raise_after(n):
12851321 with self .assertRaises (RuntimeError ):
12861322 sumprod (raise_after (5 ), range (10 ))
12871323
1324+ from test .test_iter import BasicIterClass
1325+
1326+ self .assertEqual (sumprod (BasicIterClass (1 ), [1 ]), 0 )
1327+ self .assertEqual (sumprod ([1 ], BasicIterClass (1 )), 0 )
1328+
12881329 # Error in multiplication
12891330 class BadMultiply :
12901331 def __mul__ (self , other ):
@@ -1524,6 +1565,7 @@ def testPow(self):
15241565 self .assertTrue (math .isnan (math .pow (2 , NAN )))
15251566 self .assertTrue (math .isnan (math .pow (0 , NAN )))
15261567 self .assertEqual (math .pow (1 , NAN ), 1 )
1568+ self .assertRaises (OverflowError , math .pow , 1e+100 , 1e+100 )
15271569
15281570 # pow(0., x)
15291571 self .assertEqual (math .pow (0. , INF ), 0. )
@@ -1880,6 +1922,8 @@ def __trunc__(self):
18801922 return 23
18811923 class TestNoTrunc :
18821924 pass
1925+ class TestBadTrunc :
1926+ __trunc__ = BadDescr ()
18831927
18841928 self .assertEqual (math .trunc (TestTrunc ()), 23 )
18851929 self .assertEqual (math .trunc (FloatTrunc ()), 23 )
@@ -1888,6 +1932,7 @@ class TestNoTrunc:
18881932 self .assertRaises (TypeError , math .trunc , 1 , 2 )
18891933 self .assertRaises (TypeError , math .trunc , FloatLike (23.5 ))
18901934 self .assertRaises (TypeError , math .trunc , TestNoTrunc ())
1935+ self .assertRaises (ValueError , math .trunc , TestBadTrunc ())
18911936
18921937 def testIsfinite (self ):
18931938 self .assertTrue (math .isfinite (0.0 ))
@@ -2088,6 +2133,8 @@ def test_mtestfile(self):
20882133 '\n ' .join (failures ))
20892134
20902135 def test_prod (self ):
2136+ from fractions import Fraction as F
2137+
20912138 prod = math .prod
20922139 self .assertEqual (prod ([]), 1 )
20932140 self .assertEqual (prod ([], start = 5 ), 5 )
@@ -2099,6 +2146,14 @@ def test_prod(self):
20992146 self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]), 120.0 )
21002147 self .assertEqual (prod ([1 , 2 , 3 , 4.0 , 5.0 ]), 120.0 )
21012148 self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4 , 5 ]), 120.0 )
2149+ self .assertEqual (prod ([1. , F (3 , 2 )]), 1.5 )
2150+
2151+ # Error in multiplication
2152+ class BadMultiply :
2153+ def __rmul__ (self , other ):
2154+ raise RuntimeError
2155+ with self .assertRaises (RuntimeError ):
2156+ prod ([10. , BadMultiply ()])
21022157
21032158 # Test overflow in fast-path for integers
21042159 self .assertEqual (prod ([1 , 1 , 2 ** 32 , 1 , 1 ]), 2 ** 32 )
0 commit comments