(self)
| 583 | raise AssertionError(f"Value: 0x{value_hex}") from ex |
| 584 | |
| 585 | def test_long_fromnativebytes(self): |
| 586 | import math |
| 587 | from _testcapi import ( |
| 588 | pylong_fromnativebytes as fromnativebytes, |
| 589 | SIZE_MAX, |
| 590 | ) |
| 591 | |
| 592 | # Abbreviate sizeof(Py_ssize_t) to SZ because we use it a lot |
| 593 | SZ = int(math.ceil(math.log(SIZE_MAX + 1) / math.log(2)) / 8) |
| 594 | MAX_SSIZE = 2 ** (SZ * 8 - 1) - 1 |
| 595 | MAX_USIZE = 2 ** (SZ * 8) - 1 |
| 596 | |
| 597 | for v_be, expect_s, expect_u in [ |
| 598 | (b'\x00', 0, 0), |
| 599 | (b'\x01', 1, 1), |
| 600 | (b'\xff', -1, 255), |
| 601 | (b'\x00\xff', 255, 255), |
| 602 | (b'\xff\xff', -1, 65535), |
| 603 | ]: |
| 604 | with self.subTest(f"{expect_s}-{expect_u:X}-{len(v_be)}bytes"): |
| 605 | n = len(v_be) |
| 606 | v_le = v_be[::-1] |
| 607 | |
| 608 | self.assertEqual(expect_s, fromnativebytes(v_be, n, 0, 1), |
| 609 | f"PyLong_FromNativeBytes(buffer, {n}, <big>)") |
| 610 | self.assertEqual(expect_s, fromnativebytes(v_le, n, 1, 1), |
| 611 | f"PyLong_FromNativeBytes(buffer, {n}, <little>)") |
| 612 | self.assertEqual(expect_u, fromnativebytes(v_be, n, 0, 0), |
| 613 | f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <big>)") |
| 614 | self.assertEqual(expect_u, fromnativebytes(v_le, n, 1, 0), |
| 615 | f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <little>)") |
| 616 | |
| 617 | # Check native endian when the result would be the same either |
| 618 | # way and we can test it. |
| 619 | if v_be == v_le: |
| 620 | self.assertEqual(expect_s, fromnativebytes(v_be, n, -1, 1), |
| 621 | f"PyLong_FromNativeBytes(buffer, {n}, <native>)") |
| 622 | self.assertEqual(expect_u, fromnativebytes(v_be, n, -1, 0), |
| 623 | f"PyLong_FromUnsignedNativeBytes(buffer, {n}, <native>)") |
| 624 | |
| 625 | # Swap the unsigned request for tests and use the |
| 626 | # Py_ASNATIVEBYTES_UNSIGNED_BUFFER flag instead |
| 627 | self.assertEqual(expect_u, fromnativebytes(v_be, n, 4, 1), |
| 628 | f"PyLong_FromNativeBytes(buffer, {n}, <big|unsigned>)") |
| 629 | |
| 630 | def test_long_getsign(self): |
| 631 | # Test PyLong_GetSign() |
nothing calls this directly
no test coverage detected