@@ -667,6 +667,7 @@ def msum(iterable):
667667 ([], 0.0 ),
668668 ([0.0 ], 0.0 ),
669669 ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1.0 , - 1e50 ], 1e-100 ),
670+ ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1 , - 1e50 ], 1e-100 ),
670671 ([2.0 ** 53 , - 0.5 , - 2.0 ** - 54 ], 2.0 ** 53 - 1.0 ),
671672 ([2.0 ** 53 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 2.0 ),
672673 ([2.0 ** 53 + 10.0 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 12.0 ),
@@ -714,6 +715,22 @@ def msum(iterable):
714715 s = msum (vals )
715716 self .assertEqual (msum (vals ), math .fsum (vals ))
716717
718+ self .assertEqual (math .fsum ([1.0 , math .inf ]), math .inf )
719+ self .assertTrue (math .isnan (math .fsum ([math .nan , 1.0 ])))
720+ self .assertEqual (math .fsum ([1e100 , FloatLike (1.0 ), - 1e100 , 1e-100 ,
721+ 1e50 , FloatLike (- 1.0 ), - 1e50 ]), 1e-100 )
722+ self .assertRaises (OverflowError , math .fsum , [1e+308 , 1e+308 ])
723+ self .assertRaises (ValueError , math .fsum , [math .inf , - math .inf ])
724+ self .assertRaises (TypeError , math .fsum , ['spam' ])
725+ self .assertRaises (TypeError , math .fsum , 1 )
726+ self .assertRaises (OverflowError , math .fsum , [10 ** 1000 ])
727+
728+ def bad_iter ():
729+ yield 1.0
730+ raise ZeroDivisionError
731+
732+ self .assertRaises (ZeroDivisionError , math .fsum , bad_iter ())
733+
717734 def testGcd (self ):
718735 gcd = math .gcd
719736 self .assertEqual (gcd (0 , 0 ), 0 )
@@ -774,6 +791,8 @@ def testHypot(self):
774791 # Test allowable types (those with __float__)
775792 self .assertEqual (hypot (12.0 , 5.0 ), 13.0 )
776793 self .assertEqual (hypot (12 , 5 ), 13 )
794+ self .assertEqual (hypot (1 , - 1 ), math .sqrt (2 ))
795+ self .assertEqual (hypot (1 , FloatLike (- 1. )), math .sqrt (2 ))
777796 self .assertEqual (hypot (Decimal (12 ), Decimal (5 )), 13 )
778797 self .assertEqual (hypot (Fraction (12 , 32 ), Fraction (5 , 32 )), Fraction (13 , 32 ))
779798 self .assertEqual (hypot (bool (1 ), bool (0 ), bool (1 ), bool (1 )), math .sqrt (3 ))
@@ -923,6 +942,10 @@ def testDist(self):
923942 # Test allowable types (those with __float__)
924943 self .assertEqual (dist ((14.0 , 1.0 ), (2.0 , - 4.0 )), 13.0 )
925944 self .assertEqual (dist ((14 , 1 ), (2 , - 4 )), 13 )
945+ self .assertEqual (dist ((FloatLike (14. ), 1 ), (2 , - 4 )), 13 )
946+ self .assertEqual (dist ((11 , 1 ), (FloatLike (- 1. ), - 4 )), 13 )
947+ self .assertEqual (dist ((14 , FloatLike (- 1. )), (2 , - 6 )), 13 )
948+ self .assertEqual (dist ((14 , - 1 ), (2 , - 6 )), 13 )
926949 self .assertEqual (dist ((D (14 ), D (1 )), (D (2 ), D (- 4 ))), D (13 ))
927950 self .assertEqual (dist ((F (14 , 32 ), F (1 , 32 )), (F (2 , 32 ), F (- 4 , 32 ))),
928951 F (13 , 32 ))
@@ -974,6 +997,12 @@ class T(tuple):
974997 with self .assertRaises ((ValueError , OverflowError )):
975998 dist ((2 , 3 ), (1 , int_too_big_for_float ))
976999
1000+ class BadFloat :
1001+ __float__ = BadDescr ()
1002+
1003+ with self .assertRaises (ValueError ):
1004+ dist ([1 ], [BadFloat ()])
1005+
9771006 # Verify that the one dimensional case is equivalent to abs()
9781007 for i in range (20 ):
9791008 p , q = random .random (), random .random ()
@@ -1143,6 +1172,7 @@ def testLdexp(self):
11431172
11441173 def testLog (self ):
11451174 self .assertRaises (TypeError , math .log )
1175+ self .assertRaises (TypeError , math .log , 1 , 2 , 3 )
11461176 self .ftest ('log(1/e)' , math .log (1 / math .e ), - 1 )
11471177 self .ftest ('log(1)' , math .log (1 ), 0 )
11481178 self .ftest ('log(e)' , math .log (math .e ), 1 )
@@ -1212,6 +1242,8 @@ def testSumProd(self):
12121242 self .assertEqual (sumprod (iter ([10 , 20 , 30 ]), (1 , 2 , 3 )), 140 )
12131243 self .assertEqual (sumprod ([1.5 , 2.5 ], [3.5 , 4.5 ]), 16.5 )
12141244 self .assertEqual (sumprod ([], []), 0 )
1245+ self .assertEqual (sumprod ([- 1 ], [1. ]), - 1 )
1246+ self .assertEqual (sumprod ([1. ], [- 1 ]), - 1 )
12151247
12161248 # Type preservation and coercion
12171249 for v in [
@@ -1237,11 +1269,20 @@ def testSumProd(self):
12371269 self .assertRaises (TypeError , sumprod , [], [], []) # Three args
12381270 self .assertRaises (TypeError , sumprod , None , [10 ]) # Non-iterable
12391271 self .assertRaises (TypeError , sumprod , [10 ], None ) # Non-iterable
1272+ self .assertRaises (TypeError , sumprod , ['x' ], [1.0 ])
12401273
12411274 # Uneven lengths
12421275 self .assertRaises (ValueError , sumprod , [10 , 20 ], [30 ])
12431276 self .assertRaises (ValueError , sumprod , [10 ], [20 , 30 ])
12441277
1278+ # Overflows
1279+ self .assertEqual (sumprod ([10 ** 20 ], [1 ]), 10 ** 20 )
1280+ self .assertEqual (sumprod ([1 ], [10 ** 20 ]), 10 ** 20 )
1281+ self .assertEqual (sumprod ([10 ** 10 ], [10 ** 10 ]), 10 ** 20 )
1282+ self .assertEqual (sumprod ([10 ** 7 ]* 10 ** 5 , [10 ** 7 ]* 10 ** 5 ), 10 ** 19 )
1283+ self .assertRaises (OverflowError , sumprod , [10 ** 1000 ], [1.0 ])
1284+ self .assertRaises (OverflowError , sumprod , [1.0 ], [10 ** 1000 ])
1285+
12451286 # Error in iterator
12461287 def raise_after (n ):
12471288 for i in range (n ):
@@ -1252,6 +1293,11 @@ def raise_after(n):
12521293 with self .assertRaises (RuntimeError ):
12531294 sumprod (raise_after (5 ), range (10 ))
12541295
1296+ from test .test_iter import BasicIterClass
1297+
1298+ self .assertEqual (sumprod (BasicIterClass (1 ), [1 ]), 0 )
1299+ self .assertEqual (sumprod ([1 ], BasicIterClass (1 )), 0 )
1300+
12551301 # Error in multiplication
12561302 class BadMultiply :
12571303 def __mul__ (self , other ):
@@ -1491,6 +1537,7 @@ def testPow(self):
14911537 self .assertTrue (math .isnan (math .pow (2 , NAN )))
14921538 self .assertTrue (math .isnan (math .pow (0 , NAN )))
14931539 self .assertEqual (math .pow (1 , NAN ), 1 )
1540+ self .assertRaises (OverflowError , math .pow , 1e+100 , 1e+100 )
14941541
14951542 # pow(0., x)
14961543 self .assertEqual (math .pow (0. , INF ), 0. )
@@ -1847,6 +1894,8 @@ def __trunc__(self):
18471894 return 23
18481895 class TestNoTrunc :
18491896 pass
1897+ class TestBadTrunc :
1898+ __trunc__ = BadDescr ()
18501899
18511900 self .assertEqual (math .trunc (TestTrunc ()), 23 )
18521901 self .assertEqual (math .trunc (FloatTrunc ()), 23 )
@@ -1855,6 +1904,7 @@ class TestNoTrunc:
18551904 self .assertRaises (TypeError , math .trunc , 1 , 2 )
18561905 self .assertRaises (TypeError , math .trunc , FloatLike (23.5 ))
18571906 self .assertRaises (TypeError , math .trunc , TestNoTrunc ())
1907+ self .assertRaises (ValueError , math .trunc , TestBadTrunc ())
18581908
18591909 def testIsfinite (self ):
18601910 self .assertTrue (math .isfinite (0.0 ))
@@ -2055,6 +2105,8 @@ def test_mtestfile(self):
20552105 '\n ' .join (failures ))
20562106
20572107 def test_prod (self ):
2108+ from fractions import Fraction as F
2109+
20582110 prod = math .prod
20592111 self .assertEqual (prod ([]), 1 )
20602112 self .assertEqual (prod ([], start = 5 ), 5 )
@@ -2066,6 +2118,14 @@ def test_prod(self):
20662118 self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]), 120.0 )
20672119 self .assertEqual (prod ([1 , 2 , 3 , 4.0 , 5.0 ]), 120.0 )
20682120 self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4 , 5 ]), 120.0 )
2121+ self .assertEqual (prod ([1. , F (3 , 2 )]), 1.5 )
2122+
2123+ # Error in multiplication
2124+ class BadMultiply :
2125+ def __rmul__ (self , other ):
2126+ raise RuntimeError
2127+ with self .assertRaises (RuntimeError ):
2128+ prod ([10. , BadMultiply ()])
20692129
20702130 # Test overflow in fast-path for integers
20712131 self .assertEqual (prod ([1 , 1 , 2 ** 32 , 1 , 1 ]), 2 ** 32 )
0 commit comments