Skip to content

Commit 4510716

Browse files
committed
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>
1 parent 989abae commit 4510716

6 files changed

+108
-60
lines changed

tests/micropython/viper_ptr16_store_boundary.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def set{off}(dest: ptr16):
66
saved = dest
77
dest[{off}] = {val}
88
assert int(saved) == int(dest)
9+
"""
10+
11+
GET_TEMPLATE = """
912
set{off}(buffer)
1013
print(hex(get_index(buffer, {off})))
1114
"""
@@ -14,7 +17,6 @@ def set{off}(dest: ptr16):
1417
SIZE = 2
1518
MASK = (1 << (8 * SIZE)) - 1
1619

17-
1820
next_int = 1
1921
test_buffer = bytearray(SIZE)
2022

@@ -43,18 +45,22 @@ def get_index(src, i):
4345
return src[i * SIZE] + (src[(i * SIZE) + 1] << 8)
4446

4547

48+
try:
49+
for bit in BIT_THRESHOLDS:
50+
offset = (1 << bit) - (2 * SIZE)
51+
for index in range(0, 3 * SIZE, SIZE):
52+
exec(SET_TEMPLATE.format(off=(offset + index) // SIZE, val=next_value()))
53+
except MemoryError:
54+
print("SKIP-TOO-LARGE")
55+
raise SystemExit
56+
57+
4658
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
4759
for bit in BIT_THRESHOLDS:
4860
print("---", bit)
49-
pre, idx, post = (
50-
(((1 << bit) - (2 * SIZE)) // SIZE),
51-
(((1 << bit) - (1 * SIZE)) // SIZE),
52-
((1 << bit) // SIZE),
53-
)
54-
set_index(buffer, pre, next_value())
55-
set_index(buffer, idx, next_value())
56-
set_index(buffer, post, next_value())
57-
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
58-
exec(SET_TEMPLATE.format(off=pre, val=next_value()))
59-
exec(SET_TEMPLATE.format(off=idx, val=next_value()))
60-
exec(SET_TEMPLATE.format(off=post, val=next_value()))
61+
offset = (1 << bit) - (2 * SIZE)
62+
for index in range(0, 3 * SIZE, SIZE):
63+
exec(GET_TEMPLATE.format(off=(offset + index) // SIZE))
64+
for index in range(0, 3 * SIZE, SIZE):
65+
set_index(buffer, (offset + index) // SIZE, next_value())
66+
print(hex(get_index(buffer, (offset + index) // SIZE)))
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
--- 5
2-
0x1 0x102 0x203
2+
0x1
3+
0x102
4+
0x203
5+
0xc0d
6+
0xd0e
7+
0xe0f
8+
--- 8
39
0x304
410
0x405
511
0x506
6-
--- 8
7-
0x607 0x708 0x809
8-
0x90a
9-
0xa0b
10-
0xb0c
11-
--- 11
12-
0xc0d 0xd0e 0xe0f
1312
0xf10
1413
0x1011
1514
0x1112
15+
--- 11
16+
0x607
17+
0x708
18+
0x809
19+
0x1213
20+
0x1314
21+
0x1415
1622
--- 12
17-
0x1213 0x1314 0x1415
23+
0x90a
24+
0xa0b
25+
0xb0c
1826
0x1516
1927
0x1617
2028
0x1718

tests/micropython/viper_ptr32_store_boundary.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def set{off}(dest: ptr32):
66
saved = dest
77
dest[{off}] = {val}
88
assert int(saved) == int(dest)
9+
"""
10+
11+
GET_TEMPLATE = """
912
set{off}(buffer)
1013
print(hex(get_index(buffer, {off})))
1114
"""
@@ -47,18 +50,22 @@ def get_index(src, i):
4750
)
4851

4952

53+
try:
54+
for bit in BIT_THRESHOLDS:
55+
offset = (1 << bit) - (2 * SIZE)
56+
for index in range(0, 3 * SIZE, SIZE):
57+
exec(SET_TEMPLATE.format(off=(offset + index) // SIZE, val=next_value()))
58+
except MemoryError:
59+
print("SKIP-TOO-LARGE")
60+
raise SystemExit
61+
62+
5063
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
5164
for bit in BIT_THRESHOLDS:
5265
print("---", bit)
53-
pre, idx, post = (
54-
(((1 << bit) - (2 * SIZE)) // SIZE),
55-
(((1 << bit) - (1 * SIZE)) // SIZE),
56-
((1 << bit) // SIZE),
57-
)
58-
set_index(buffer, pre, next_value())
59-
set_index(buffer, idx, next_value())
60-
set_index(buffer, post, next_value())
61-
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
62-
exec(SET_TEMPLATE.format(off=pre, val=next_value()))
63-
exec(SET_TEMPLATE.format(off=idx, val=next_value()))
64-
exec(SET_TEMPLATE.format(off=post, val=next_value()))
66+
offset = (1 << bit) - (2 * SIZE)
67+
for index in range(0, 3 * SIZE, SIZE):
68+
exec(GET_TEMPLATE.format(off=(offset + index) // SIZE))
69+
for index in range(0, 3 * SIZE, SIZE):
70+
set_index(buffer, (offset + index) // SIZE, next_value())
71+
print(hex(get_index(buffer, (offset + index) // SIZE)))
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
--- 5
2-
0x1 0x102 0x10203
2+
0x1
3+
0x102
4+
0x10203
5+
0xa0b0c0d
6+
0xb0c0d0e
7+
0xc0d0e0f
8+
--- 8
39
0x1020304
410
0x2030405
511
0x3040506
6-
--- 8
7-
0x4050607 0x5060708 0x6070809
8-
0x708090a
9-
0x8090a0b
10-
0x90a0b0c
11-
--- 11
12-
0xa0b0c0d 0xb0c0d0e 0xc0d0e0f
1312
0xd0e0f10
1413
0xe0f1011
1514
0xf101112
15+
--- 11
16+
0x4050607
17+
0x5060708
18+
0x6070809
19+
0x10111213
20+
0x11121314
21+
0x12131415
1622
--- 12
17-
0x10111213 0x11121314 0x12131415
23+
0x708090a
24+
0x8090a0b
25+
0x90a0b0c
1826
0x13141516
1927
0x14151617
2028
0x15161718

tests/micropython/viper_ptr8_store_boundary.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ def set{off}(dest: ptr8):
66
saved = dest
77
dest[{off}] = {val}
88
assert int(saved) == int(dest)
9+
"""
10+
11+
GET_TEMPLATE = """
912
set{off}(buffer)
1013
print(hex(get_index(buffer, {off})))
1114
"""
@@ -42,14 +45,22 @@ def get_index(src: ptr8, i: int):
4245
return src[i]
4346

4447

48+
try:
49+
for bit in BIT_THRESHOLDS:
50+
offset = (1 << bit) - (2 * SIZE)
51+
for index in range(0, 3 * SIZE, SIZE):
52+
exec(SET_TEMPLATE.format(off=(offset + index) // SIZE, val=next_value()))
53+
except MemoryError:
54+
print("SKIP-TOO-LARGE")
55+
raise SystemExit
56+
57+
4558
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
4659
for bit in BIT_THRESHOLDS:
4760
print("---", bit)
48-
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
49-
set_index(buffer, pre, next_value())
50-
set_index(buffer, idx, next_value())
51-
set_index(buffer, post, next_value())
52-
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
53-
exec(SET_TEMPLATE.format(off=pre, val=next_value()))
54-
exec(SET_TEMPLATE.format(off=idx, val=next_value()))
55-
exec(SET_TEMPLATE.format(off=post, val=next_value()))
61+
offset = (1 << bit) - (2 * SIZE)
62+
for index in range(0, 3 * SIZE, SIZE):
63+
exec(GET_TEMPLATE.format(off=(offset + index) // SIZE))
64+
for index in range(0, 3 * SIZE, SIZE):
65+
set_index(buffer, (offset + index) // SIZE, next_value())
66+
print(hex(get_index(buffer, (offset + index) // SIZE)))
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
--- 5
2-
0x1 0x2 0x3
2+
0x1
3+
0x2
4+
0x3
5+
0xd
6+
0xe
7+
0xf
8+
--- 8
39
0x4
410
0x5
511
0x6
6-
--- 8
7-
0x7 0x8 0x9
8-
0xa
9-
0xb
10-
0xc
11-
--- 11
12-
0xd 0xe 0xf
1312
0x10
1413
0x11
1514
0x12
15+
--- 11
16+
0x7
17+
0x8
18+
0x9
19+
0x13
20+
0x14
21+
0x15
1622
--- 12
17-
0x13 0x14 0x15
23+
0xa
24+
0xb
25+
0xc
1826
0x16
1927
0x17
2028
0x18

0 commit comments

Comments
 (0)