From 948747a9397fb06825d8257788b7941c541cb819 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 Sep 2023 19:00:11 +0300 Subject: [PATCH 1/2] gh-109546: Add more tests for formatting floats and fractions --- Lib/test/test_float.py | 8 ++++++-- Lib/test/test_fractions.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index c4ee1e08251d63..749b6d2c6adb48 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -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') diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index e112f49d2e7944..ccda2dad8359a1 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -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 + with open(format_testfile, encoding="utf-8") as testfile: + for line in testfile: + if line.startswith('--'): + continue + line = line.strip() + if not line: + continue + + 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)) + 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() From 22245e4dd490c79ecc38aea7a30d8990169234d8 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 Sep 2023 20:42:22 +0300 Subject: [PATCH 2/2] Address review comments. --- Lib/test/test_float.py | 5 +++-- Lib/test/test_fractions.py | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 749b6d2c6adb48..84270ce7dd4780 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -737,8 +737,9 @@ def test_format_testfile(self): 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) + fmt2 = fmt[1:] + self.assertEqual(format(f, fmt2), rhs) + self.assertEqual(format(-f, fmt2), '-' + rhs) def test_issue5864(self): self.assertEqual(format(123.456, '.4'), '123.5') diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index ccda2dad8359a1..4f4ea7c03f9a4c 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -7,6 +7,7 @@ import operator import fractions import functools +import os import sys import typing import unittest @@ -15,6 +16,9 @@ from pickle import dumps, loads F = fractions.Fraction +#locate file with float format test values +test_dir = os.path.dirname(__file__) or os.curdir +format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt') class DummyFloat(object): """Dummy float class for testing comparisons with Fractions""" @@ -1222,7 +1226,6 @@ def test_invalid_formats(self): @requires_IEEE_754 def test_float_format_testfile(self): - from test.test_float import format_testfile with open(format_testfile, encoding="utf-8") as testfile: for line in testfile: if line.startswith('--'): @@ -1235,14 +1238,15 @@ def test_float_format_testfile(self): fmt, arg = lhs.split() if fmt == '%r': continue + fmt2 = fmt[1:] with self.subTest(fmt=fmt, arg=arg): f = F(float(arg)) - self.assertEqual(format(f, fmt[1:]), rhs) + self.assertEqual(format(f, fmt2), rhs) if f: # skip negative zero - self.assertEqual(format(-f, fmt[1:]), '-' + rhs) + self.assertEqual(format(-f, fmt2), '-' + rhs) f = F(arg) - self.assertEqual(float(format(f, fmt[1:])), float(rhs)) - self.assertEqual(float(format(-f, fmt[1:])), float('-' + rhs)) + self.assertEqual(float(format(f, fmt2)), float(rhs)) + self.assertEqual(float(format(-f, fmt2)), float('-' + rhs)) if __name__ == '__main__':