tests/extmod: Improve moductypes test coverage.

This commit is contained in:
Rami Ali 2016-12-12 16:12:42 +11:00 committed by Damien George
parent a3c61004c2
commit d22762017e
8 changed files with 126 additions and 1 deletions

View File

@ -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")

View File

@ -3,3 +3,11 @@
0x4455
0x66778899
True
0x11
0x1122
0x11223344
0x11223344
0x11223344
IndexError
TypeError
TypeError

View File

@ -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"

View File

@ -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')

View File

@ -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"

View File

@ -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')

View File

@ -0,0 +1,22 @@
# 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)

View File

@ -0,0 +1,4 @@
<struct STRUCT 0>
<struct ERROR 0>
<struct ARRAY 0>
<struct PTR 0>