Skip to content

Commit b50030b

Browse files
Anton-2pfalcon
authored andcommitted
tests/uctypes: Test item assignment for scalar arrays.
1 parent 26ed001 commit b50030b

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import uctypes
2+
3+
desc = {
4+
# arr is array at offset 0, of UINT8 elements, array size is 2
5+
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
6+
# arr2 is array at offset 0, size 2, of structures defined recursively
7+
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
8+
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
9+
10+
# aligned
11+
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
12+
# unaligned
13+
"arr6": (uctypes.ARRAY | 1, uctypes.UINT32 | 1),
14+
15+
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
16+
"arr8": (uctypes.ARRAY | 1, 1, {"l": uctypes.UINT32 | 0})
17+
}
18+
19+
data = bytearray(5)
20+
21+
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
22+
23+
# assign byte
24+
S.arr[0] = 0x11
25+
print(hex(S.arr[0]))
26+
assert hex(S.arr[0]) == "0x11"
27+
28+
# assign word
29+
S.arr3[0] = 0x2233
30+
print(hex(S.arr3[0]))
31+
assert hex(S.arr3[0]) == "0x2233"
32+
33+
# assign word, with index
34+
S.arr3[1] = 0x4455
35+
print(hex(S.arr3[1]))
36+
assert hex(S.arr3[1]) == "0x4455"
37+
38+
# assign long, aligned
39+
S.arr5[0] = 0x66778899
40+
print(hex(S.arr5[0]))
41+
assert hex(S.arr5[0]) == "0x66778899"
42+
43+
print(S.arr5[0] == S.arr7[0].l)
44+
assert S.arr5[0] == S.arr7[0].l
45+
46+
47+
# assign long, unaligned
48+
S.arr6[0] = 0xAABBCCDD
49+
print(hex(S.arr6[0]))
50+
assert hex(S.arr6[0]) == "0xaabbccdd"
51+
52+
print(S.arr6[0] == S.arr8[0].l)
53+
assert S.arr6[0] == S.arr8[0].l
54+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
0x11
2+
0x2233
3+
0x4455
4+
0x66778899
5+
True
6+
0xaabbccdd
7+
True
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
import uctypes
3+
4+
if sys.byteorder != "little":
5+
print("SKIP")
6+
sys.exit()
7+
8+
desc = {
9+
# arr is array at offset 0, of UINT8 elements, array size is 2
10+
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
11+
# arr2 is array at offset 0, size 2, of structures defined recursively
12+
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
13+
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
14+
15+
# aligned
16+
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
17+
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
18+
}
19+
20+
data = bytearray(5)
21+
22+
S = uctypes.struct(uctypes.addressof(data), desc)
23+
24+
# assign byte
25+
S.arr[0] = 0x11
26+
print(hex(S.arr[0]))
27+
assert hex(S.arr[0]) == "0x11"
28+
29+
# assign word
30+
S.arr3[0] = 0x2233
31+
print(hex(S.arr3[0]))
32+
assert hex(S.arr3[0]) == "0x2233"
33+
34+
# assign word, with index
35+
S.arr3[1] = 0x4455
36+
print(hex(S.arr3[1]))
37+
assert hex(S.arr3[1]) == "0x4455"
38+
39+
# assign long, aligned
40+
S.arr5[0] = 0x66778899
41+
print(hex(S.arr5[0]))
42+
assert hex(S.arr5[0]) == "0x66778899"
43+
44+
print(S.arr5[0] == S.arr7[0].l)
45+
assert S.arr5[0] == S.arr7[0].l
46+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0x11
2+
0x2233
3+
0x4455
4+
0x66778899
5+
True

0 commit comments

Comments
 (0)