]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-109546: Add more tests for formatting floats and fractions (GH-109548)...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 2 Oct 2023 14:59:26 +0000 (07:59 -0700)
committerGitHub <noreply@github.com>
Mon, 2 Oct 2023 14:59:26 +0000 (16:59 +0200)
gh-109546: Add more tests for formatting floats and fractions (GH-109548)
(cherry picked from commit beb5ec5817b645562ebbdd59f25683a93061c32c)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_float.py
Lib/test/test_fractions.py

index c4ee1e08251d6367a45e2ee40b2928f2ff7fdf06..84270ce7dd4780338e63f9f012620c1c994f7023 100644 (file)
@@ -733,8 +733,13 @@ class FormatTestCase(unittest.TestCase):
 
                 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':
+                    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')
index e112f49d2e79440710286bdc342c4ea09614b411..4f4ea7c03f9a4cb50dbb4097cbe9ad341808c659 100644 (file)
@@ -7,6 +7,7 @@ import numbers
 import operator
 import fractions
 import functools
+import os
 import sys
 import typing
 import unittest
@@ -15,6 +16,9 @@ import pickle
 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"""
@@ -1220,6 +1224,30 @@ class FractionTest(unittest.TestCase):
                 with self.assertRaises(ValueError):
                     format(fraction, spec)
 
+    @requires_IEEE_754
+    def test_float_format_testfile(self):
+        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
+                fmt2 = fmt[1:]
+                with self.subTest(fmt=fmt, arg=arg):
+                    f = F(float(arg))
+                    self.assertEqual(format(f, fmt2), rhs)
+                    if f:  # skip negative zero
+                        self.assertEqual(format(-f, fmt2), '-' + rhs)
+                    f = F(arg)
+                    self.assertEqual(float(format(f, fmt2)), float(rhs))
+                    self.assertEqual(float(format(-f, fmt2)), float('-' + rhs))
+
 
 if __name__ == '__main__':
     unittest.main()