mirror of
https://github.com/python/cpython.git
synced 2025-09-24 09:23:02 +00:00
gh-109546: Add more tests for formatting floats and fractions (GH-109548)
This commit is contained in:
parent
c829975428
commit
beb5ec5817
2 changed files with 35 additions and 2 deletions
|
@ -733,8 +733,13 @@ class FormatTestCase(unittest.TestCase):
|
||||||
|
|
||||||
lhs, rhs = map(str.strip, line.split('->'))
|
lhs, rhs = map(str.strip, line.split('->'))
|
||||||
fmt, arg = lhs.split()
|
fmt, arg = lhs.split()
|
||||||
self.assertEqual(fmt % float(arg), rhs)
|
f = float(arg)
|
||||||
self.assertEqual(fmt % -float(arg), '-' + rhs)
|
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):
|
def test_issue5864(self):
|
||||||
self.assertEqual(format(123.456, '.4'), '123.5')
|
self.assertEqual(format(123.456, '.4'), '123.5')
|
||||||
|
|
|
@ -7,6 +7,7 @@ import numbers
|
||||||
import operator
|
import operator
|
||||||
import fractions
|
import fractions
|
||||||
import functools
|
import functools
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
import unittest
|
import unittest
|
||||||
|
@ -15,6 +16,9 @@ import pickle
|
||||||
from pickle import dumps, loads
|
from pickle import dumps, loads
|
||||||
F = fractions.Fraction
|
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):
|
class DummyFloat(object):
|
||||||
"""Dummy float class for testing comparisons with Fractions"""
|
"""Dummy float class for testing comparisons with Fractions"""
|
||||||
|
@ -1220,6 +1224,30 @@ class FractionTest(unittest.TestCase):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
format(fraction, spec)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue