From d22762017e3acc1d9350102bf914f5d5b3f21029 Mon Sep 17 00:00:00 2001 From: Rami Ali Date: Mon, 12 Dec 2016 16:12:42 +1100 Subject: [PATCH] tests/extmod: Improve moductypes test coverage. --- .../extmod/uctypes_array_assign_native_le.py | 51 ++++++++++++++++++- .../uctypes_array_assign_native_le.py.exp | 8 +++ tests/extmod/uctypes_le.py | 19 +++++++ tests/extmod/uctypes_le.py.exp | 2 + tests/extmod/uctypes_native_le.py | 19 +++++++ tests/extmod/uctypes_native_le.py.exp | 2 + tests/extmod/uctypes_print.py | 22 ++++++++ tests/extmod/uctypes_print.py.exp | 4 ++ 8 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 tests/extmod/uctypes_print.py create mode 100644 tests/extmod/uctypes_print.py.exp diff --git a/tests/extmod/uctypes_array_assign_native_le.py b/tests/extmod/uctypes_array_assign_native_le.py index 6901654dd8..632c4d252b 100644 --- a/tests/extmod/uctypes_array_assign_native_le.py +++ b/tests/extmod/uctypes_array_assign_native_le.py @@ -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") diff --git a/tests/extmod/uctypes_array_assign_native_le.py.exp b/tests/extmod/uctypes_array_assign_native_le.py.exp index 0c3dc50fb0..4efcdec668 100644 --- a/tests/extmod/uctypes_array_assign_native_le.py.exp +++ b/tests/extmod/uctypes_array_assign_native_le.py.exp @@ -3,3 +3,11 @@ 0x4455 0x66778899 True +0x11 +0x1122 +0x11223344 +0x11223344 +0x11223344 +IndexError +TypeError +TypeError diff --git a/tests/extmod/uctypes_le.py b/tests/extmod/uctypes_le.py index ff499476f9..5ae410b011 100644 --- a/tests/extmod/uctypes_le.py +++ b/tests/extmod/uctypes_le.py @@ -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" diff --git a/tests/extmod/uctypes_le.py.exp b/tests/extmod/uctypes_le.py.exp index 8efd7a6ea0..2598b4eabd 100644 --- a/tests/extmod/uctypes_le.py.exp +++ b/tests/extmod/uctypes_le.py.exp @@ -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') diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py index a053b68d55..ef0f9f5e95 100644 --- a/tests/extmod/uctypes_native_le.py +++ b/tests/extmod/uctypes_native_le.py @@ -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" diff --git a/tests/extmod/uctypes_native_le.py.exp b/tests/extmod/uctypes_native_le.py.exp index 8efd7a6ea0..2598b4eabd 100644 --- a/tests/extmod/uctypes_native_le.py.exp +++ b/tests/extmod/uctypes_native_le.py.exp @@ -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') diff --git a/tests/extmod/uctypes_print.py b/tests/extmod/uctypes_print.py new file mode 100644 index 0000000000..71981ce7ea --- /dev/null +++ b/tests/extmod/uctypes_print.py @@ -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) diff --git a/tests/extmod/uctypes_print.py.exp b/tests/extmod/uctypes_print.py.exp new file mode 100644 index 0000000000..63daefc848 --- /dev/null +++ b/tests/extmod/uctypes_print.py.exp @@ -0,0 +1,4 @@ + + + +