Files
micropython/tests/basics/int_64_basics.py
Jeff Epler 1671977ca4
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
py/parsenum: Fix parsing LLONG_MIN in longlong configuration.
Re-organize `mp_parse_num_integer()` (for longlong) slightly so that the
most negative 64-bit integer can be parsed.

Fixes issue #17932.

Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-08-28 11:31:03 +10:00

162 lines
3.6 KiB
Python

# test support for 64-bit long integers
# (some ports don't support arbitrary precision but do support these)
# this test is adapted from int_big1.py with numbers kept within 64-bit signed range
# to test arbitrary precision integers
x = 1000000000000000000
xn = -1000000000000000000
y = 2000000000000000000
# printing
print(x)
print(y)
print('%#X' % (x - x)) # print prefix
print('{:#,}'.format(x)) # print with commas
# construction
print(int(x))
# addition
print(x + 1)
print(x + y)
print(x + xn == 0)
print(bool(x + xn))
# subtraction
print(x - 1)
print(x - y)
print(y - x)
print(x - x == 0)
print(bool(x - x))
# multiplication
print(x * 2)
print(1090511627776 * 1048500)
# integer division
print(x // 2)
print(y // x)
# bit inversion
print(~x)
print(~(-x))
# left shift
print("left shift positive")
x = 0x40000000
for i in range(32):
x = x << 1
print(x)
# right shift
print("right shift positive")
x = 0x2000000000000000 # TODO: why can't second-tip bit be set?
for i in range(64):
x = x >> 1
print(x)
# left shift of a negative number
print("left shift negative")
for i in range(8):
print(-10000000000000000 << i)
print(-10000000000000001 << i)
print(-10000000000000002 << i)
print(-10000000000000003 << i)
print(-10000000000000004 << i)
# right shift of a negative number
print("right shift negative")
for i in range(8):
print(-1000000000000000000 >> i)
print(-1000000000000000001 >> i)
print(-1000000000000000002 >> i)
print(-1000000000000000003 >> i)
print(-1000000000000000004 >> i)
# conversion from string
print(int("1234567890123456789"))
print(int("-1234567890123456789"))
print(int("1234567890abcdef", 16))
print(int("1234567890ABCDEF", 16))
print(int("-1234567890ABCDEF", 16))
print(int("ijklmnopqrsz", 36))
# numbers close to 64-bit limits
print(int("-9111222333444555666"))
print(int("9111222333444555666"))
# numbers with preceding 0s
print(int("-00000000000000000000009111222333444555666"))
print(int("0000000000000000000000009111222333444555666"))
# invalid characters in string
try:
print(int("1234567890abcdef"))
except ValueError:
print('ValueError');
try:
print(int("123456789\x01"))
except ValueError:
print('ValueError');
# test parsing ints just on threshold of small to big
# for 32 bit archs
x = 1073741823 # small
x = -1073741823 # small
x = 1073741824 # big
x = -1073741824 # big
# for 64 bit archs
x = 4611686018427387903 # small
x = -4611686018427387903 # small
x = 4611686018427387904 # big
x = -4611686018427387904 # big
# sys.maxsize is a constant bigint, so test it's compatible with dynamic ones
import sys
if hasattr(sys, "maxsize"):
print(sys.maxsize - 1 + 1 == sys.maxsize)
else:
print(True) # No maxsize property in this config
# test extraction of big int value via mp_obj_get_int_maybe
x = 1 << 62
print('a' * (x + 4 - x))
# test overflow check in mp_obj_get_int_maybe
x = 1 << 32
r = None
try:
r = range(0, x)
except OverflowError:
# 32-bit target, correctly handled the overflow of x
print("ok")
if r is not None:
if len(r) == x:
# 64-bit target, everything is just a small-int
print("ok")
else:
# 32-bit target that did not handle the overflow of x
print("unhandled overflow")
# negative shifts are invalid
try:
print((1 << 48) >> -4)
except ValueError as e:
print(e)
try:
print((1 << 48) << -6)
except ValueError as e:
print(e)
# Test that the most extreme 64 bit integer values all parse with int()
print(int("-9223372036854775807"))
print(int("-9223372036854775808"))
print(int("9223372036854775807"))
# Test that the most negative 64 bit integer can be formed via arithmetic
print(-9223372036854775807-1)