From e675ea3ca4a09e5afc718208e7ba7dd1c8817018 Mon Sep 17 00:00:00 2001 From: KB Sriram Date: Sat, 17 Feb 2024 20:26:37 -0800 Subject: [PATCH] Add tests for bit_length() Also verified the tests fail without the fix in https://github.com/adafruit/circuitpython/pull/4845 Fixes https://github.com/adafruit/circuitpython/issues/4846 --- tests/basics/int_length.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tests/basics/int_length.py diff --git a/tests/basics/int_length.py b/tests/basics/int_length.py new file mode 100644 index 000000000000..56730297c3e9 --- /dev/null +++ b/tests/basics/int_length.py @@ -0,0 +1,23 @@ +# CIRCUITPY-CHANGE +# test bit_length for various sizes of ints + +for x in range(-10, 10): + print(x.bit_length()) + +for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]: + a = 2**i + print((a - 1).bit_length()) + print((1 - a).bit_length()) + print(a.bit_length()) + print((-a).bit_length()) + print((a + 1).bit_length()) + print((-a - 1).bit_length()) + # Ensure transitioning between small and large int representations + # still work. + print((a - a).bit_length()) + print((0 * a).bit_length()) + print((0 * a - 1).bit_length()) + print((0 * a + 1).bit_length()) + print((2 * a).bit_length()) + print((2 * a + 1).bit_length()) + print((2 * a - 1).bit_length())