8000 gh-109546: Add more tests for formatting floats and fractions by serhiy-storchaka · Pull Request #109548 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-109546: Add more tests for formatting floats and fractions #109548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
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
Next Next commit
gh-109546: Add more tests for formatting floats and fractions
  • Loading branch information
serhiy-storchaka committed Sep 18, 2023
commit 948747a9397fb06825d8257788b7941c541cb819
8 changes: 6 additions & 2 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,8 +733,12 @@ def test_format_testfile(self):

lhs, rhs = map(str.strip, line.split('->'))
fmt, arg = lhs.split()
self.assertEqual(fmt % float(arg), rhs)
self.assertEqual(fmt % -float(arg), '-' + rhs)
f = float(arg)
self.assertEqual(fmt % f, rhs)
self.assertEqual(fmt % -f, '-' + rhs)
if fmt != '%r':
self.assertEqual(format(f, fmt[1:]), rhs)
self.assertEqual(format(-f, fmt[1:]), '-' + rhs)

def test_issue5864(self):
self.assertEqual(format(123.456, '.4'), '123.5')
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,30 @@ def test_invalid_formats(self):
with self.assertRaises(ValueError):
format(fraction, spec)

@requires_IEEE_754
def test_float_format_testfile(self):
from test.test_float import format_testfile
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to just copy/paste the code to get the path here:

format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')

I prefer to not make tests inter-dependents. Sometimes, just importing a test has side effects.

with open(format_testfile, encoding="utf-8") as testfile:
for line in testfile:
if line.startswith('--'):
continue
line = line.strip()
if not line:
continue
Copy link
Member

Choose a reason for hiding this comment

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

I suppose that such logic is used in other files using formatfloat_testcases.txt. Is there a good place to share common code?

If we add mathdata/, can we add a helper function there which would iterate on the file and strip comments?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was going to add it in test_float, but it is simple enough, so I just copied it.


lhs, rhs = map(str.strip, line.split('->'))
fmt, arg = lhs.split()
if fmt == '%r':
continue
with self.subTest(fmt=fmt, arg=arg):
f = F(float(arg))
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a variable for fmt[1:].

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

self.assertEqual(format(f, fmt[1:]), rhs)
if f: # skip negative zero
self.assertEqual(format(-f, fmt[1:]), '-' + rhs)
f = F(arg)
self.assertEqual(float(format(f, fmt[1:])), float(rhs))
self.assertEqual(float(format(-f, fmt[1:])), float('-' + rhs))


if __name__ == '__main__':
unittest.main()
0