Skip to content

Commit b1d5c65

Browse files
agattidpgeorge
authored andcommitted
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>
1 parent f10707f commit b1d5c65

6 files changed

+67
-60
lines changed

tests/micropython/viper_ptr16_store_boundary_intbig.py renamed to tests/micropython/viper_ptr16_store_boundary.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ def set{off}(dest: ptr16):
1515
MASK = (1 << (8 * SIZE)) - 1
1616

1717

18+
next_int = 1
19+
test_buffer = bytearray(SIZE)
20+
21+
22+
def next_value() -> uint:
23+
global next_int
24+
global test_buffer
25+
for index in range(1, SIZE):
26+
test_buffer[index - 1] = test_buffer[index]
27+
test_buffer[SIZE - 1] = next_int
28+
next_int += 1
29+
output = 0
30+
for byte in test_buffer:
31+
output = (output << 8) | byte
32+
return output & MASK
33+
34+
1835
@micropython.viper
1936
def set_index(dest: ptr16, i: int, val: uint):
2037
saved = dest
@@ -27,31 +44,17 @@ def get_index(src, i):
2744

2845

2946
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
30-
next = 1
31-
val = 0
3247
for bit in BIT_THRESHOLDS:
3348
print("---", bit)
3449
pre, idx, post = (
3550
(((1 << bit) - (2 * SIZE)) // SIZE),
3651
(((1 << bit) - (1 * SIZE)) // SIZE),
3752
((1 << bit) // SIZE),
3853
)
39-
val = (val << 8) + next
40-
next += 1
41-
set_index(buffer, pre, val & MASK)
42-
val = (val << 8) + next
43-
next += 1
44-
set_index(buffer, idx, val & MASK)
45-
val = (val << 8) + next
46-
next += 1
47-
set_index(buffer, post, val & MASK)
48-
val = (val << 8) + next
49-
next += 1
54+
set_index(buffer, pre, next_value())
55+
set_index(buffer, idx, next_value())
56+
set_index(buffer, post, next_value())
5057
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
51-
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
52-
val = (val << 8) + next
53-
next += 1
54-
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
55-
val = (val << 8) + next
56-
next += 1
57-
exec(SET_TEMPLATE.format(off=post, val=val & MASK))
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()))

tests/micropython/viper_ptr32_store_boundary_intbig.py renamed to tests/micropython/viper_ptr32_store_boundary.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ def set{off}(dest: ptr32):
1414
SIZE = 4
1515
MASK = (1 << (8 * SIZE)) - 1
1616

17+
next_int = 1
18+
test_buffer = bytearray(SIZE)
19+
20+
21+
def next_value() -> uint:
22+
global next_int
23+
global test_buffer
24+
for index in range(1, SIZE):
25+
test_buffer[index - 1] = test_buffer[index]
26+
test_buffer[SIZE - 1] = next_int
27+
next_int += 1
28+
output = 0
29+
for byte in test_buffer:
30+
output = (output << 8) | byte
31+
return output & MASK
32+
1733

1834
@micropython.viper
1935
def set_index(dest: ptr32, i: int, val: uint):
@@ -32,31 +48,17 @@ def get_index(src, i):
3248

3349

3450
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
35-
next = 1
36-
val = 0
3751
for bit in BIT_THRESHOLDS:
3852
print("---", bit)
3953
pre, idx, post = (
4054
(((1 << bit) - (2 * SIZE)) // SIZE),
4155
(((1 << bit) - (1 * SIZE)) // SIZE),
4256
((1 << bit) // SIZE),
4357
)
44-
val = (val << 8) + next
45-
next += 1
46-
set_index(buffer, pre, val & MASK)
47-
val = (val << 8) + next
48-
next += 1
49-
set_index(buffer, idx, val & MASK)
50-
val = (val << 8) + next
51-
next += 1
52-
set_index(buffer, post, val & MASK)
53-
val = (val << 8) + next
54-
next += 1
58+
set_index(buffer, pre, next_value())
59+
set_index(buffer, idx, next_value())
60+
set_index(buffer, post, next_value())
5561
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
56-
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
57-
val = (val << 8) + next
58-
next += 1
59-
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
60-
val = (val << 8) + next
61-
next += 1
62-
exec(SET_TEMPLATE.format(off=post, val=val & MASK))
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()))

tests/micropython/viper_ptr8_store_boundary_intbig.py renamed to tests/micropython/viper_ptr8_store_boundary.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ def set{off}(dest: ptr8):
1414
SIZE = 1
1515
MASK = (1 << (8 * SIZE)) - 1
1616

17+
next_int = 1
18+
test_buffer = bytearray(SIZE)
19+
20+
21+
def next_value() -> uint:
22+
global next_int
23+
global test_buffer
24+
for index in range(1, SIZE):
25+
test_buffer[index - 1] = test_buffer[index]
26+
test_buffer[SIZE - 1] = next_int
27+
next_int += 1
28+
output = 0
29+
for byte in test_buffer:
30+
output = (output << 8) | byte
31+
return output & MASK
32+
1733

1834
@micropython.viper
1935
def set_index(dest: ptr8, i: int, val: uint):
@@ -27,27 +43,13 @@ def get_index(src: ptr8, i: int):
2743

2844

2945
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
30-
next = 1
31-
val = 0
3246
for bit in BIT_THRESHOLDS:
3347
print("---", bit)
3448
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
35-
val = (val << 8) + next
36-
next += 1
37-
set_index(buffer, pre, val & MASK)
38-
val = (val << 8) + next
39-
next += 1
40-
set_index(buffer, idx, val & MASK)
41-
val = (val << 8) + next
42-
next += 1
43-
set_index(buffer, post, val & MASK)
44-
val = (val << 8) + next
45-
next += 1
49+
set_index(buffer, pre, next_value())
50+
set_index(buffer, idx, next_value())
51+
set_index(buffer, post, next_value())
4652
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
47-
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
48-
val = (val << 8) + next
49-
next += 1
50-
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
51-
val = (val << 8) + next
52-
next += 1
53-
exec(SET_TEMPLATE.format(off=post, val=val & MASK))
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()))

0 commit comments

Comments
 (0)