summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2018-08-26 08:53:29 +0300
committerDamien George <damien.p.george@gmail.com>2018-10-13 16:08:25 +1100
commit7059b4af6d651a819daf8b6ea838ae82f62287f0 (patch)
tree1e95a683275f9ea601fc74728e988a8b9a43dd18
parent9fbd12f2faae97ca29a6f3fe514f6216656ed3c7 (diff)
downloadmicropython-7059b4af6d651a819daf8b6ea838ae82f62287f0.tar.gz
micropython-7059b4af6d651a819daf8b6ea838ae82f62287f0.zip
tests/uctypes_sizeof_od: Test for using OrderedDict as struct descriptor
Just a copy of uctypes_sizeof.py with minimal changes.
-rw-r--r--tests/extmod/uctypes_sizeof_od.py48
-rw-r--r--tests/extmod/uctypes_sizeof_od.py.exp7
2 files changed, 55 insertions, 0 deletions
diff --git a/tests/extmod/uctypes_sizeof_od.py b/tests/extmod/uctypes_sizeof_od.py
new file mode 100644
index 0000000000..192ee91528
--- /dev/null
+++ b/tests/extmod/uctypes_sizeof_od.py
@@ -0,0 +1,48 @@
+try:
+ from ucollections import OrderedDict
+ import uctypes
+except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+desc = OrderedDict({
+ # arr is array at offset 0, of UINT8 elements, array size is 2
+ "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
+ # arr2 is array at offset 0, size 2, of structures defined recursively
+ "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
+ "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
+ "arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
+ "sub": (0, {
+ 'b1': uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
+ 'b2': uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
+ }),
+})
+
+data = bytearray(b"01234567")
+
+S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
+
+print(uctypes.sizeof(S.arr))
+assert uctypes.sizeof(S.arr) == 2
+
+print(uctypes.sizeof(S.arr2))
+assert uctypes.sizeof(S.arr2) == 2
+
+print(uctypes.sizeof(S.arr3))
+
+try:
+ print(uctypes.sizeof(S.arr3[0]))
+except TypeError:
+ print("TypeError")
+
+print(uctypes.sizeof(S.arr4))
+assert uctypes.sizeof(S.arr4) == 6
+
+print(uctypes.sizeof(S.sub))
+assert uctypes.sizeof(S.sub) == 1
+
+# invalid descriptor
+try:
+ print(uctypes.sizeof([]))
+except TypeError:
+ print("TypeError")
diff --git a/tests/extmod/uctypes_sizeof_od.py.exp b/tests/extmod/uctypes_sizeof_od.py.exp
new file mode 100644
index 0000000000..b35b11aa0c
--- /dev/null
+++ b/tests/extmod/uctypes_sizeof_od.py.exp
@@ -0,0 +1,7 @@
+2
+2
+4
+TypeError
+6
+1
+TypeError