tests/basics: Split out memoryview slice-assign tests to separate file.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2020-07-16 23:20:30 +10:00
parent f80b1d8535
commit 895b1dbdda
2 changed files with 63 additions and 48 deletions

View File

@ -54,54 +54,6 @@ print(list(m[1:-1]))
m[2] = 6
print(a)
# test slice assignment between memoryviews
b1 = bytearray(b'1234')
b2 = bytearray(b'5678')
b3 = bytearray(b'5678')
m1 = memoryview(b1)
m2 = memoryview(b2)
m3 = memoryview(b3)
m2[1:3] = m1[0:2]
print(b2)
b3[1:3] = m1[0:2]
print(b3)
m1[2:4] = b3[1:3]
print(b1)
try:
m2[1:3] = b1[0:4]
except ValueError:
print("ValueError")
try:
m2[1:3] = m1[0:4]
except ValueError:
print("ValueError")
try:
m2[0:4] = m1[1:3]
except ValueError:
print("ValueError")
# test memoryview of arrays with items sized larger than 1
a1 = array.array('i', [0]*5)
m4 = memoryview(a1)
a2 = array.array('i', [3]*5)
m5 = memoryview(a2)
m4[1:3] = m5[1:3]
print(a1)
try:
m4[1:3] = m2[1:3]
except ValueError:
print("ValueError")
# invalid assignment on RHS
try:
memoryview(array.array('i'))[0:2] = b'1234'
except ValueError:
print('ValueError')
# invalid attribute
try:
memoryview(b'a').noexist

View File

@ -0,0 +1,63 @@
# test slice assignment to memoryview
try:
memoryview(bytearray(1))[:] = memoryview(bytearray(1))
except (NameError, TypeError):
print("SKIP")
raise SystemExit
try:
import uarray as array
except ImportError:
try:
import array
except ImportError:
print("SKIP")
raise SystemExit
# test slice assignment between memoryviews
b1 = bytearray(b'1234')
b2 = bytearray(b'5678')
b3 = bytearray(b'5678')
m1 = memoryview(b1)
m2 = memoryview(b2)
m3 = memoryview(b3)
m2[1:3] = m1[0:2]
print(b2)
b3[1:3] = m1[0:2]
print(b3)
m1[2:4] = b3[1:3]
print(b1)
# invalid slice assignments
try:
m2[1:3] = b1[0:4]
except ValueError:
print("ValueError")
try:
m2[1:3] = m1[0:4]
except ValueError:
print("ValueError")
try:
m2[0:4] = m1[1:3]
except ValueError:
print("ValueError")
# test memoryview of arrays with items sized larger than 1
a1 = array.array('i', [0]*5)
m4 = memoryview(a1)
a2 = array.array('i', [3]*5)
m5 = memoryview(a2)
m4[1:3] = m5[1:3]
print(a1)
try:
m4[1:3] = m2[1:3]
except ValueError:
print("ValueError")
# invalid assignment on RHS
try:
memoryview(array.array('i'))[0:2] = b'1234'
except ValueError:
print('ValueError')