From d35c6ffc84e94dca370479a057eed60b9b347c1b Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 19 Dec 2017 16:48:41 +1100 Subject: [PATCH] tests/extmod: Add some uctypes tests to improve coverage of that module. --- tests/extmod/uctypes_bytearray.py | 3 ++ tests/extmod/uctypes_bytearray.py.exp | 1 + tests/extmod/uctypes_byteat.py | 10 +++++++ tests/extmod/uctypes_byteat.py.exp | 2 ++ tests/extmod/uctypes_error.py | 37 ++++++++++++++++++++++++ tests/extmod/uctypes_error.py.exp | 4 +++ tests/extmod/uctypes_sizeof.py | 5 ++++ tests/extmod/uctypes_sizeof.py.exp | 1 + tests/extmod/uctypes_sizeof_float.py | 8 +++++ tests/extmod/uctypes_sizeof_float.py.exp | 2 ++ 10 files changed, 73 insertions(+) create mode 100644 tests/extmod/uctypes_byteat.py create mode 100644 tests/extmod/uctypes_byteat.py.exp create mode 100644 tests/extmod/uctypes_error.py create mode 100644 tests/extmod/uctypes_error.py.exp create mode 100644 tests/extmod/uctypes_sizeof_float.py create mode 100644 tests/extmod/uctypes_sizeof_float.py.exp diff --git a/tests/extmod/uctypes_bytearray.py b/tests/extmod/uctypes_bytearray.py index 61c7da271f..77c93c3766 100644 --- a/tests/extmod/uctypes_bytearray.py +++ b/tests/extmod/uctypes_bytearray.py @@ -17,3 +17,6 @@ S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) print(S.arr) # But not INT8, because value range is different print(type(S.arr2)) + +# convert to buffer +print(bytearray(S)) diff --git a/tests/extmod/uctypes_bytearray.py.exp b/tests/extmod/uctypes_bytearray.py.exp index 294f8a5fa4..7c84edbb6d 100644 --- a/tests/extmod/uctypes_bytearray.py.exp +++ b/tests/extmod/uctypes_bytearray.py.exp @@ -1,2 +1,3 @@ bytearray(b'01') +bytearray(b'0123') diff --git a/tests/extmod/uctypes_byteat.py b/tests/extmod/uctypes_byteat.py new file mode 100644 index 0000000000..ab2535db8f --- /dev/null +++ b/tests/extmod/uctypes_byteat.py @@ -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)) diff --git a/tests/extmod/uctypes_byteat.py.exp b/tests/extmod/uctypes_byteat.py.exp new file mode 100644 index 0000000000..e1ae4d0534 --- /dev/null +++ b/tests/extmod/uctypes_byteat.py.exp @@ -0,0 +1,2 @@ +b'0123' +bytearray(b'0123') diff --git a/tests/extmod/uctypes_error.py b/tests/extmod/uctypes_error.py new file mode 100644 index 0000000000..95ba0fad44 --- /dev/null +++ b/tests/extmod/uctypes_error.py @@ -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') diff --git a/tests/extmod/uctypes_error.py.exp b/tests/extmod/uctypes_error.py.exp new file mode 100644 index 0000000000..802c260d2b --- /dev/null +++ b/tests/extmod/uctypes_error.py.exp @@ -0,0 +1,4 @@ +TypeError +TypeError +TypeError +TypeError diff --git a/tests/extmod/uctypes_sizeof.py b/tests/extmod/uctypes_sizeof.py index 5a6adb4376..e42e06c924 100644 --- a/tests/extmod/uctypes_sizeof.py +++ b/tests/extmod/uctypes_sizeof.py @@ -40,3 +40,8 @@ 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.py.exp b/tests/extmod/uctypes_sizeof.py.exp index fb74def602..b35b11aa0c 100644 --- a/tests/extmod/uctypes_sizeof.py.exp +++ b/tests/extmod/uctypes_sizeof.py.exp @@ -4,3 +4,4 @@ TypeError 6 1 +TypeError diff --git a/tests/extmod/uctypes_sizeof_float.py b/tests/extmod/uctypes_sizeof_float.py new file mode 100644 index 0000000000..1ba8871bdc --- /dev/null +++ b/tests/extmod/uctypes_sizeof_float.py @@ -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})) diff --git a/tests/extmod/uctypes_sizeof_float.py.exp b/tests/extmod/uctypes_sizeof_float.py.exp new file mode 100644 index 0000000000..de78180725 --- /dev/null +++ b/tests/extmod/uctypes_sizeof_float.py.exp @@ -0,0 +1,2 @@ +4 +8