◐ Shell
reader mode source ↗
Skip to content
Merged
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
226 changes: 221 additions & 5 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ def test_long(self):
self.assertRaises(ValueError, int, '\u3053\u3093\u306b\u3061\u306f')


def test_conversion(self):

class JustLong:
Expand All @@ -392,7 +394,8 @@ def __long__(self):
return 42
def __trunc__(self):
return 1729
self.assertEqual(int(LongTrunc()), 1729)

def check_float_conversion(self, n):
# Check that int -> float conversion behaviour matches
Expand Down Expand Up @@ -952,8 +955,13 @@ def test_huge_lshift(self, size):
self.assertEqual(1 << (sys.maxsize + 1000), 1 << 1000 << sys.maxsize)

def test_huge_rshift(self):
self.assertEqual(42 >> (1 << 1000), 0)
self.assertEqual((-42) >> (1 << 1000), -1)

@support.cpython_only
@support.bigmemtest(sys.maxsize + 500, memuse=2/15, dry_run=False)
Expand All @@ -962,6 +970,64 @@ def test_huge_rshift_of_huge(self, size):
self.assertEqual(huge >> (sys.maxsize + 1), (1 << 499) + 5)
self.assertEqual(huge >> (sys.maxsize + 1000), 0)

@support.cpython_only
def test_small_ints_in_huge_calculation(self):
a = 2 ** 100
Expand All @@ -970,6 +1036,48 @@ def test_small_ints_in_huge_calculation(self):
self.assertIs(a + b, 1)
self.assertIs(c - a, 1)

def test_small_ints(self):
for i in range(-5, 257):
self.assertIs(i, i + 0)
Expand Down Expand Up @@ -1111,16 +1219,44 @@ def test_round(self):

def test_to_bytes(self):
def check(tests, byteorder, signed=False):
for test, expected in tests.items():
try:
self.assertEqual(
test.to_bytes(len(expected), byteorder, signed=signed),
expected)
except Exception as err:
raise AssertionError(
"failed to convert {0} with byteorder={1} and signed={2}"
.format(test, byteorder, signed)) from err

# Convert integers to signed big-endian byte arrays.
tests1 = {
0: b'\x00',
Expand Down Expand Up @@ -1208,18 +1344,58 @@ def check(tests, byteorder, signed=False):
b'\xff\xff\xff\xff\xff')
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_from_bytes(self):
def check(tests, byteorder, signed=False):
for test, expected in tests.items():
try:
self.assertEqual(
int.from_bytes(test, byteorder, signed=signed),
expected)
except Exception as err:
raise AssertionError(
"failed to convert {0} with byteorder={1!r} and signed={2}"
.format(test, byteorder, signed)) from err

# Convert signed big-endian byte arrays to integers.
Expand Down Expand Up @@ -1360,6 +1536,35 @@ def __init__(self, value):
self.assertEqual(i, 1)
self.assertEqual(getattr(i, 'foo', 'none'), 'bar')

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
Expand Down Expand Up @@ -1391,6 +1596,17 @@ class myint(int):
self.assertEqual(type(numerator), int)
self.assertEqual(type(denominator), int)


if __name__ == "__main__":
unittest.main()
Toggle all file notes Toggle all file annotations