Skip to content

Commit d227620

Browse files
flowergrassdpgeorge
authored andcommitted
tests/extmod: Improve moductypes test coverage.
1 parent a3c6100 commit d227620

8 files changed

Lines changed: 126 additions & 1 deletion

tests/extmod/uctypes_array_assign_native_le.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
# aligned
1616
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
1717
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
18+
19+
"arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1),
20+
"arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1),
21+
"arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1),
22+
"arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1),
23+
"arr12": (uctypes.ARRAY | 0, uctypes.UINT64| 1),
24+
"arr13": (uctypes.ARRAY | 1, 1, {"l": {}}),
1825
}
1926

20-
data = bytearray(5)
27+
data = bytearray(8)
2128

2229
S = uctypes.struct(uctypes.addressof(data), desc)
2330

@@ -44,3 +51,45 @@
4451
print(S.arr5[0] == S.arr7[0].l)
4552
assert S.arr5[0] == S.arr7[0].l
4653

54+
# assign int8
55+
S.arr8[0] = 0x11
56+
print(hex(S.arr8[0]))
57+
assert hex(S.arr8[0]) == "0x11"
58+
59+
# assign int16
60+
S.arr9[0] = 0x1122
61+
print(hex(S.arr9[0]))
62+
assert hex(S.arr9[0]) == "0x1122"
63+
64+
# assign int32
65+
S.arr10[0] = 0x11223344
66+
print(hex(S.arr10[0]))
67+
assert hex(S.arr10[0]) == "0x11223344"
68+
69+
# assign int64
70+
S.arr11[0] = 0x11223344
71+
print(hex(S.arr11[0]))
72+
assert hex(S.arr11[0]) == "0x11223344"
73+
74+
# assign uint64
75+
S.arr12[0] = 0x11223344
76+
print(hex(S.arr12[0]))
77+
assert hex(S.arr12[0]) == "0x11223344"
78+
79+
# index out of range
80+
try:
81+
print(S.arr8[2])
82+
except IndexError:
83+
print("IndexError")
84+
85+
# syntax error in descriptor
86+
try:
87+
S.arr13[0].l = 0x11
88+
except TypeError:
89+
print("TypeError")
90+
91+
# operation not supported
92+
try:
93+
S.arr13[0] = 0x11
94+
except TypeError:
95+
print("TypeError")

tests/extmod/uctypes_array_assign_native_le.py.exp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@
33
0x4455
44
0x66778899
55
True
6+
0x11
7+
0x1122
8+
0x11223344
9+
0x11223344
10+
0x11223344
11+
IndexError
12+
TypeError
13+
TypeError

tests/extmod/uctypes_le.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,22 @@
6666
S.bf3 = 5
6767
print(data)
6868
assert bytes(data) == b"2Q"
69+
70+
desc2 = {
71+
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
72+
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN
73+
}
74+
75+
data2 = bytearray(b"0123")
76+
77+
S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.LITTLE_ENDIAN)
78+
79+
# bitfield using uint8 as base type
80+
S2.bf8 = 5
81+
print(data2)
82+
assert bytes(data2) == b"5123"
83+
84+
# bitfield using uint32 as base type
85+
S2.bf32 = 5
86+
print(data2)
87+
assert bytes(data2) == b"51R3"

tests/extmod/uctypes_le.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ bf: 48 49
88
bf 4bit: 3 1 3 0
99
bytearray(b'21')
1010
bytearray(b'2Q')
11+
bytearray(b'5123')
12+
bytearray(b'51R3')

tests/extmod/uctypes_native_le.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,22 @@
7474
S.bf3 = 5
7575
print(data)
7676
assert bytes(data) == b"2Q"
77+
78+
desc2 = {
79+
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
80+
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN
81+
}
82+
83+
data2 = bytearray(b"0123")
84+
85+
S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.NATIVE)
86+
87+
# bitfield using uint8 as base type
88+
S2.bf8 = 5
89+
print(data2)
90+
assert bytes(data2) == b"5123"
91+
92+
# bitfield using uint32 as base type
93+
S2.bf32 = 5
94+
print(data2)
95+
assert bytes(data2) == b"51R3"

tests/extmod/uctypes_native_le.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ bf: 48 49
88
bf 4bit: 3 1 3 0
99
bytearray(b'21')
1010
bytearray(b'2Q')
11+
bytearray(b'5123')
12+
bytearray(b'51R3')

tests/extmod/uctypes_print.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# test printing of uctypes objects
2+
3+
import uctypes
4+
5+
# we use an address of "0" because we just want to print something deterministic
6+
# and don't actually need to set/get any values in the struct
7+
8+
desc = {"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 1)}
9+
S = uctypes.struct(0, desc)
10+
print(S)
11+
12+
desc2 = [(uctypes.ARRAY | 0, uctypes.UINT8 | 1)]
13+
S2 = uctypes.struct(0, desc2)
14+
print(S2)
15+
16+
desc3 = ((uctypes.ARRAY | 0, uctypes.UINT8 | 1))
17+
S3 = uctypes.struct(0, desc3)
18+
print(S3)
19+
20+
desc4 = ((uctypes.PTR | 0, uctypes.UINT8 | 1))
21+
S4 = uctypes.struct(0, desc4)
22+
print(S4)

tests/extmod/uctypes_print.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<struct STRUCT 0>
2+
<struct ERROR 0>
3+
<struct ARRAY 0>
4+
<struct PTR 0>

0 commit comments

Comments
 (0)