@@ -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):
714715s = msum (vals )
715716self .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+
717734def testGcd (self ):
718735gcd = math .gcd
719736self .assertEqual (gcd (0 ,0 ),0 )
@@ -774,6 +791,8 @@ def testHypot(self):
774791# Test allowable types (those with __float__)
775792self .assertEqual (hypot (12.0 ,5.0 ),13.0 )
776793self .assertEqual (hypot (12 ,5 ),13 )
794+ self .assertEqual (hypot (1 ,- 1 ),math .sqrt (2 ))
795+ self .assertEqual (hypot (1 ,FloatLike (- 1. )),math .sqrt (2 ))
777796self .assertEqual (hypot (Decimal (12 ),Decimal (5 )),13 )
778797self .assertEqual (hypot (Fraction (12 ,32 ),Fraction (5 ,32 )),Fraction (13 ,32 ))
779798self .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__)
924943self .assertEqual (dist ((14.0 ,1.0 ), (2.0 ,- 4.0 )),13.0 )
925944self .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 )
926949self .assertEqual (dist ((D (14 ),D (1 )), (D (2 ),D (- 4 ))),D (13 ))
927950self .assertEqual (dist ((F (14 ,32 ),F (1 ,32 )), (F (2 ,32 ),F (- 4 ,32 ))),
928951F (13 ,32 ))
@@ -974,6 +997,12 @@ class T(tuple):
974997with self .assertRaises ((ValueError ,OverflowError )):
975998dist ((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()
9781007for i in range (20 ):
9791008p ,q = random .random (),random .random ()
@@ -1143,6 +1172,7 @@ def testLdexp(self):
11431172
11441173def testLog (self ):
11451174self .assertRaises (TypeError ,math .log )
1175+ self .assertRaises (TypeError ,math .log ,1 ,2 ,3 )
11461176self .ftest ('log(1/e)' ,math .log (1 / math .e ),- 1 )
11471177self .ftest ('log(1)' ,math .log (1 ),0 )
11481178self .ftest ('log(e)' ,math .log (math .e ),1 )
@@ -1212,6 +1242,8 @@ def testSumProd(self):
12121242self .assertEqual (sumprod (iter ([10 ,20 ,30 ]), (1 ,2 ,3 )),140 )
12131243self .assertEqual (sumprod ([1.5 ,2.5 ], [3.5 ,4.5 ]),16.5 )
12141244self .assertEqual (sumprod ([], []),0 )
1245+ self .assertEqual (sumprod ([- 1 ], [1. ]),- 1 )
1246+ self .assertEqual (sumprod ([1. ], [- 1 ]),- 1 )
12151247
12161248# Type preservation and coercion
12171249for v in [
@@ -1237,11 +1269,20 @@ def testSumProd(self):
12371269self .assertRaises (TypeError ,sumprod , [], [], [])# Three args
12381270self .assertRaises (TypeError ,sumprod ,None , [10 ])# Non-iterable
12391271self .assertRaises (TypeError ,sumprod , [10 ],None )# Non-iterable
1272+ self .assertRaises (TypeError ,sumprod , ['x' ], [1.0 ])
12401273
12411274# Uneven lengths
12421275self .assertRaises (ValueError ,sumprod , [10 ,20 ], [30 ])
12431276self .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
12461287def raise_after (n ):
12471288for i in range (n ):
@@ -1252,6 +1293,11 @@ def raise_after(n):
12521293with self .assertRaises (RuntimeError ):
12531294sumprod (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
12561302class BadMultiply :
12571303def __mul__ (self ,other ):
@@ -1491,6 +1537,7 @@ def testPow(self):
14911537self .assertTrue (math .isnan (math .pow (2 ,NAN )))
14921538self .assertTrue (math .isnan (math .pow (0 ,NAN )))
14931539self .assertEqual (math .pow (1 ,NAN ),1 )
1540+ self .assertRaises (OverflowError ,math .pow ,1e+100 ,1e+100 )
14941541
14951542# pow(0., x)
14961543self .assertEqual (math .pow (0. ,INF ),0. )
@@ -1847,6 +1894,8 @@ def __trunc__(self):
18471894return 23
18481895class TestNoTrunc :
18491896pass
1897+ class TestBadTrunc :
1898+ __trunc__ = BadDescr ()
18501899
18511900self .assertEqual (math .trunc (TestTrunc ()),23 )
18521901self .assertEqual (math .trunc (FloatTrunc ()),23 )
@@ -1855,6 +1904,7 @@ class TestNoTrunc:
18551904self .assertRaises (TypeError ,math .trunc ,1 ,2 )
18561905self .assertRaises (TypeError ,math .trunc ,FloatLike (23.5 ))
18571906self .assertRaises (TypeError ,math .trunc ,TestNoTrunc ())
1907+ self .assertRaises (ValueError ,math .trunc ,TestBadTrunc ())
18581908
18591909def testIsfinite (self ):
18601910self .assertTrue (math .isfinite (0.0 ))
@@ -2055,6 +2105,8 @@ def test_mtestfile(self):
20552105'\n ' .join (failures ))
20562106
20572107def test_prod (self ):
2108+ from fractions import Fraction as F
2109+
20582110prod = math .prod
20592111self .assertEqual (prod ([]),1 )
20602112self .assertEqual (prod ([],start = 5 ),5 )
@@ -2066,6 +2118,14 @@ def test_prod(self):
20662118self .assertEqual (prod ([1.0 ,2.0 ,3.0 ,4.0 ,5.0 ]),120.0 )
20672119self .assertEqual (prod ([1 ,2 ,3 ,4.0 ,5.0 ]),120.0 )
20682120self .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
20712131self .assertEqual (prod ([1 ,1 ,2 ** 32 ,1 ,1 ]),2 ** 32 )