Skip to content

Commit 1d7449c

Browse files
Add tests for types that have both __float__ and __index__.
1 parent 48f3e27 commit 1d7449c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Lib/test/test_math.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,22 @@ def __init__(self, value):
235235
def __index__(self):
236236
return self.value
237237

238+
class IndexableFloatLike:
239+
def __init__(self, float_value, index_value):
240+
self.float_value = float_value
241+
self.index_value = index_value
242+
243+
def __float__(self):
244+
if isinstance(self.float_value, BaseException):
245+
raise self.float_value
246+
return self.float_value
247+
248+
def __index__(self):
249+
if isinstance(self.index_value, BaseException):
250+
raise self.index_value
251+
return self.index_value
252+
253+
238254
class BadDescr:
239255
def __get__(self, obj, objtype=None):
240256
raise ValueError
@@ -1230,6 +1246,10 @@ def testLog(self):
12301246
self.assertEqual(math.log(INF), INF)
12311247
self.assertTrue(math.isnan(math.log(NAN)))
12321248

1249+
self.assertEqual(math.log(IndexableFloatLike(math.e, 10**1000)), 1.0)
1250+
self.assertAlmostEqual(math.log(IndexableFloatLike(OverflowError(), 10**1000)),
1251+
2302.5850929940457)
1252+
12331253
def testLog1p(self):
12341254
self.assertRaises(TypeError, math.log1p)
12351255
for n in [2, 2**90, 2**300]:
@@ -1264,6 +1284,9 @@ def testLog2(self):
12641284
self.assertRaises(ValueError, math.log2, NINF)
12651285
self.assertTrue(math.isnan(math.log2(NAN)))
12661286

1287+
self.assertEqual(math.log2(IndexableFloatLike(8.0, 2**2000)), 3.0)
1288+
self.assertEqual(math.log2(IndexableFloatLike(OverflowError(), 2**2000)), 2000.0)
1289+
12671290
@requires_IEEE_754
12681291
# log2() is not accurate enough on Mac OS X Tiger (10.4)
12691292
@support.requires_mac_ver(10, 5)
@@ -1294,6 +1317,9 @@ def testLog10(self):
12941317
self.assertEqual(math.log(INF), INF)
12951318
self.assertTrue(math.isnan(math.log10(NAN)))
12961319

1320+
self.assertEqual(math.log10(IndexableFloatLike(100.0, 10**1000)), 2.0)
1321+
self.assertEqual(math.log10(IndexableFloatLike(OverflowError(), 10**1000)), 1000.0)
1322+
12971323
def testSumProd(self):
12981324
sumprod = math.sumprod
12991325
Decimal = decimal.Decimal

0 commit comments

Comments
 (0)