@@ -685,6 +685,7 @@ def msum(iterable):
685
685
([], 0.0 ),
686
686
([0.0 ], 0.0 ),
687
687
([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1.0 , - 1e50 ], 1e-100 ),
688
+ ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1 , - 1e50 ], 1e-100 ),
688
689
([2.0 ** 53 , - 0.5 , - 2.0 ** - 54 ], 2.0 ** 53 - 1.0 ),
689
690
([2.0 ** 53 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 2.0 ),
690
691
([2.0 ** 53 + 10.0 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 12.0 ),
@@ -733,9 +734,20 @@ def msum(iterable):
733
734
self .assertEqual (msum (vals ), math .fsum (vals ))
734
735
735
736
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 )
736
740
self .assertRaises (OverflowError , math .fsum , [1e+308 , 1e+308 ])
737
741
self .assertRaises (ValueError , math .fsum , [math .inf , - math .inf ])
738
742
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 ())
739
751
740
752
def testGcd (self ):
741
753
gcd = math .gcd
@@ -797,6 +809,8 @@ def testHypot(self):
797
809
# Test allowable types (those with __float__)
798
810
self .assertEqual (hypot (12.0 , 5.0 ), 13.0 )
799
811
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 ))
800
814
self .assertEqual (hypot (Decimal (12 ), Decimal (5 )), 13 )
801
815
self .assertEqual (hypot (Fraction (12 , 32 ), Fraction (5 , 32 )), Fraction (13 , 32 ))
802
816
self .assertEqual (hypot (bool (1 ), bool (0 ), bool (1 ), bool (1 )), math .sqrt (3 ))
@@ -948,6 +962,10 @@ def testDist(self):
948
962
# Test allowable types (those with __float__)
949
963
self .assertEqual (dist ((14.0 , 1.0 ), (2.0 , - 4.0 )), 13.0 )
950
964
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 )
951
969
self .assertEqual (dist ((D (14 ), D (1 )), (D (2 ), D (- 4 ))), D (13 ))
952
970
self .assertEqual (dist ((F (14 , 32 ), F (1 , 32 )), (F (2 , 32 ), F (- 4 , 32 ))),
953
971
F (13 , 32 ))
@@ -1005,6 +1023,12 @@ class T(tuple):
1005
1023
with self .assertRaises (TypeError ):
1006
1024
dist ([1 ], 2 )
1007
1025
1026
+ class BadFloat :
1027
+ __float__ = BadDescr ()
1028
+
1029
+ with self .assertRaises (ValueError ):
1030
+ dist ([1 ], [BadFloat ()])
1031
+
1008
1032
# Verify that the one dimensional case is equivalent to abs()
1009
1033
for i in range (20 ):
1010
1034
p , q = random .random (), random .random ()
@@ -1175,6 +1199,7 @@ def testLdexp(self):
1175
1199
1176
1200
def testLog (self ):
1177
1201
self .assertRaises (TypeError , math .log )
1202
+ self .assertRaises (TypeError , math .log , 1 , 2 , 3 )
1178
1203
self .ftest ('log(1/e)' , math .log (1 / math .e ), - 1 )
1179
1204
self .ftest ('log(1)' , math .log (1 ), 0 )
1180
1205
self .ftest ('log(e)' , math .log (math .e ), 1 )
@@ -1245,6 +1270,8 @@ def testSumProd(self):
1245
1270
self .assertEqual (sumprod (iter ([10 , 20 , 30 ]), (1 , 2 , 3 )), 140 )
1246
1271
self .assertEqual (sumprod ([1.5 , 2.5 ], [3.5 , 4.5 ]), 16.5 )
1247
1272
self .assertEqual (sumprod ([], []), 0 )
1273
+ self .assertEqual (sumprod ([- 1 ], [1. ]), - 1 )
1274
+ self .assertEqual (sumprod ([1. ], [- 1 ]), - 1 )
1248
1275
1249
1276
# Type preservation and coercion
1250
1277
for v in [
@@ -1270,11 +1297,20 @@ def testSumProd(self):
1270
1297
self .assertRaises (TypeError , sumprod , [], [], []) # Three args
1271
1298
self .assertRaises (TypeError , sumprod , None , [10 ]) # Non-iterable
1272
1299
self .assertRaises (TypeError , sumprod , [10 ], None ) # Non-iterable
1300
+ self .assertRaises (TypeError , sumprod , ['x' ], [1.0 ])
1273
1301
1274
1302
# Uneven lengths
1275
1303
self .assertRaises (ValueError , sumprod , [10 , 20 ], [30 ])
1276
1304
self .assertRaises (ValueError , sumprod , [10 ], [20 , 30 ])
1277
1305
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
+
1278
1314
# Error in iterator
1279
1315
def raise_after (n ):
1280
1316
for i in range (n ):
@@ -1285,6 +1321,11 @@ def raise_after(n):
1285
1321
with self .assertRaises (RuntimeError ):
1286
1322
sumprod (raise_after (5 ), range (10 ))
1287
1323
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
+
1288
1329
# Error in multiplication
1289
1330
class BadMultiply :
1290
1331
def __mul__ (self , other ):
@@ -1524,6 +1565,7 @@ def testPow(self):
1524
1565
self .assertTrue (math .isnan (math .pow (2 , NAN )))
1525
1566
self .assertTrue (math .isnan (math .pow (0 , NAN )))
1526
1567
self .assertEqual (math .pow (1 , NAN ), 1 )
1568
+ self .assertRaises (OverflowError , math .pow , 1e+100 , 1e+100 )
1527
1569
1528
1570
# pow(0., x)
1529
1571
self .assertEqual (math .pow (0. , INF ), 0. )
@@ -1880,6 +1922,8 @@ def __trunc__(self):
1880
1922
return 23
1881
1923
class TestNoTrunc :
1882
1924
pass
1925
+ class TestBadTrunc :
1926
+ __trunc__ = BadDescr ()
1883
1927
1884
1928
self .assertEqual (math .trunc (TestTrunc ()), 23 )
1885
1929
self .assertEqual (math .trunc (FloatTrunc ()), 23 )
@@ -1888,6 +1932,7 @@ class TestNoTrunc:
1888
1932
self .assertRaises (TypeError , math .trunc , 1 , 2 )
1889
1933
self .assertRaises (TypeError , math .trunc , FloatLike (23.5 ))
1890
1934
self .assertRaises (TypeError , math .trunc , TestNoTrunc ())
1935
+ self .assertRaises (ValueError , math .trunc , TestBadTrunc ())
1891
1936
1892
1937
def testIsfinite (self ):
1893
1938
self .assertTrue (math .isfinite (0.0 ))
@@ -2088,6 +2133,8 @@ def test_mtestfile(self):
2088
2133
'\n ' .join (failures ))
2089
2134
2090
2135
def test_prod (self ):
2136
+ from fractions import Fraction as F
2137
+
2091
2138
prod = math .prod
2092
2139
self .assertEqual (prod ([]), 1 )
2093
2140
self .assertEqual (prod ([], start = 5 ), 5 )
@@ -2099,6 +2146,14 @@ def test_prod(self):
2099
2146
self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]), 120.0 )
2100
2147
self .assertEqual (prod ([1 , 2 , 3 , 4.0 , 5.0 ]), 120.0 )
2101
2148
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 ()])
2102
2157
2103
2158
# Test overflow in fast-path for integers
2104
2159
self .assertEqual (prod ([1 , 1 , 2 ** 32 , 1 , 1 ]), 2 ** 32 )
0 commit comments