tests/micropython: Improve viper ptr boundary tests.

This commit reworks the Viper pointer boundary tests in order to make
them more accurate and easier to extend.

The tests are now easier to reason about in their output, using easier
to read values, and bit thresholds are now more configurable.  If a new
conditional code sequence is introduced, adding a new bit threshold is
just a matter of adding a value into a tuple at the beginning of the
relevant test file.

Load tests have also been made more accurate, with better function
templates to test register-indexed operations.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit is contained in:
Alessandro Gatti
2025-06-11 09:31:34 +02:00
committed by Damien George
parent c8c8b04569
commit 0d435959e0
12 changed files with 303 additions and 172 deletions

View File

@@ -2,24 +2,38 @@
GET_TEMPLATE = """ GET_TEMPLATE = """
@micropython.viper @micropython.viper
def get{off}(src: ptr16) -> int: def get{off}(src: ptr16) -> uint:
return src[{off}] return uint(src[{off}])
print(b[{off} * 2:({off} + 1) * 2]) print(hex(get{off}(buffer)))
""" """
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 2
@micropython.viper @micropython.viper
def get_index(src: ptr16, i: int) -> int: def get_index(src: ptr16, i: int) -> int:
return src[i] return src[i]
b = bytearray(5000) def data(start, len):
b[28:38] = b"0123456789" output = bytearray(len)
b[252:262] = b"ABCDEFGHIJ" for idx in range(len):
b[4092:4102] = b"KLMNOPQRST" output[idx] = (start + idx) & 0xFF
return output
for pre, idx, post in (15, 16, 17), (127, 128, 129), (2047, 2048, 2049):
print(get_index(b, pre), get_index(b, idx), get_index(b, post)) buffer = bytearray((((1 << max(BIT_THRESHOLDS)) + 1) // 1024) * 1024)
val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = ((1 << bit) - (2 * SIZE), (1 << bit) - (1 * SIZE), 1 << bit)
buffer[pre:post] = data(val, 3 * SIZE)
val = val + (3 * SIZE)
pre, idx, post = pre // SIZE, idx // SIZE, post // SIZE
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(GET_TEMPLATE.format(off=pre)) exec(GET_TEMPLATE.format(off=pre))
exec(GET_TEMPLATE.format(off=idx)) exec(GET_TEMPLATE.format(off=idx))
exec(GET_TEMPLATE.format(off=post)) exec(GET_TEMPLATE.format(off=post))

View File

@@ -1,12 +1,20 @@
13106 13620 14134 --- 5
bytearray(b'23') 0x100 0x302 0x504
bytearray(b'45') 0x100
bytearray(b'67') 0x302
17475 17989 18503 0x504
bytearray(b'CD') --- 8
bytearray(b'EF') 0x706 0x908 0xb0a
bytearray(b'GH') 0x706
20045 20559 21073 0x908
bytearray(b'MN') 0xb0a
bytearray(b'OP') --- 11
bytearray(b'QR') 0xd0c 0xf0e 0x1110
0xd0c
0xf0e
0x1110
--- 12
0x1312 0x1514 0x1716
0x1312
0x1514
0x1716

View File

@@ -4,38 +4,50 @@ SET_TEMPLATE = """
@micropython.viper @micropython.viper
def set{off}(dest: ptr16): def set{off}(dest: ptr16):
dest[{off}] = {val} dest[{off}] = {val}
set{off}(b) set{off}(buffer)
print(b[{off} * 2:({off} + 1) * 2]) print(hex(get_index(buffer, {off})))
""" """
TEST_DATA = ( BIT_THRESHOLDS = (5, 8, 11, 12)
(15, (0x4241, 0x4443, 0x4645)), SIZE = 2
(127, (0x4847, 0x4A49, 0x4C4B)), MASK = (1 << (8 * SIZE)) - 1
(2047, (0x4E4D, 0x504F, 0x5251)),
)
@micropython.viper @micropython.viper
def set_index(dest: ptr16, i: int, val: int): def set_index(dest: ptr16, i: int, val: uint):
dest[i] = val dest[i] = val
@micropython.viper def get_index(src, i):
def set_index(dest: ptr16, i: int, val: int): return src[i * SIZE] + (src[(i * SIZE) + 1] << 8)
dest[i] = val
b = bytearray(5000) buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
for start, vals in TEST_DATA: next = 1
for i, v in enumerate(vals): val = 0
set_index(b, start + i, v) for bit in BIT_THRESHOLDS:
print(b[(start + i) * 2 : (start + i + 1) * 2]) print("---", bit)
pre, idx, post = (
(((1 << bit) - (2 * SIZE)) // SIZE),
for i in range(len(b)): (((1 << bit) - (1 * SIZE)) // SIZE),
b[i] = 0 ((1 << bit) // SIZE),
)
val = (val << 8) + next
for start, vals in TEST_DATA: next += 1
for i, v in enumerate(vals): set_index(buffer, pre, val & MASK)
exec(SET_TEMPLATE.format(off=start + i, val=v + 0x0101)) val = (val << 8) + next
next += 1
set_index(buffer, idx, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, post, val & MASK)
val = (val << 8) + next
next += 1
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=post, val=val & MASK))

View File

@@ -1,18 +1,20 @@
bytearray(b'AB') --- 5
bytearray(b'CD') 0x1 0x102 0x203
bytearray(b'EF') 0x304
bytearray(b'GH') 0x405
bytearray(b'IJ') 0x506
bytearray(b'KL') --- 8
bytearray(b'MN') 0x607 0x708 0x809
bytearray(b'OP') 0x90a
bytearray(b'QR') 0xa0b
bytearray(b'BC') 0xb0c
bytearray(b'DE') --- 11
bytearray(b'FG') 0xc0d 0xd0e 0xe0f
bytearray(b'HI') 0xf10
bytearray(b'JK') 0x1011
bytearray(b'LM') 0x1112
bytearray(b'NO') --- 12
bytearray(b'PQ') 0x1213 0x1314 0x1415
bytearray(b'RS') 0x1516
0x1617
0x1718

View File

@@ -2,24 +2,38 @@
GET_TEMPLATE = """ GET_TEMPLATE = """
@micropython.viper @micropython.viper
def get{off}(src: ptr32) -> int: def get{off}(src: ptr32) -> uint:
return src[{off}] return uint(src[{off}])
print(b[{off} * 4:({off} + 1) * 4]) print(hex(get{off}(buffer)))
""" """
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 4
@micropython.viper @micropython.viper
def get_index(src: ptr32, i: int) -> int: def get_index(src: ptr32, i: int) -> int:
return src[i] return src[i]
b = bytearray(5000) def data(start, len):
b[24:43] = b"0123456789ABCDEFGHIJ" output = bytearray(len)
b[248:268] = b"KLMNOPQRSTUVWXYZabcd" for idx in range(len):
b[4088:4108] = b"efghijklmnopqrstuvwx" output[idx] = (start + idx) & 0xFF
return output
for pre, idx, post in (7, 8, 9), (63, 64, 65), (1023, 1024, 1025):
print(get_index(b, pre), get_index(b, idx), get_index(b, post)) buffer = bytearray((((1 << max(BIT_THRESHOLDS)) + 1) // 1024) * 1024)
val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
buffer[pre:post] = data(val, 3 * SIZE)
val = val + (3 * SIZE)
pre, idx, post = pre // SIZE, idx // SIZE, post // SIZE
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(GET_TEMPLATE.format(off=pre)) exec(GET_TEMPLATE.format(off=pre))
exec(GET_TEMPLATE.format(off=idx)) exec(GET_TEMPLATE.format(off=idx))
exec(GET_TEMPLATE.format(off=post)) exec(GET_TEMPLATE.format(off=post))

View File

@@ -1,12 +1,20 @@
926299444 1111570744 1178944579 --- 5
bytearray(b'4567') 0x3020100 0x7060504 0xb0a0908
bytearray(b'89AB') 0x3020100
bytearray(b'CDEF') 0x7060504
1381060687 1448432723 1515804759 0xb0a0908
bytearray(b'OPQR') --- 8
bytearray(b'STUV') 0xf0e0d0c 0x13121110 0x17161514
bytearray(b'WXYZ') 0xf0e0d0c
1818978921 1886350957 1953722993 0x13121110
bytearray(b'ijkl') 0x17161514
bytearray(b'mnop') --- 11
bytearray(b'qrst') 0x1b1a1918 0x1f1e1d1c 0x23222120
0x1b1a1918
0x1f1e1d1c
0x23222120
--- 12
0x27262524 0x2b2a2928 0x2f2e2d2c
0x27262524
0x2b2a2928
0x2f2e2d2c

View File

@@ -1,35 +1,58 @@
# Test boundary conditions for various architectures # Test boundary conditions for various architectures
TEST_DATA = (
(3, (0x04030201, 0x08070605, 0x0C0B0A09)),
(63, (0x100F0E0D, 0x14131211, 0x18171615)),
(1023, (0x1C1B1A19, 0x201F1E1D, 0x24232221)),
)
SET_TEMPLATE = """ SET_TEMPLATE = """
@micropython.viper @micropython.viper
def set{off}(dest: ptr32): def set{off}(dest: ptr32):
dest[{off}] = {val} & 0x3FFFFFFF dest[{off}] = {val}
set{off}(b) set{off}(buffer)
print(b[{off} * 4:({off} + 1) * 4]) print(hex(get_index(buffer, {off})))
""" """
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 4
MASK = (1 << (8 * SIZE)) - 1
@micropython.viper @micropython.viper
def set_index(dest: ptr32, i: int, val: int): def set_index(dest: ptr32, i: int, val: uint):
dest[i] = val dest[i] = val
b = bytearray(5000) def get_index(src, i):
for start, vals in TEST_DATA: return (
for i, v in enumerate(vals): src[i * SIZE]
set_index(b, start + i, v) + (src[(i * SIZE) + 1] << 8)
print(b[(start + i) * 4 : (start + i + 1) * 4]) + (src[(i * SIZE) + 2] << 16)
+ (src[(i * SIZE) + 3] << 24)
for i in range(len(b)): )
b[i] = 0
for start, vals in TEST_DATA: buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
for i, v in enumerate(vals): next = 1
exec(SET_TEMPLATE.format(off=start + i, val=v + 0x01010101)) val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = (
(((1 << bit) - (2 * SIZE)) // SIZE),
(((1 << bit) - (1 * SIZE)) // SIZE),
((1 << bit) // SIZE),
)
val = (val << 8) + next
next += 1
set_index(buffer, pre, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, idx, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, post, val & MASK)
val = (val << 8) + next
next += 1
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=post, val=val & MASK))

View File

@@ -1,18 +1,20 @@
bytearray(b'\x01\x02\x03\x04') --- 5
bytearray(b'\x05\x06\x07\x08') 0x1 0x102 0x10203
bytearray(b'\t\n\x0b\x0c') 0x1020304
bytearray(b'\r\x0e\x0f\x10') 0x2030405
bytearray(b'\x11\x12\x13\x14') 0x3040506
bytearray(b'\x15\x16\x17\x18') --- 8
bytearray(b'\x19\x1a\x1b\x1c') 0x4050607 0x5060708 0x6070809
bytearray(b'\x1d\x1e\x1f ') 0x708090a
bytearray(b'!"#$') 0x8090a0b
bytearray(b'\x02\x03\x04\x05') 0x90a0b0c
bytearray(b'\x06\x07\x08\t') --- 11
bytearray(b'\n\x0b\x0c\r') 0xa0b0c0d 0xb0c0d0e 0xc0d0e0f
bytearray(b'\x0e\x0f\x10\x11') 0xd0e0f10
bytearray(b'\x12\x13\x14\x15') 0xe0f1011
bytearray(b'\x16\x17\x18\x19') 0xf101112
bytearray(b'\x1a\x1b\x1c\x1d') --- 12
bytearray(b'\x1e\x1f !') 0x10111213 0x11121314 0x12131415
bytearray(b'"#$%') 0x13141516
0x14151617
0x15161718

View File

@@ -2,24 +2,37 @@
GET_TEMPLATE = """ GET_TEMPLATE = """
@micropython.viper @micropython.viper
def get{off}(src: ptr8) -> int: def get{off}(src: ptr8) -> uint:
return src[{off}] return uint(src[{off}])
print(get{off}(b)) print(hex(get{off}(buffer)))
""" """
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 1
@micropython.viper @micropython.viper
def get_index(src: ptr8, i: int) -> int: def get_index(src: ptr8, i: int) -> int:
return src[i] return src[i]
b = bytearray(5000) def data(start, len):
b[30:32] = b"123" output = bytearray(len)
b[254:256] = b"456" for idx in range(len):
b[4094:4096] = b"789" output[idx] = (start + idx) & 0xFF
return output
for pre, idx, post in (30, 31, 32), (254, 255, 256), (4094, 4095, 4096):
print(get_index(b, pre), get_index(b, idx), get_index(b, post)) buffer = bytearray((((1 << max(BIT_THRESHOLDS)) + 1) // 1024) * 1024)
val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
buffer[pre:post] = data(val, 3 * SIZE)
val = val + (3 * SIZE)
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(GET_TEMPLATE.format(off=pre)) exec(GET_TEMPLATE.format(off=pre))
exec(GET_TEMPLATE.format(off=idx)) exec(GET_TEMPLATE.format(off=idx))
exec(GET_TEMPLATE.format(off=post)) exec(GET_TEMPLATE.format(off=post))

View File

@@ -1,12 +1,20 @@
49 50 51 --- 5
49 0x0 0x1 0x2
50 0x0
51 0x1
52 53 54 0x2
52 --- 8
53 0x3 0x4 0x5
54 0x3
55 56 57 0x4
55 0x5
56 --- 11
57 0x6 0x7 0x8
0x6
0x7
0x8
--- 12
0x9 0xa 0xb
0x9
0xa
0xb

View File

@@ -1,30 +1,49 @@
# Test boundary conditions for various architectures # Test boundary conditions for various architectures
TEST_DATA = ((49, 30, 3), (52, 254, 3), (55, 4094, 3))
SET_TEMPLATE = """ SET_TEMPLATE = """
@micropython.viper @micropython.viper
def set{off}(dest: ptr8): def set{off}(dest: ptr8):
dest[{off}] = {val} dest[{off}] = {val}
set{off}(b) set{off}(buffer)
print(b[{off}]) print(hex(get_index(buffer, {off})))
""" """
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 1
MASK = (1 << (8 * SIZE)) - 1
@micropython.viper @micropython.viper
def set_index(dest: ptr8, i: int, val: int): def set_index(dest: ptr8, i: int, val: uint):
dest[i] = val dest[i] = val
b = bytearray(5000) def get_index(src: ptr8, i: int):
for val, start, count in TEST_DATA: return src[i]
for i in range(count):
set_index(b, start + i, val + i)
print(b[start : start + count])
for i in range(len(b)):
b[i] = 0
for val, start, count in TEST_DATA: buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
for i in range(count): next = 1
exec(SET_TEMPLATE.format(off=start + i, val=val + i + 16)) val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
val = (val << 8) + next
next += 1
set_index(buffer, pre, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, idx, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, post, val & MASK)
val = (val << 8) + next
next += 1
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=post, val=val & MASK))

View File

@@ -1,12 +1,20 @@
bytearray(b'123') --- 5
bytearray(b'456') 0x1 0x2 0x3
bytearray(b'789') 0x4
65 0x5
66 0x6
67 --- 8
68 0x7 0x8 0x9
69 0xa
70 0xb
71 0xc
72 --- 11
73 0xd 0xe 0xf
0x10
0x11
0x12
--- 12
0x13 0x14 0x15
0x16
0x17
0x18