Skip to content

Commit

Permalink
further test refinements.
Browse files Browse the repository at this point in the history
  • Loading branch information
gpshead committed Feb 12, 2024
1 parent 0302840 commit 5099616
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,21 @@ def test_long_asnativebytes(self):
(-(2**256-1), 33),
]:
with self.subTest(f"sizeof-{v:X}"):
buffer = bytearray(1)
buffer = bytearray(b"\x5a")
self.assertEqual(expect, asnativebytes(v, buffer, 0, -1),
"PyLong_AsNativeBytes(v, NULL, 0, -1)")
"PyLong_AsNativeBytes(v, <unknown>, 0, -1)")
# Also check via the __index__ path
self.assertEqual(expect, asnativebytes(Index(v), buffer, 0, -1),
"PyLong_AsNativeBytes(Index(v), NULL, 0, -1)")
"PyLong_AsNativeBytes(Index(v), <unknown>, 0, -1)")

# Test that we populate n=2 bytes but do not overwrite more.
buffer = bytearray(b"\x99"*3)
self.assertEqual(2, asnativebytes(4, buffer, 2, 0), # BE
"PyLong_AsNativeBytes(v, <3 byte buffer>, 2, 0) // BE")
self.assertEqual(buffer, b"\x00\x04\x99")
self.assertEqual(2, asnativebytes(4, buffer, 2, 1), # LE
"PyLong_AsNativeBytes(v, <3 byte buffer>, 2, 1) // LE")
self.assertEqual(buffer, b"\x04\x00\x99")

# We request as many bytes as `expect_be` contains, and always check
# the result (both big and little endian). We check the return value
Expand Down Expand Up @@ -512,7 +521,7 @@ def test_long_asnativebytes(self):
n = len(expect_be)
# Fill the buffer with dummy data to ensure all bytes
# are overwritten.
buffer = bytearray(b'\xa5'*n)
buffer = bytearray(b"\xa5"*n)
expect_le = expect_be[::-1]

self.assertEqual(expect_n, asnativebytes(v, buffer, n, 0),
Expand Down

0 comments on commit 5099616

Please sign in to comment.