Skip to content

Commit d35c6ff

Browse files
committed
tests/extmod: Add some uctypes tests to improve coverage of that module.
1 parent 35a759d commit d35c6ff

10 files changed

Lines changed: 73 additions & 0 deletions

tests/extmod/uctypes_bytearray.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@
1717
print(S.arr)
1818
# But not INT8, because value range is different
1919
print(type(S.arr2))
20+
21+
# convert to buffer
22+
print(bytearray(S))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
bytearray(b'01')
22
<class 'struct'>
3+
bytearray(b'0123')

tests/extmod/uctypes_byteat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
try:
2+
import uctypes
3+
except ImportError:
4+
print("SKIP")
5+
raise SystemExit
6+
7+
data = bytearray(b'01234567')
8+
9+
print(uctypes.bytes_at(uctypes.addressof(data), 4))
10+
print(uctypes.bytearray_at(uctypes.addressof(data), 4))

tests/extmod/uctypes_byteat.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
b'0123'
2+
bytearray(b'0123')

tests/extmod/uctypes_error.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# test general errors with uctypes
2+
3+
try:
4+
import uctypes
5+
except ImportError:
6+
print("SKIP")
7+
raise SystemExit
8+
9+
data = bytearray(b"01234567")
10+
11+
# del subscr not supported
12+
S = uctypes.struct(uctypes.addressof(data), {})
13+
try:
14+
del S[0]
15+
except TypeError:
16+
print('TypeError')
17+
18+
# list is an invalid descriptor
19+
S = uctypes.struct(uctypes.addressof(data), [])
20+
try:
21+
S.x
22+
except TypeError:
23+
print('TypeError')
24+
25+
# can't access attribute with invalid descriptor
26+
S = uctypes.struct(uctypes.addressof(data), {'x':[]})
27+
try:
28+
S.x
29+
except TypeError:
30+
print('TypeError')
31+
32+
# can't assign to aggregate
33+
S = uctypes.struct(uctypes.addressof(data), {'x':(uctypes.ARRAY | 0, uctypes.INT8 | 2)})
34+
try:
35+
S.x = 1
36+
except TypeError:
37+
print('TypeError')

tests/extmod/uctypes_error.py.exp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TypeError
2+
TypeError
3+
TypeError
4+
TypeError

tests/extmod/uctypes_sizeof.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@
4040
print(uctypes.sizeof(S.sub))
4141
assert uctypes.sizeof(S.sub) == 1
4242

43+
# invalid descriptor
44+
try:
45+
print(uctypes.sizeof([]))
46+
except TypeError:
47+
print("TypeError")

tests/extmod/uctypes_sizeof.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
TypeError
55
6
66
1
7+
TypeError
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
try:
2+
import uctypes
3+
except ImportError:
4+
print("SKIP")
5+
raise SystemExit
6+
7+
print(uctypes.sizeof({'f':uctypes.FLOAT32}))
8+
print(uctypes.sizeof({'f':uctypes.FLOAT64}))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4
2+
8

0 commit comments

Comments
 (0)