diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-06-28 03:03:47 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-07-09 19:28:24 +0300 |
commit | 8215847b4d3bbbf859893db44f6de8a9fdea9f35 (patch) | |
tree | 7d8bbf9f219e5ccbda0693b03f335b58d6bd0cf8 /tests | |
parent | 42b6419056a67a0ea8e28eaf27e51f53bc65eec2 (diff) | |
download | micropython-8215847b4d3bbbf859893db44f6de8a9fdea9f35.tar.gz micropython-8215847b4d3bbbf859893db44f6de8a9fdea9f35.zip |
moductypes: Foreign data interface module, roughly based on ctype ideas.
But much smaller and memory-efficient. Uses Python builtin data structures
(dict, tuple, int) to describe structure layout.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/extmod/uctypes_le.py | 68 | ||||
-rw-r--r-- | tests/extmod/uctypes_le.py.exp | 10 | ||||
-rw-r--r-- | tests/extmod/uctypes_native_le.py | 76 | ||||
-rw-r--r-- | tests/extmod/uctypes_native_le.py.exp | 10 | ||||
-rw-r--r-- | tests/extmod/uctypes_ptr_le.py | 25 | ||||
-rw-r--r-- | tests/extmod/uctypes_ptr_le.py.exp | 6 | ||||
-rw-r--r-- | tests/extmod/uctypes_ptr_native_le.py | 31 | ||||
-rw-r--r-- | tests/extmod/uctypes_ptr_native_le.py.exp | 6 | ||||
-rw-r--r-- | tests/extmod/uctypes_sizeof_native.py | 53 | ||||
-rw-r--r-- | tests/extmod/uctypes_sizeof_native.py.exp | 0 |
10 files changed, 285 insertions, 0 deletions
diff --git a/tests/extmod/uctypes_le.py b/tests/extmod/uctypes_le.py new file mode 100644 index 0000000000..0e3bd9a82d --- /dev/null +++ b/tests/extmod/uctypes_le.py @@ -0,0 +1,68 @@ +import uctypes + +desc = { + "s0": uctypes.UINT16 | 0, + "sub": (0, { + "b0": uctypes.UINT8 | 0, + "b1": uctypes.UINT8 | 1, + }), + "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), + "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), + "bitf0": uctypes.BFUINT16 | 0 | 0 << 17 | 8 << 22, + "bitf1": uctypes.BFUINT16 | 0 | 8 << 17 | 8 << 22, + + "bf0": uctypes.BFUINT16 | 0 | 0 << 17 | 4 << 22, + "bf1": uctypes.BFUINT16 | 0 | 4 << 17 | 4 << 22, + "bf2": uctypes.BFUINT16 | 0 | 8 << 17 | 4 << 22, + "bf3": uctypes.BFUINT16 | 0 | 12 << 17 | 4 << 22, + + "ptr": (uctypes.PTR | 0, uctypes.UINT8), + "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), +} + +data = bytearray(b"01") + +S = uctypes.struct(desc, uctypes.addressof(data), uctypes.LITTLE_ENDIAN) + +#print(S) +print(hex(S.s0)) +assert hex(S.s0) == "0x3130" +#print(S.sub.b0) +print(S.sub.b0, S.sub.b1) +assert S.sub.b0, S.sub.b1 == (0x30, 0x31) + +try: + S[0] + assert False, "Can't index struct" +except TypeError: + print("TypeError") + +print("arr:", S.arr[0], S.arr[1]) +assert (S.arr[0], S.arr[1]) == (0x30, 0x31) + +print("arr of struct:", S.arr2[0].b, S.arr2[1].b) +assert (S.arr2[0].b, S.arr2[1].b) == (0x30, 0x31) + + +try: + S.arr[2] + assert False, "Out of bounds index" +except IndexError: + print("IndexError") + +print("bf:", S.bitf0, S.bitf1) +assert (S.bitf0, S.bitf1) == (0x30, 0x31) + +print("bf 4bit:", S.bf3, S.bf2, S.bf1, S.bf0) +assert (S.bf3, S.bf2, S.bf1, S.bf0) == (3, 1, 3, 0) + + +# Write access + +S.sub.b0 = ord("2") +print(data) +assert bytes(data) == b"21" + +S.bf3 = 5 +print(data) +assert bytes(data) == b"2Q" diff --git a/tests/extmod/uctypes_le.py.exp b/tests/extmod/uctypes_le.py.exp new file mode 100644 index 0000000000..8efd7a6ea0 --- /dev/null +++ b/tests/extmod/uctypes_le.py.exp @@ -0,0 +1,10 @@ +0x3130 +48 49 +TypeError +arr: 48 49 +arr of struct: 48 49 +IndexError +bf: 48 49 +bf 4bit: 3 1 3 0 +bytearray(b'21') +bytearray(b'2Q') diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py new file mode 100644 index 0000000000..7a5e38733a --- /dev/null +++ b/tests/extmod/uctypes_native_le.py @@ -0,0 +1,76 @@ +# This test is exactly like uctypes_le.py, but uses native structure layout. +# Codepaths for packed vs native structures are different. This test only works +# on little-endian machine (no matter if 32 or 64 bit). +import sys +import uctypes + +if sys.byteorder != "little": + print("SKIP") + sys.exit() + + +desc = { + "s0": uctypes.UINT16 | 0, + "sub": (0, { + "b0": uctypes.UINT8 | 0, + "b1": uctypes.UINT8 | 1, + }), + "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), + "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), + "bitf0": uctypes.BFUINT16 | 0 | 0 << 17 | 8 << 22, + "bitf1": uctypes.BFUINT16 | 0 | 8 << 17 | 8 << 22, + + "bf0": uctypes.BFUINT16 | 0 | 0 << 17 | 4 << 22, + "bf1": uctypes.BFUINT16 | 0 | 4 << 17 | 4 << 22, + "bf2": uctypes.BFUINT16 | 0 | 8 << 17 | 4 << 22, + "bf3": uctypes.BFUINT16 | 0 | 12 << 17 | 4 << 22, + + "ptr": (uctypes.PTR | 0, uctypes.UINT8), + "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), +} + +data = bytearray(b"01") + +S = uctypes.struct(desc, uctypes.addressof(data), uctypes.NATIVE) + +#print(S) +print(hex(S.s0)) +assert hex(S.s0) == "0x3130" +#print(S.sub.b0) +print(S.sub.b0, S.sub.b1) +assert S.sub.b0, S.sub.b1 == (0x30, 0x31) + +try: + S[0] + assert False, "Can't index struct" +except TypeError: + print("TypeError") + +print("arr:", S.arr[0], S.arr[1]) +assert (S.arr[0], S.arr[1]) == (0x30, 0x31) + +print("arr of struct:", S.arr2[0].b, S.arr2[1].b) +assert (S.arr2[0].b, S.arr2[1].b) == (0x30, 0x31) + + +try: + S.arr[2] + assert False, "Out of bounds index" +except IndexError: + print("IndexError") + +print("bf:", S.bitf0, S.bitf1) +assert (S.bitf0, S.bitf1) == (0x30, 0x31) + +print("bf 4bit:", S.bf3, S.bf2, S.bf1, S.bf0) +assert (S.bf3, S.bf2, S.bf1, S.bf0) == (3, 1, 3, 0) + +# Write access + +S.sub.b0 = ord("2") +print(data) +assert bytes(data) == b"21" + +S.bf3 = 5 +print(data) +assert bytes(data) == b"2Q" diff --git a/tests/extmod/uctypes_native_le.py.exp b/tests/extmod/uctypes_native_le.py.exp new file mode 100644 index 0000000000..8efd7a6ea0 --- /dev/null +++ b/tests/extmod/uctypes_native_le.py.exp @@ -0,0 +1,10 @@ +0x3130 +48 49 +TypeError +arr: 48 49 +arr of struct: 48 49 +IndexError +bf: 48 49 +bf 4bit: 3 1 3 0 +bytearray(b'21') +bytearray(b'2Q') diff --git a/tests/extmod/uctypes_ptr_le.py b/tests/extmod/uctypes_ptr_le.py new file mode 100644 index 0000000000..ef9890c3f1 --- /dev/null +++ b/tests/extmod/uctypes_ptr_le.py @@ -0,0 +1,25 @@ +import uctypes + +desc = { + "ptr": (uctypes.PTR | 0, uctypes.UINT8), + "ptr16": (uctypes.PTR | 0, uctypes.UINT16), + "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), +} + +bytes = b"01" + +addr = uctypes.addressof(bytes) +buf = addr.to_bytes(4) + +S = uctypes.struct(desc, uctypes.addressof(buf), uctypes.LITTLE_ENDIAN) + +print(S.ptr[0]) +assert S.ptr[0] == ord("0") +print(S.ptr[1]) +assert S.ptr[1] == ord("1") +print(hex(S.ptr16[0])) +assert hex(S.ptr16[0]) == "0x3130" +print(S.ptr2[0].b, S.ptr2[1].b) +print (S.ptr2[0].b, S.ptr2[1].b) +print(hex(S.ptr16[0])) +assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49) diff --git a/tests/extmod/uctypes_ptr_le.py.exp b/tests/extmod/uctypes_ptr_le.py.exp new file mode 100644 index 0000000000..30d159edd1 --- /dev/null +++ b/tests/extmod/uctypes_ptr_le.py.exp @@ -0,0 +1,6 @@ +48 +49 +0x3130 +48 49 +48 49 +0x3130 diff --git a/tests/extmod/uctypes_ptr_native_le.py b/tests/extmod/uctypes_ptr_native_le.py new file mode 100644 index 0000000000..139ed3280c --- /dev/null +++ b/tests/extmod/uctypes_ptr_native_le.py @@ -0,0 +1,31 @@ +import sys +import uctypes + +if sys.byteorder != "little": + print("SKIP") + sys.exit() + + +desc = { + "ptr": (uctypes.PTR | 0, uctypes.UINT8), + "ptr16": (uctypes.PTR | 0, uctypes.UINT16), + "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), +} + +bytes = b"01" + +addr = uctypes.addressof(bytes) +buf = addr.to_bytes(4) + +S = uctypes.struct(desc, uctypes.addressof(buf), uctypes.NATIVE) + +print(S.ptr[0]) +assert S.ptr[0] == ord("0") +print(S.ptr[1]) +assert S.ptr[1] == ord("1") +print(hex(S.ptr16[0])) +assert hex(S.ptr16[0]) == "0x3130" +print(S.ptr2[0].b, S.ptr2[1].b) +print (S.ptr2[0].b, S.ptr2[1].b) +print(hex(S.ptr16[0])) +assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49) diff --git a/tests/extmod/uctypes_ptr_native_le.py.exp b/tests/extmod/uctypes_ptr_native_le.py.exp new file mode 100644 index 0000000000..30d159edd1 --- /dev/null +++ b/tests/extmod/uctypes_ptr_native_le.py.exp @@ -0,0 +1,6 @@ +48 +49 +0x3130 +48 49 +48 49 +0x3130 diff --git a/tests/extmod/uctypes_sizeof_native.py b/tests/extmod/uctypes_sizeof_native.py new file mode 100644 index 0000000000..0dfbfa980a --- /dev/null +++ b/tests/extmod/uctypes_sizeof_native.py @@ -0,0 +1,53 @@ +import uctypes + +S1 = {} +assert uctypes.sizeof(S1) == 0 + +S2 = {"a": uctypes.UINT8 | 0} +assert uctypes.sizeof(S2) == 1 + +S3 = { + "a": uctypes.UINT8 | 0, + "b": uctypes.UINT8 | 1, +} +assert uctypes.sizeof(S3) == 2 + +S4 = { + "a": uctypes.UINT8 | 0, + "b": uctypes.UINT32 | 4, + "c": uctypes.UINT8 | 8, +} +assert uctypes.sizeof(S4) == 12 + +S5 = { + "a": uctypes.UINT8 | 0, + "b": uctypes.UINT32 | 4, + "c": uctypes.UINT8 | 8, + "d": uctypes.UINT32 | 0, + "sub": (4, { + "b0": uctypes.UINT8 | 0, + "b1": uctypes.UINT8 | 1, + }), +} + +assert uctypes.sizeof(S5) == 12 + +s5 = uctypes.struct(S5, 0) +assert uctypes.sizeof(s5) == 12 +assert uctypes.sizeof(s5.sub) == 2 + +S6 = { + "ptr": (uctypes.PTR | 0, uctypes.UINT8), +} +# As if there're no other arch bitnesses +assert uctypes.sizeof(S6) in (4, 8) + +S7 = { + "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 5), +} +assert uctypes.sizeof(S7) == 5 + +S8 = { + "arr": (uctypes.ARRAY | 0, 3, {"a": uctypes.UINT32 | 0, "b": uctypes.UINT8 | 4}), +} +assert uctypes.sizeof(S8) == 24 diff --git a/tests/extmod/uctypes_sizeof_native.py.exp b/tests/extmod/uctypes_sizeof_native.py.exp new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/extmod/uctypes_sizeof_native.py.exp |