diff options
author | Alessandro Gatti <a.gatti@frob.it> | 2025-07-24 23:48:01 +0200 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2025-08-11 16:46:15 +1000 |
commit | b1d5c656de3f11fc1729f165c8d9709594ccbfb6 (patch) | |
tree | 3b8f0f448aa53fcef222030d1da5055b7f9f653d /tests/micropython/viper_ptr32_store_boundary.py | |
parent | f10707febb41e042e929efaf9ee4a66b847a68d4 (diff) |
tests/micropython: Remove big ints dependence for viper boundary tests.
This commit provides an implementation for viper boundary tests that can
work even without big int support.
Since it uses a fixed-size buffer to hold values to work with, this
should work on any platform as long as its integers are at least 32 bits
wide, regardless its configuration on how big integers can get.
Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
Diffstat (limited to 'tests/micropython/viper_ptr32_store_boundary.py')
-rw-r--r-- | tests/micropython/viper_ptr32_store_boundary.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/micropython/viper_ptr32_store_boundary.py b/tests/micropython/viper_ptr32_store_boundary.py new file mode 100644 index 000000000..8c207278e --- /dev/null +++ b/tests/micropython/viper_ptr32_store_boundary.py @@ -0,0 +1,64 @@ +# Test boundary conditions for various architectures + +SET_TEMPLATE = """ +@micropython.viper +def set{off}(dest: ptr32): + saved = dest + dest[{off}] = {val} + assert int(saved) == int(dest) +set{off}(buffer) +print(hex(get_index(buffer, {off}))) +""" + +BIT_THRESHOLDS = (5, 8, 11, 12) +SIZE = 4 +MASK = (1 << (8 * SIZE)) - 1 + +next_int = 1 +test_buffer = bytearray(SIZE) + + +def next_value() -> uint: + global next_int + global test_buffer + for index in range(1, SIZE): + test_buffer[index - 1] = test_buffer[index] + test_buffer[SIZE - 1] = next_int + next_int += 1 + output = 0 + for byte in test_buffer: + output = (output << 8) | byte + return output & MASK + + +@micropython.viper +def set_index(dest: ptr32, i: int, val: uint): + saved = dest + dest[i] = val + assert int(saved) == int(dest) + + +def get_index(src, i): + return ( + src[i * SIZE] + + (src[(i * SIZE) + 1] << 8) + + (src[(i * SIZE) + 2] << 16) + + (src[(i * SIZE) + 3] << 24) + ) + + +buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024) +for bit in BIT_THRESHOLDS: + print("---", bit) + pre, idx, post = ( + (((1 << bit) - (2 * SIZE)) // SIZE), + (((1 << bit) - (1 * SIZE)) // SIZE), + ((1 << bit) // SIZE), + ) + set_index(buffer, pre, next_value()) + set_index(buffer, idx, next_value()) + set_index(buffer, post, next_value()) + print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post))) + exec(SET_TEMPLATE.format(off=pre, val=next_value())) + exec(SET_TEMPLATE.format(off=idx, val=next_value())) + exec(SET_TEMPLATE.format(off=post, val=next_value())) |