@@ -234,6 +234,10 @@ def __init__(self, value):
234
234
def __index__ (self ):
235
235
return self .value
236
236
237
+ class BadDescr :
238
+ def __get__ (self , obj , objtype = None ):
239
+ raise ValueError
240
+
237
241
class MathTests (unittest .TestCase ):
238
242
239
243
def ftest (self , name , got , expected , ulp_tol = 5 , abs_tol = 0.0 ):
@@ -323,6 +327,7 @@ def testAtan2(self):
323
327
self .ftest ('atan2(0, 1)' , math .atan2 (0 , 1 ), 0 )
324
328
self .ftest ('atan2(1, 1)' , math .atan2 (1 , 1 ), math .pi / 4 )
325
329
self .ftest ('atan2(1, 0)' , math .atan2 (1 , 0 ), math .pi / 2 )
330
+ self .ftest ('atan2(1, -1)' , math .atan2 (1 , - 1 ), 3 * math .pi / 4 )
326
331
327
332
# math.atan2(0, x)
328
333
self .ftest ('atan2(0., -inf)' , math .atan2 (0. , NINF ), math .pi )
@@ -416,16 +421,22 @@ def __ceil__(self):
416
421
return 42
417
422
class TestNoCeil :
418
423
pass
424
+ class TestBadCeil :
425
+ __ceil__ = BadDescr ()
419
426
self .assertEqual (math .ceil (TestCeil ()), 42 )
420
427
self .assertEqual (math .ceil (FloatCeil ()), 42 )
421
428
self .assertEqual (math .ceil (FloatLike (42.5 )), 43 )
422
429
self .assertRaises (TypeError , math .ceil , TestNoCeil ())
430
+ self .assertRaises (ValueError , math .ceil , TestBadCeil ())
423
431
424
432
t = TestNoCeil ()
425
433
t .__ceil__ = lambda * args : args
426
434
self .assertRaises (TypeError , math .ceil , t )
427
435
self .assertRaises (TypeError , math .ceil , t , 0 )
428
436
437
+ self .assertEqual (math .ceil (FloatLike (+ 1.0 )), + 1.0 )
438
+ self .assertEqual (math .ceil (FloatLike (- 1.0 )), - 1.0 )
439
+
429
440
@requires_IEEE_754
430
441
def testCopysign (self ):
431
442
self .assertEqual (math .copysign (1 , 42 ), 1.0 )
@@ -566,16 +577,22 @@ def __floor__(self):
566
577
return 42
567
578
class TestNoFloor :
568
579
pass
580
+ class TestBadFloor :
581
+ __floor__ = BadDescr ()
569
582
self .assertEqual (math .floor (TestFloor ()), 42 )
570
583
self .assertEqual (math .floor (FloatFloor ()), 42 )
571
584
self .assertEqual (math .floor (FloatLike (41.9 )), 41 )
572
585
self .assertRaises (TypeError , math .floor , TestNoFloor ())
586
+ self .assertRaises (ValueError , math .floor , TestBadFloor ())
573
587
574
588
t = TestNoFloor ()
575
589
t .__floor__ = lambda * args : args
576
590
self .assertRaises (TypeError , math .floor , t )
577
591
self .assertRaises (TypeError , math .floor , t , 0 )
578
592
593
+ self .assertEqual (math .floor (FloatLike (+ 1.0 )), + 1.0 )
594
+ self .assertEqual (math .floor (FloatLike (- 1.0 )), - 1.0 )
595
+
579
596
def testFmod (self ):
580
597
self .assertRaises (TypeError , math .fmod )
581
598
self .ftest ('fmod(10, 1)' , math .fmod (10 , 1 ), 0.0 )
@@ -597,6 +614,7 @@ def testFmod(self):
597
614
self .assertEqual (math .fmod (- 3.0 , NINF ), - 3.0 )
598
615
self .assertEqual (math .fmod (0.0 , 3.0 ), 0.0 )
599
616
self .assertEqual (math .fmod (0.0 , NINF ), 0.0 )
617
+ self .assertRaises (ValueError , math .fmod , INF , INF )
600
618
601
619
def testFrexp (self ):
602
620
self .assertRaises (TypeError , math .frexp )
@@ -666,6 +684,7 @@ def msum(iterable):
666
684
([], 0.0 ),
667
685
([0.0 ], 0.0 ),
668
686
([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1.0 , - 1e50 ], 1e-100 ),
687
+ ([1e100 , 1.0 , - 1e100 , 1e-100 , 1e50 , - 1 , - 1e50 ], 1e-100 ),
669
688
([2.0 ** 53 , - 0.5 , - 2.0 ** - 54 ], 2.0 ** 53 - 1.0 ),
670
689
([2.0 ** 53 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 2.0 ),
671
690
([2.0 ** 53 + 10.0 , 1.0 , 2.0 ** - 100 ], 2.0 ** 53 + 12.0 ),
@@ -713,6 +732,22 @@ def msum(iterable):
713
732
s = msum (vals )
714
733
self .assertEqual (msum (vals ), math .fsum (vals ))
715
734
735
+ self .assertEqual (math .fsum ([1.0 , math .inf ]), math .inf )
736
+ self .assertTrue (math .isnan (math .fsum ([math .nan , 1.0 ])))
737
+ self .assertEqual (math .fsum ([1e100 , FloatLike (1.0 ), - 1e100 , 1e-100 ,
738
+ 1e50 , FloatLike (- 1.0 ), - 1e50 ]), 1e-100 )
739
+ self .assertRaises (OverflowError , math .fsum , [1e+308 , 1e+308 ])
740
+ self .assertRaises (ValueError , math .fsum , [math .inf , - math .inf ])
741
+ self .assertRaises (TypeError , math .fsum , ['spam' ])
742
+ self .assertRaises (TypeError , math .fsum , 1 )
743
+ self .assertRaises (OverflowError , math .fsum , [10 ** 1000 ])
744
+
745
+ def bad_iter ():
746
+ yield 1.0
747
+ raise ZeroDivisionError
748
+
749
+ self .assertRaises (ZeroDivisionError , math .fsum , bad_iter ())
750
+
716
751
def testGcd (self ):
717
752
gcd = math .gcd
718
753
self .assertEqual (gcd (0 , 0 ), 0 )
@@ -773,6 +808,8 @@ def testHypot(self):
773
808
# Test allowable types (those with __float__)
774
809
self .assertEqual (hypot (12.0 , 5.0 ), 13.0 )
775
810
self .assertEqual (hypot (12 , 5 ), 13 )
811
+ self .assertEqual (hypot (1 , - 1 ), math .sqrt (2 ))
812
+ self .assertEqual (hypot (1 , FloatLike (- 1. )), math .sqrt (2 ))
776
813
self .assertEqual (hypot (Decimal (12 ), Decimal (5 )), 13 )
777
814
self .assertEqual (hypot (Fraction (12 , 32 ), Fraction (5 , 32 )), Fraction (13 , 32 ))
778
815
self .assertEqual (hypot (bool (1 ), bool (0 ), bool (1 ), bool (1 )), math .sqrt (3 ))
@@ -830,6 +867,8 @@ def testHypot(self):
830
867
scale = FLOAT_MIN / 2.0 ** exp
831
868
self .assertEqual (math .hypot (4 * scale , 3 * scale ), 5 * scale )
832
869
870
+ self .assertRaises (TypeError , math .hypot , * ([1.0 ]* 18 ), 'spam' )
871
+
833
872
@requires_IEEE_754
834
873
@unittest .skipIf (HAVE_DOUBLE_ROUNDING ,
835
874
"hypot() loses accuracy on machines with double rounding" )
@@ -922,6 +961,10 @@ def testDist(self):
922
961
# Test allowable types (those with __float__)
923
962
self .assertEqual (dist ((14.0 , 1.0 ), (2.0 , - 4.0 )), 13.0 )
924
963
self .assertEqual (dist ((14 , 1 ), (2 , - 4 )), 13 )
964
+ self .assertEqual (dist ((FloatLike (14. ), 1 ), (2 , - 4 )), 13 )
965
+ self .assertEqual (dist ((11 , 1 ), (FloatLike (- 1. ), - 4 )), 13 )
966
+ self .assertEqual (dist ((14 , FloatLike (- 1. )), (2 , - 6 )), 13 )
967
+ self .assertEqual (dist ((14 , - 1 ), (2 , - 6 )), 13 )
925
968
self .assertEqual (dist ((D (14 ), D (1 )), (D (2 ), D (- 4 ))), D (13 ))
926
969
self .assertEqual (dist ((F (14 , 32 ), F (1 , 32 )), (F (2 , 32 ), F (- 4 , 32 ))),
927
970
F (13 , 32 ))
@@ -965,13 +1008,25 @@ class T(tuple):
965
1008
dist ((1 , 2 , 3 , 4 ), (5 , 6 , 7 ))
966
1009
with self .assertRaises (ValueError ): # Check dimension agree
967
1010
dist ((1 , 2 , 3 ), (4 , 5 , 6 , 7 ))
1011
+ with self .assertRaises (TypeError ):
1012
+ dist ((1 ,)* 17 + ("spam" ,), (1 ,)* 18 )
968
1013
with self .assertRaises (TypeError ): # Rejects invalid types
969
1014
dist ("abc" , "xyz" )
970
1015
int_too_big_for_float = 10 ** (sys .float_info .max_10_exp + 5 )
971
1016
with self .assertRaises ((ValueError , OverflowError )):
972
1017
dist ((1 , int_too_big_for_float ), (2 , 3 ))
973
1018
with self .assertRaises ((ValueError , OverflowError )):
974
1019
dist ((2 , 3 ), (1 , int_too_big_for_float ))
1020
+ with self .assertRaises (TypeError ):
1021
+ dist ((1 ,), 2 )
1022
+ with self .assertRaises (TypeError ):
1023
+ dist ([1 ], 2 )
1024
+
1025
+ class BadFloat :
1026
+ __float__ = BadDescr ()
1027
+
1028
+ with self .assertRaises (ValueError ):
1029
+ dist ([1 ], [BadFloat ()])
975
1030
976
1031
# Verify that the one dimensional case is equivalent to abs()
977
1032
for i in range (20 ):
@@ -1110,6 +1165,7 @@ def test_lcm(self):
1110
1165
1111
1166
def testLdexp (self ):
1112
1167
self .assertRaises (TypeError , math .ldexp )
1168
+ self .assertRaises (TypeError , math .ldexp , 2.0 , 1.1 )
1113
1169
self .ftest ('ldexp(0,1)' , math .ldexp (0 ,1 ), 0 )
1114
1170
self .ftest ('ldexp(1,1)' , math .ldexp (1 ,1 ), 2 )
1115
1171
self .ftest ('ldexp(1,-1)' , math .ldexp (1 ,- 1 ), 0.5 )
@@ -1142,6 +1198,7 @@ def testLdexp(self):
1142
1198
1143
1199
def testLog (self ):
1144
1200
self .assertRaises (TypeError , math .log )
1201
+ self .assertRaises (TypeError , math .log , 1 , 2 , 3 )
1145
1202
self .ftest ('log(1/e)' , math .log (1 / math .e ), - 1 )
1146
1203
self .ftest ('log(1)' , math .log (1 ), 0 )
1147
1204
self .ftest ('log(e)' , math .log (math .e ), 1 )
@@ -1152,6 +1209,7
1C6A
@@ def testLog(self):
1152
1209
2302.5850929940457 )
1153
1210
self .assertRaises (ValueError , math .log , - 1.5 )
1154
1211
self .assertRaises (ValueError , math .log , - 10 ** 1000 )
1212
+ self .assertRaises (ValueError , math .log , 10 , - 10 )
1155
1213
self .assertRaises (ValueError , math .log , NINF )
1156
1214
self .assertEqual (math .log (INF ), INF )
1157
1215
self .assertTrue (math .isnan (math .log (NAN )))
@@ -1235,6 +1293,7 @@ def testPow(self):
1235
1293
self .assertTrue (math .isnan (math .pow (2 , NAN )))
1236
1294
self .assertTrue (math .isnan (math .pow (0 , NAN )))
1237
1295
self .assertEqual (math .pow (1 , NAN ), 1 )
1296
+ self .assertRaises (OverflowError , math .pow , 1e+100 , 1e+100 )
1238
1297
1239
1298
# pow(0., x)
1240
1299
self .assertEqual (math .pow (0. , INF ), 0. )
@@ -1591,6 +1650,8 @@ def __trunc__(self):
1591
1650
return 23
1592
1651
class TestNoTrunc :
1593
1652
pass
1653
+ class TestBadTrunc :
1654
+ __trunc__ = BadDescr ()
1594
1655
1595
1656
self .assertEqual (math .trunc (TestTrunc ()), 23 )
1596
1657
self .assertEqual (math .trunc (FloatTrunc ()), 23 )
@@ -1599,6 +1660,7 @@ class TestNoTrunc:
1599
1660
self .assertRaises (TypeError , math .trunc , 1 , 2 )
1600
1661
self .assertRaises (TypeError , math .trunc , FloatLike (23.5 ))
1601
1662
self .assertRaises (TypeError , math .trunc , TestNoTrunc ())
1663
+ self .assertRaises (ValueError , math .trunc , TestBadTrunc ())
1602
1664
1603
1665
def testIsfinite (self ):
1604
1666
self .assertTrue (math .isfinite (0.0 ))
@@ -1799,6 +1861,8 @@ def test_mtestfile(self):
1799
1861
'\n ' .join (failures ))
1800
1862
1801
1863
def test_prod (self ):
1864
+ from fractions import Fraction as F
1865
+
1802
1866
prod = math .prod
1803
1867
self .assertEqual (prod ([]), 1 )
1804
1868
self .assertEqual (prod ([], start = 5 ), 5 )
@@ -1810,6 +1874,14 @@ def test_prod(self):
1810
1874
self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4.0 , 5.0 ]), 120.0 )
1811
1875
self .assertEqual (prod ([1 , 2 , 3 , 4.0 , 5.0 ]), 120.0 )
1812
1876
self .assertEqual (prod ([1.0 , 2.0 , 3.0 , 4 , 5 ]), 120.0 )
1877
+ self .assertEqual (prod ([1. , F (3 , 2 )]), 1.5 )
1878
+
1879
+ # Error in multiplication
1880
+ class BadMultiply :
1881
+ def __rmul__ (self , other ):
1882
+ raise RuntimeError
1883
+ with self .assertRaises (RuntimeError ):
1884
+ prod ([10. , BadMultiply ()])
1813
1885
1814
1886
# Test overflow in fast-path for integers
1815
1887
self .assertEqual (prod ([1 , 1 , 2 ** 32 , 1 , 1 ]), 2 ** 32 )
@@ -2109,6 +2181,14 @@ def __float__(self):
2109
2181
# argument to a float.
2110
2182
self .assertFalse (getattr (y , "converted" , False ))
2111
2183
2184
+ def test_input_exceptions (self ):
2185
+ self .assertRaises (TypeError , math .exp , "spam" )
2186
+ self .assertRaises (TypeError , math .erf , "spam" )
2187
+ self .assertRaises (TypeError , math .atan2 , "spam" , 1.0 )
2188
+ self .assertRaises (TypeError , math .atan2 , 1.0 , "spam" )
2189
+ self .assertRaises (TypeError , math .atan2 , 1.0 )
2190
+ self .assertRaises (TypeError , math .atan2 , 1.0 , 2.0 , 3.0 )
2191
+
2112
2192
# Custom assertions.
2113
2193
2114
2194
def assertIsNaN (self , value ):
0 commit comments