-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_uint_test.py
81 lines (66 loc) · 2.11 KB
/
int_uint_test.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# (c) 2023 Hermann Paul von Borries
# MIT License
import array
import builtins
@micropython.viper
def test_ptr_returns_int():
a1 = array.array("l", (1,-1,2,0x7fffffff,0x80000000))
ptra1 = ptr32(a1)
# Test if signed int
x = ptra1[0] << 31
assert x < 0
print("ptr32[] returns a viper signed int")
a2 = array.array("H", (1,10,100,1000,65525))
ptra2 = ptr16(a2)
y = ptra2[0] << 31
if y < 0:
print("ptr16[] returns a viper signed int")
else:
print("ptr16[] returns a unsigned int")
assert False
a3 = bytearray(((1,2,4,8,255)))
ptra3 = ptr8(a3)
z = ptra3[0] << 31
if z < 0:
print("ptr8[] returns a viper signed int")
else:
print("ptr8[] returns a unsigned int")
assert False
a4 = array.array("l", (0xffffffff, 1))
if ptr32(a4)[0] == -1 and ptr32(a4)[1] == 1:
print("ptr32 returns all 32 bits")
else:
print("Unexpected a4 pointer behaviour")
assert False
a5 = array.array("H", (0xffff, 1, 0xc0c0))
if ptr16(a5)[0] == 0xffff and ptr16(a5)[1] == 1 and ptr16(a5)[2] == 0xc0c0:
print("ptr16 returns 16 bits, no sign propagation")
else:
print(f"Unexpected ptr16 pointer result {ptr16(a5)[0]=:x} {ptr16(a5)[3]=:x}")
assert False
a6 = array.array("B", (0xff, 1, 0xA6))
if ptr8(a6)[0] == 0xff and ptr8(a6)[1] == 1 and ptr8(a6)[2] == 0xA6:
print("ptr32 returns 16 bits, no sign propagation")
else:
print(f"Unexpected a4 pointer result {ptr8(a6)[0]=:x} {ptr8(a6)[1]=:x}")
assert False
test_ptr_returns_int()
@micropython.viper
def test_uint_int_assignments():
x = int(-1)
y = uint(x)
print(f"{x=} uint(x)={y=:08x} expected 0xffffffff")
assert y == uint(0xffffffff)
z = int(y)
print(f"{y=} int(y)={y=:08x} expected 0xffffffff")
assert z == -1
print(f"{str(x)=} {repr(x)=}")
assert str(x) == "-1"
assert repr(x) == "-1"
test_uint_int_assignments()
@micropython.viper
def wrap_around():
y = 2**15
x = 2**17
print(f"viper arithmetic {x}*{y}={x*y}")
wrap_around()