Skip to content

Commit ce1093b

Browse files
vstinnerskirpichev
andcommitted
Rewrite test_705836()
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
1 parent eba13d8 commit ce1093b

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

Lib/test/test_struct.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def test_p_code(self):
365365
(got,) = struct.unpack(code, got)
366366
self.assertEqual(got, expectedback)
367367

368-
def check_705836(self, format, reverse_format):
368+
def test_705836(self):
369369
# SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry
370370
# from the low-order discarded bits could propagate into the exponent
371371
# field, causing the result to be wrong by a factor of 2.
@@ -376,33 +376,42 @@ def check_705836(self, format, reverse_format):
376376
delta /= 2.0
377377
smaller = base - delta
378378
# Packing this rounds away a solid string of trailing 1 bits.
379-
packed = struct.pack(format, smaller)
380-
unpacked = struct.unpack(format, packed)[0]
379+
packed = struct.pack("<f", smaller)
380+
unpacked = struct.unpack("<f", packed)[0]
381381
# This failed at base = 2, 4, and 32, with unpacked = 1, 2, and
382382
# 16, respectively.
383383
self.assertEqual(base, unpacked)
384-
385-
bigpacked = struct.pack(reverse_format, smaller)
384+
bigpacked = struct.pack(">f", smaller)
386385
self.assertEqual(bigpacked, string_reverse(packed))
387-
unpacked = struct.unpack(reverse_format, bigpacked)[0]
386+
unpacked = struct.unpack(">f", bigpacked)[0]
388387
self.assertEqual(base, unpacked)
389388

390389
# Largest finite IEEE single.
391390
big = (1 << 24) - 1
392391
big = math.ldexp(big, 127 - 23)
393-
packed = struct.pack(format, big)
394-
unpacked = struct.unpack(format, packed)[0]
392+
packed = struct.pack(">f", big)
393+
unpacked = struct.unpack(">f", packed)[0]
395394
self.assertEqual(big, unpacked)
396395

397396
# The same, but tack on a 1 bit so it rounds up to infinity.
398397
big = (1 << 25) - 1
399398
big = math.ldexp(big, 127 - 24)
400-
self.assertRaises(OverflowError, struct.pack, format, big)
401-
402-
def test_705836(self):
403-
self.check_705836("<f", ">f")
404-
self.check_705836(">f", "<f")
405-
self.check_705836("f", "<f" if sys.byteorder == "big" else ">f")
399+
self.assertRaises(OverflowError, struct.pack, ">f", big)
400+
self.assertRaises(OverflowError, struct.pack, "<f", big)
401+
# same for native format, see gh-145633
402+
self.assertRaises(OverflowError, struct.pack, "f", big)
403+
404+
# And for half-floats
405+
big = (1 << 11) - 1
406+
big = math.ldexp(big, 15 - 10)
407+
packed = struct.pack(">e", big)
408+
unpacked = struct.unpack(">e", packed)[0]
409+
self.assertEqual(big, unpacked)
410+
big = (1 << 12) - 1
411+
big = math.ldexp(big, 15 - 11)
412+
self.assertRaises(OverflowError, struct.pack, ">e", big)
413+
self.assertRaises(OverflowError, struct.pack, "<e", big)
414+
self.assertRaises(OverflowError, struct.pack, "e", big)
406415

407416
def test_1530559(self):
408417
for code, byteorder in iter_integer_formats():
@@ -1207,20 +1216,6 @@ def test_half_float(self):
12071216
for formatcode, bits, f in format_bits_float__doubleRoundingError_list:
12081217
self.assertEqual(bits, struct.pack(formatcode, f))
12091218

1210-
def test_float_round_trip(self):
1211-
for format in ("f", "<f", ">f", "d", "<d", ">d"):
1212-
with self.subTest(format=format):
1213-
f = struct.unpack(format, struct.pack(format, 1.5))[0]
1214-
self.assertEqual(f, 1.5)
1215-
f = struct.unpack(format, struct.pack(format, NAN))[0]
1216-
self.assertTrue(math.isnan(f), f)
1217-
f = struct.unpack(format, struct.pack(format, INF))[0]
1218-
self.assertTrue(math.isinf(f), f)
1219-
self.assertEqual(math.copysign(1.0, f), 1.0)
1220-
f = struct.unpack(format, struct.pack(format, -INF))[0]
1221-
self.assertTrue(math.isinf(f), f)
1222-
self.assertEqual(math.copysign(1.0, f), -1.0)
1223-
12241219

12251220
if __name__ == '__main__':
12261221
unittest.main()

0 commit comments

Comments
 (0)