tests/extmod: Add some uctypes tests to improve coverage of that module.

This commit is contained in:
Damien George 2017-12-19 16:48:41 +11:00
parent 35a759dc1d
commit d35c6ffc84
10 changed files with 73 additions and 0 deletions

View File

@ -17,3 +17,6 @@ S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
print(S.arr) print(S.arr)
# But not INT8, because value range is different # But not INT8, because value range is different
print(type(S.arr2)) print(type(S.arr2))
# convert to buffer
print(bytearray(S))

View File

@ -1,2 +1,3 @@
bytearray(b'01') bytearray(b'01')
<class 'struct'> <class 'struct'>
bytearray(b'0123')

View File

@ -0,0 +1,10 @@
try:
import uctypes
except ImportError:
print("SKIP")
raise SystemExit
data = bytearray(b'01234567')
print(uctypes.bytes_at(uctypes.addressof(data), 4))
print(uctypes.bytearray_at(uctypes.addressof(data), 4))

View File

@ -0,0 +1,2 @@
b'0123'
bytearray(b'0123')

View File

@ -0,0 +1,37 @@
# test general errors with uctypes
try:
import uctypes
except ImportError:
print("SKIP")
raise SystemExit
data = bytearray(b"01234567")
# del subscr not supported
S = uctypes.struct(uctypes.addressof(data), {})
try:
del S[0]
except TypeError:
print('TypeError')
# list is an invalid descriptor
S = uctypes.struct(uctypes.addressof(data), [])
try:
S.x
except TypeError:
print('TypeError')
# can't access attribute with invalid descriptor
S = uctypes.struct(uctypes.addressof(data), {'x':[]})
try:
S.x
except TypeError:
print('TypeError')
# can't assign to aggregate
S = uctypes.struct(uctypes.addressof(data), {'x':(uctypes.ARRAY | 0, uctypes.INT8 | 2)})
try:
S.x = 1
except TypeError:
print('TypeError')

View File

@ -0,0 +1,4 @@
TypeError
TypeError
TypeError
TypeError

View File

@ -40,3 +40,8 @@ assert uctypes.sizeof(S.arr4) == 6
print(uctypes.sizeof(S.sub)) print(uctypes.sizeof(S.sub))
assert uctypes.sizeof(S.sub) == 1 assert uctypes.sizeof(S.sub) == 1
# invalid descriptor
try:
print(uctypes.sizeof([]))
except TypeError:
print("TypeError")

View File

@ -4,3 +4,4 @@
TypeError TypeError
6 6
1 1
TypeError

View File

@ -0,0 +1,8 @@
try:
import uctypes
except ImportError:
print("SKIP")
raise SystemExit
print(uctypes.sizeof({'f':uctypes.FLOAT32}))
print(uctypes.sizeof({'f':uctypes.FLOAT64}))

View File

@ -0,0 +1,2 @@
4
8