|
7 | 7 | import operator
|
8 | 8 | import fractions
|
9 | 9 | import functools
|
| 10 | +import os |
10 | 11 | import sys
|
11 | 12 | import typing
|
12 | 13 | import unittest
|
|
15 | 16 | from pickle import dumps, loads
|
16 | 17 | F = fractions.Fraction
|
17 | 18 |
|
| 19 | +#locate file with float format test values |
| 20 | +test_dir = os.path.dirname(__file__) or os.curdir |
| 21 | +format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt') |
18 | 22 |
|
19 | 23 | class DummyFloat(object):
|
20 | 24 | """Dummy float class for testing comparisons with Fractions"""
|
@@ -1220,6 +1224,30 @@ def test_invalid_formats(self):
|
1220 | 1224 | with self.assertRaises(ValueError):
|
1221 | 1225 | format(fraction, spec)
|
1222 | 1226 |
|
| 1227 | + @requires_IEEE_754 |
| 1228 | + def test_float_format_testfile(self): |
| 1229 | + with open(format_testfile, encoding="utf-8") as testfile: |
| 1230 | + for line in testfile: |
| 1231 | + if line.startswith('--'): |
| 1232 | + continue |
| 1233 | + line = line.strip() |
| 1234 | + if not line: |
| 1235 | + continue |
| 1236 | + |
| 1237 | + lhs, rhs = map(str.strip, line.split('->')) |
| 1238 | + fmt, arg = lhs.split() |
| 1239 | + if fmt == '%r': |
| 1240 | + continue |
| 1241 | + fmt2 = fmt[1:] |
| 1242 | + with self.subTest(fmt=fmt, arg=arg): |
| 1243 | + f = F(float(arg)) |
| 1244 | + self.assertEqual(format(f, fmt2), rhs) |
| 1245 | + if f: # skip negative zero |
| 1246 | + self.assertEqual(format(-f, fmt2), '-' + rhs) |
| 1247 | + f = F(arg) |
| 1248 | + self.assertEqual(float(format(f, fmt2)), float(rhs)) |
| 1249 | + self.assertEqual(float(format(-f, fmt2)), float('-' + rhs)) |
| 1250 | + |
1223 | 1251 |
|
1224 | 1252 | if __name__ == '__main__':
|
1225 | 1253 | unittest.main()
|
0 commit comments