Skip to content

Commit 5b69f64

Browse files
committed
test(longobject): add INT64_MIN boundary tests for _PyLong_TryAsInt64Exact
Verify that _PyLong_TryAsInt64Exact correctly handles INT64_MIN (abs_val == INT64_MAX + 1 with negative sign), INT64_MAX, and that values outside the int64 range gracefully fall back to the slow path.
1 parent 47f21dd commit 5b69f64

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lib/test/test_long.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,30 @@ def test_small_ints(self):
11401140
self.assertIs(i - i, 0)
11411141
self.assertIs(0 * i, 0)
11421142

1143+
@support.cpython_only
1144+
def test_int64_boundary_add(self):
1145+
# Verify _PyLong_TryAsInt64Exact handles INT64 extremes correctly.
1146+
INT64_MAX = (1 << 63) - 1
1147+
INT64_MIN = -(1 << 63)
1148+
1149+
# INT64_MAX: addition that stays within range
1150+
self.assertEqual(INT64_MAX + 0, INT64_MAX)
1151+
self.assertEqual(INT64_MAX + (-1), INT64_MAX - 1)
1152+
1153+
# INT64_MIN: extraction must succeed and arithmetic must be correct
1154+
self.assertEqual(INT64_MIN + 0, INT64_MIN)
1155+
self.assertEqual(INT64_MIN + 1, INT64_MIN + 1)
1156+
1157+
# Adding two values that overflow int64 falls back to slow path
1158+
self.assertEqual(INT64_MAX + 1, 1 << 63)
1159+
self.assertEqual(INT64_MIN + (-1), INT64_MIN - 1)
1160+
1161+
# Values just outside int64 range are not handled by the fast path
1162+
beyond_max = INT64_MAX + 2
1163+
beyond_min = INT64_MIN - 2
1164+
self.assertEqual(beyond_max + 1, INT64_MAX + 3)
1165+
self.assertEqual(beyond_min + (-1), INT64_MIN - 3)
1166+
11431167
def test_bit_length(self):
11441168
tiny = 1e-10
11451169
for x in range(-65000, 65000):

0 commit comments

Comments
 (0)