Skip to content

Commit

Permalink
Add tests for types that have both __float__ and __index__.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Jul 13, 2024
1 parent 48f3e27 commit 1d7449c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ def __init__(self, value):
def __index__(self):
return self.value

class IndexableFloatLike:
def __init__(self, float_value, index_value):
self.float_value = float_value
self.index_value = index_value

def __float__(self):
if isinstance(self.float_value, BaseException):
raise self.float_value
return self.float_value

def __index__(self):
if isinstance(self.index_value, BaseException):
raise self.index_value
return self.index_value


class BadDescr:
def __get__(self, obj, objtype=None):
raise ValueError
Expand Down Expand Up @@ -1230,6 +1246,10 @@ def testLog(self):
self.assertEqual(math.log(INF), INF)
self.assertTrue(math.isnan(math.log(NAN)))

self.assertEqual(math.log(IndexableFloatLike(math.e, 10**1000)), 1.0)
self.assertAlmostEqual(math.log(IndexableFloatLike(OverflowError(), 10**1000)),
2302.5850929940457)

def testLog1p(self):
self.assertRaises(TypeError, math.log1p)
for n in [2, 2**90, 2**300]:
Expand Down Expand Up @@ -1264,6 +1284,9 @@ def testLog2(self):
self.assertRaises(ValueError, math.log2, NINF)
self.assertTrue(math.isnan(math.log2(NAN)))

self.assertEqual(math.log2(IndexableFloatLike(8.0, 2**2000)), 3.0)
self.assertEqual(math.log2(IndexableFloatLike(OverflowError(), 2**2000)), 2000.0)

@requires_IEEE_754
# log2() is not accurate enough on Mac OS X Tiger (10.4)
@support.requires_mac_ver(10, 5)
Expand Down Expand Up @@ -1294,6 +1317,9 @@ def testLog10(self):
self.assertEqual(math.log(INF), INF)
self.assertTrue(math.isnan(math.log10(NAN)))

self.assertEqual(math.log10(IndexableFloatLike(100.0, 10**1000)), 2.0)
self.assertEqual(math.log10(IndexableFloatLike(OverflowError(), 10**1000)), 1000.0)

def testSumProd(self):
sumprod = math.sumprod
Decimal = decimal.Decimal
Expand Down

0 comments on commit 1d7449c

Please sign in to comment.