Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,10 @@ def test_long_checkexact(self):
def test_long_fromdouble(self):
# Test PyLong_FromDouble()
fromdouble = _testcapi.pylong_fromdouble
self.assertEqual(fromdouble(5.0), 5)
self.assertEqual(fromdouble(5.1), 5)
self.assertEqual(fromdouble(5.9), 5)
self.assertEqual(fromdouble(-5.1), -5)
self.assertEqual(fromdouble(-5.9), -5)
self.assertEqual(fromdouble(sys.float_info.max), int(sys.float_info.max))
self.assertEqual(fromdouble(-sys.float_info.max), -int(sys.float_info.max))
float_max = sys.float_info.max
for value in (5.0, 5.1, 5.9, -5.1, -5.9, 0.0, -0.0, float_max, -float_max):
with self.subTest(value=value):
self.assertEqual(fromdouble(value), int(value))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is now suspicious. What if int() executes exactly the same code path, PyLong_FromDouble()?

I would prefer to hardcode the expected result:

for value, expected in (
    (5.0, 5),
    ...
    (float_max, int(float_max)),
):
    ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it is the purpose. PyLong_FromDouble() works exactly as int() with float argument.

How does int() with float argument work is tested in other tests.

self.assertRaises(OverflowError, fromdouble, float('inf'))
self.assertRaises(OverflowError, fromdouble, float('-inf'))
self.assertRaises(ValueError, fromdouble, float('nan'))
Expand All @@ -100,7 +97,7 @@ def test_long_fromvoidptr(self):
self.assertIsInstance(x, int)
self.assertGreaterEqual(x, 0)
self.assertIsInstance(y, int)
self.assertGreaterEqual(y, 0)
self.assertEqual(y, 0)
self.assertNotEqual(x, y)

def test_long_fromstring(self):
Expand Down