Commit d2276201 authored by Rami Ali's avatar Rami Ali Committed by Damien George
Browse files

tests/extmod: Improve moductypes test coverage.

parent a3c61004
......@@ -15,9 +15,16 @@ desc = {
# aligned
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
"arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1),
"arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1),
"arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1),
"arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1),
"arr12": (uctypes.ARRAY | 0, uctypes.UINT64| 1),
"arr13": (uctypes.ARRAY | 1, 1, {"l": {}}),
}
data = bytearray(5)
data = bytearray(8)
S = uctypes.struct(uctypes.addressof(data), desc)
......@@ -44,3 +51,45 @@ assert hex(S.arr5[0]) == "0x66778899"
print(S.arr5[0] == S.arr7[0].l)
assert S.arr5[0] == S.arr7[0].l
# assign int8
S.arr8[0] = 0x11
print(hex(S.arr8[0]))
assert hex(S.arr8[0]) == "0x11"
# assign int16
S.arr9[0] = 0x1122
print(hex(S.arr9[0]))
assert hex(S.arr9[0]) == "0x1122"
# assign int32
S.arr10[0] = 0x11223344
print(hex(S.arr10[0]))
assert hex(S.arr10[0]) == "0x11223344"
# assign int64
S.arr11[0] = 0x11223344
print(hex(S.arr11[0]))
assert hex(S.arr11[0]) == "0x11223344"
# assign uint64
S.arr12[0] = 0x11223344
print(hex(S.arr12[0]))
assert hex(S.arr12[0]) == "0x11223344"
# index out of range
try:
print(S.arr8[2])
except IndexError:
print("IndexError")
# syntax error in descriptor
try:
S.arr13[0].l = 0x11
except TypeError:
print("TypeError")
# operation not supported
try:
S.arr13[0] = 0x11
except TypeError:
print("TypeError")
......@@ -3,3 +3,11 @@
0x4455
0x66778899
True
0x11
0x1122
0x11223344
0x11223344
0x11223344
IndexError
TypeError
TypeError
......@@ -66,3 +66,22 @@ assert bytes(data) == b"21"
S.bf3 = 5
print(data)
assert bytes(data) == b"2Q"
desc2 = {
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN
}
data2 = bytearray(b"0123")
S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.LITTLE_ENDIAN)
# bitfield using uint8 as base type
S2.bf8 = 5
print(data2)
assert bytes(data2) == b"5123"
# bitfield using uint32 as base type
S2.bf32 = 5
print(data2)
assert bytes(data2) == b"51R3"
......@@ -8,3 +8,5 @@ bf: 48 49
bf 4bit: 3 1 3 0
bytearray(b'21')
bytearray(b'2Q')
bytearray(b'5123')
bytearray(b'51R3')
......@@ -74,3 +74,22 @@ assert bytes(data) == b"21"
S.bf3 = 5
print(data)
assert bytes(data) == b"2Q"
desc2 = {
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN
}
data2 = bytearray(b"0123")
S2 = uctypes.struct(uctypes.addressof(data2), desc2, uctypes.NATIVE)
# bitfield using uint8 as base type
S2.bf8 = 5
print(data2)
assert bytes(data2) == b"5123"
# bitfield using uint32 as base type
S2.bf32 = 5
print(data2)
assert bytes(data2) == b"51R3"
......@@ -8,3 +8,5 @@ bf: 48 49
bf 4bit: 3 1 3 0
bytearray(b'21')
bytearray(b'2Q')
bytearray(b'5123')
bytearray(b'51R3')
# test printing of uctypes objects
import uctypes
# we use an address of "0" because we just want to print something deterministic
# and don't actually need to set/get any values in the struct
desc = {"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 1)}
S = uctypes.struct(0, desc)
print(S)
desc2 = [(uctypes.ARRAY | 0, uctypes.UINT8 | 1)]
S2 = uctypes.struct(0, desc2)
print(S2)
desc3 = ((uctypes.ARRAY | 0, uctypes.UINT8 | 1))
S3 = uctypes.struct(0, desc3)
print(S3)
desc4 = ((uctypes.PTR | 0, uctypes.UINT8 | 1))
S4 = uctypes.struct(0, desc4)
print(S4)
<struct STRUCT 0>
<struct ERROR 0>
<struct ARRAY 0>
<struct PTR 0>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment