Update test_long.py and impl "is_integer" for int

This commit is contained in:
ShaharNaveh committed 2025-08-23 21:43:35 +03:00
1 parent c0b3cc9048
commit 933db1075f
2 files changed
+55 -8

No files matched your search

+50 -6
View File
@@ -412,8 +412,7 @@ class LongTest(unittest.TestCase):
"Got {}, expected {}.".format(n, actual, expected))
self.assertEqual(actual, expected, msg)
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
@support.requires_IEEE_754
def test_float_conversion(self):
@@ -824,8 +823,7 @@ class LongTest(unittest.TestCase):
self.assertEqual(expected, got, "Incorrectly rounded division {}/{}: "
"expected {}, got {}".format(a, b, expected, got))
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
@support.requires_IEEE_754
def test_correctly_rounded_true_division(self):
# more stringent tests than those above, checking that the
@@ -1344,8 +1342,7 @@ class LongTest(unittest.TestCase):
self.assertEqual((0).to_bytes(1, SubStr('big')), b'\x00')
self.assertEqual((0).to_bytes(0, SubStr('little')), b'')
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_from_bytes(self):
def check(tests, byteorder, signed=False):
def equivalent_python(byte_array, byteorder, signed=False):
@@ -1559,6 +1556,11 @@ class LongTest(unittest.TestCase):
b = i.to_bytes(2, signed=True)
self.assertIs(int.from_bytes(b, signed=True), i)
def test_is_integer(self):
self.assertTrue((-1).is_integer())
self.assertTrue((0).is_integer())
self.assertTrue((1).is_integer())
def test_access_to_nonexistent_digit_0(self):
# http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that
# ob_digit[0] was being incorrectly accessed for instances of a
@@ -1602,5 +1604,47 @@ class LongTest(unittest.TestCase):
self.assertEqual(n**2,
(1 << (2 * bitlen)) - (1 << (bitlen + 1)) + 1)
@unittest.expectedFailure # TODO: RUSTPYTHON
def test___sizeof__(self):
self.assertEqual(int.__itemsize__, sys.int_info.sizeof_digit)
# Pairs (test_value, number of allocated digits)
test_values = [
# We always allocate space for at least one digit, even for
# a value of zero; sys.getsizeof should reflect that.
(0, 1),
(1, 1),
(-1, 1),
(BASE-1, 1),
(1-BASE, 1),
(BASE, 2),
(-BASE, 2),
(BASE*BASE - 1, 2),
(BASE*BASE, 3),
]
for value, ndigits in test_values:
with self.subTest(value):
self.assertEqual(
value.__sizeof__(),
int.__basicsize__ + int.__itemsize__ * ndigits
)
# Same test for a subclass of int.
class MyInt(int):
pass
self.assertEqual(MyInt.__itemsize__, sys.int_info.sizeof_digit)
for value, ndigits in test_values:
with self.subTest(value):
self.assertEqual(
MyInt(value).__sizeof__(),
MyInt.__basicsize__ + MyInt.__itemsize__ * ndigits
)
# GH-117195 -- This shouldn't crash
object.__sizeof__(1)
if __name__ == "__main__":
unittest.main()
+5 -2
View File
@@ -686,8 +686,11 @@ impl PyInt {
}
#[pymethod]
/// Returns the number of ones 1 an int. When the number is < 0,
/// then it returns the number of ones of the absolute value.
const fn is_integer(&self) -> bool {
true
}
#[pymethod]
fn bit_count(&self) -> u32 {
self.value.iter_u32_digits().map(|n| n.count_ones()).sum()
}