circuitpython/tests/micropython/viper_ptr16_store_boundary.py
Alessandro Gatti b0fd0079f4 tests/micropython: Make tests behave in low memory condition.
This commit changes the "viper_ptr*_store_boundary" tests to make them
fail more gracefully in low memory conditions.

The original version of the tests compiled viper code blocks on the fly
when it needed them, making them fail at runtime on some boards that do
not come with enough memory for this test.  This clashes with
"run-tests.py"'s ability to look for a particular signature to mark
tests as skipped due to not enough memory.

Now compiled code blocks are generated at the beginning of the test
inside an appropriate exception handler.  In case of a memory error when
pre-compiling a code block, the running test exits reporting a low
memory condition to the test runner.  This allows to have clean test
runs on all platforms when it comes to viper pointer tests.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-08-26 10:47:17 +10:00

63 lines
1.6 KiB
Python

# Test boundary conditions for various architectures
SET_TEMPLATE = """
@micropython.viper
def set{off}(dest: ptr16):
saved = dest
dest[{off}] = {val}
assert int(saved) == int(dest)
"""
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 2
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
def get_index(src, i):
return src[i * SIZE] + (src[(i * SIZE) + 1] << 8)
@micropython.viper
def set_index(dest: ptr16, i: int, val: uint):
saved = dest
dest[i] = val
assert int(saved) == int(dest)
try:
buffer = bytearray((((1 << max(BIT_THRESHOLDS)) // 1024) + 1) * 1024)
for bit in BIT_THRESHOLDS:
offset = (1 << bit) - (2 * SIZE)
for index in range(0, 3 * SIZE, SIZE):
exec(SET_TEMPLATE.format(off=(offset + index) // SIZE, val=next_value()))
except MemoryError:
print("SKIP-TOO-LARGE")
raise SystemExit
for bit in BIT_THRESHOLDS:
print("---", bit)
offset = (1 << bit) - (2 * SIZE)
for index in range(0, 3 * SIZE, SIZE):
globals()["set{}".format((offset + index) // SIZE)](buffer)
print(hex(get_index(buffer, (offset + index) // SIZE)))
for index in range(0, 3 * SIZE, SIZE):
set_index(buffer, (offset + index) // SIZE, next_value())
print(hex(get_index(buffer, (offset + index) // SIZE)))