diff options
| author | stijn <stijn@ignitron.net> | 2021-05-12 17:02:06 +0200 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-05-13 22:16:14 +1000 |
| commit | 57365d855734142deb030ebcd00c10efcedf554b (patch) | |
| tree | d121b5fc9be3e172649d5c91eba81a559f183175 /tests/basics | |
| parent | 6affcb0104336e629390b2bfba997f3446f9ea58 (diff) | |
py/objarray: Prohibit comparison of mismatching types.
Array equality is defined as each element being equal but to keep
code size down MicroPython implements a binary comparison. This
can only be used correctly for elements with the same binary layout
though so turn it into an NotImplementedError when comparing types
for which the binary comparison yielded incorrect results: types
with different sizes, and floating point numbers because nan != nan.
Diffstat (limited to 'tests/basics')
| -rw-r--r-- | tests/basics/array1.py | 9 | ||||
| -rw-r--r-- | tests/basics/array_micropython.py | 12 |
2 files changed, 21 insertions, 0 deletions
diff --git a/tests/basics/array1.py b/tests/basics/array1.py index 5b3f47578..15789e2c9 100644 --- a/tests/basics/array1.py +++ b/tests/basics/array1.py @@ -41,14 +41,23 @@ except ValueError: # equality (CPython requires both sides are array) print(bytes(array.array('b', [0x61, 0x62, 0x63])) == b'abc') print(array.array('b', [0x61, 0x62, 0x63]) == b'abc') +print(array.array('B', [0x61, 0x62, 0x63]) == b'abc') print(array.array('b', [0x61, 0x62, 0x63]) != b'abc') print(array.array('b', [0x61, 0x62, 0x63]) == b'xyz') print(array.array('b', [0x61, 0x62, 0x63]) != b'xyz') print(b'abc' == array.array('b', [0x61, 0x62, 0x63])) +print(b'abc' == array.array('B', [0x61, 0x62, 0x63])) print(b'abc' != array.array('b', [0x61, 0x62, 0x63])) print(b'xyz' == array.array('b', [0x61, 0x62, 0x63])) print(b'xyz' != array.array('b', [0x61, 0x62, 0x63])) +compatible_typecodes = [] +for t in ["b", "h", "i", "l", "q"]: + compatible_typecodes.append((t, t)) + compatible_typecodes.append((t, t.upper())) +for a, b in compatible_typecodes: + print(array.array(a, [1, 2]) == array.array(b, [1, 2])) + class X(array.array): pass diff --git a/tests/basics/array_micropython.py b/tests/basics/array_micropython.py index 6b3dc7a93..44dc1d83d 100644 --- a/tests/basics/array_micropython.py +++ b/tests/basics/array_micropython.py @@ -17,3 +17,15 @@ print(a[0]) a = array.array('P') a.append(1) print(a[0]) + +# comparison between mismatching binary layouts is not implemented +typecodes = ["b", "h", "i", "l", "q", "P", "O", "S", "f", "d"] +for a in typecodes: + for b in typecodes: + if a == b and a not in ["f", "d"]: + continue + try: + array.array(a) == array.array(b) + print('FAIL') + except NotImplementedError: + pass |
