2017-01-06 22:13:40 +00:00
|
|
|
try:
|
2019-10-22 07:33:23 +01:00
|
|
|
import uarray as array
|
2017-01-06 22:13:40 +00:00
|
|
|
except ImportError:
|
2019-10-22 07:33:23 +01:00
|
|
|
try:
|
|
|
|
import array
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
2014-01-28 01:16:37 +00:00
|
|
|
|
|
|
|
a = array.array('B', [1, 2, 3])
|
|
|
|
print(a, len(a))
|
|
|
|
i = array.array('I', [1, 2, 3])
|
|
|
|
print(i, len(i))
|
|
|
|
print(a[0])
|
|
|
|
print(i[-1])
|
2016-09-19 15:20:41 +01:00
|
|
|
a = array.array('l', [-1])
|
|
|
|
print(len(a), a[0])
|
|
|
|
a1 = array.array('l', [1, 2, 3])
|
|
|
|
a2 = array.array('L', [1, 2, 3])
|
|
|
|
print(a2[1])
|
|
|
|
print(a1 == a2)
|
2014-01-28 01:16:37 +00:00
|
|
|
|
|
|
|
# Empty arrays
|
|
|
|
print(len(array.array('h')))
|
|
|
|
print(array.array('i'))
|
|
|
|
|
2015-03-25 23:10:09 +00:00
|
|
|
# bool operator acting on arrays
|
|
|
|
print(bool(array.array('i')))
|
|
|
|
print(bool(array.array('i', [1])))
|
|
|
|
|
2016-10-17 01:43:47 +01:00
|
|
|
# containment, with incorrect type
|
|
|
|
print('12' in array.array('B', b'12'))
|
|
|
|
print([] in array.array('B', b'12'))
|
|
|
|
|
2015-03-25 23:10:09 +00:00
|
|
|
# bad typecode
|
|
|
|
try:
|
|
|
|
array.array('X')
|
|
|
|
except ValueError:
|
|
|
|
print("ValueError")
|
2020-02-20 12:14:02 +00:00
|
|
|
|
|
|
|
# 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')
|
2021-05-12 16:02:06 +01:00
|
|
|
print(array.array('B', [0x61, 0x62, 0x63]) == b'abc')
|
2020-02-20 12:14:02 +00:00
|
|
|
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]))
|
2021-05-12 16:02:06 +01:00
|
|
|
print(b'abc' == array.array('B', [0x61, 0x62, 0x63]))
|
2020-02-20 12:14:02 +00:00
|
|
|
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]))
|
|
|
|
|
2021-05-12 16:02:06 +01:00
|
|
|
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]))
|
|
|
|
|
2020-02-20 12:14:02 +00:00
|
|
|
class X(array.array):
|
|
|
|
pass
|
|
|
|
|
|
|
|
print(bytes(X('b', [0x61, 0x62, 0x63])) == b'abc')
|
|
|
|
print(X('b', [0x61, 0x62, 0x63]) == b'abc')
|
|
|
|
print(X('b', [0x61, 0x62, 0x63]) != b'abc')
|
|
|
|
print(X('b', [0x61, 0x62, 0x63]) == array.array('b', [0x61, 0x62, 0x63]))
|
|
|
|
print(X('b', [0x61, 0x62, 0x63]) != array.array('b', [0x61, 0x62, 0x63]))
|
2021-05-10 15:40:51 +01:00
|
|
|
|
|
|
|
# other comparisons
|
|
|
|
for typecode in ["B", "H", "I", "L", "Q"]:
|
|
|
|
a = array.array(typecode, [1, 1])
|
|
|
|
print(a < a)
|
|
|
|
print(a <= a)
|
|
|
|
print(a > a)
|
|
|
|
print(a >= a)
|
|
|
|
|
|
|
|
al = array.array(typecode, [1, 0])
|
|
|
|
ab = array.array(typecode, [1, 2])
|
|
|
|
|
|
|
|
print(a < al)
|
|
|
|
print(a <= al)
|
|
|
|
print(a > al)
|
|
|
|
print(a >= al)
|
|
|
|
|
|
|
|
print(a < ab)
|
|
|
|
print(a <= ab)
|
|
|
|
print(a > ab)
|
|
|
|
print(a >= ab)
|