summaryrefslogtreecommitdiff
path: root/tests/micropython/viper_ptr16_store_boundary.py
blob: 7c774d4d1ca18332cc18976f167f2cd8a0559020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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)))