tests/micropython: Add some tests for failed heap allocation.

This adds tests for some locations in the code where a memory allocation
should raise an exception.
pull/4703/head
Damien George 2019-04-18 14:34:12 +10:00
rodzic 4ce0091449
commit eb1f81b209
12 zmienionych plików z 227 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,90 @@
# test handling of failed heap allocation with bytearray
import micropython
class GetSlice:
def __getitem__(self, idx):
return idx
sl = GetSlice()[:]
# create bytearray
micropython.heap_lock()
try:
bytearray(4)
except MemoryError:
print('MemoryError: bytearray create')
micropython.heap_unlock()
# create bytearray from bytes
micropython.heap_lock()
try:
bytearray(b'0123')
except MemoryError:
print('MemoryError: bytearray create from bytes')
micropython.heap_unlock()
# create bytearray from iterator
r = range(4)
micropython.heap_lock()
try:
bytearray(r)
except MemoryError:
print('MemoryError: bytearray create from iter')
micropython.heap_unlock()
# bytearray add
b = bytearray(4)
micropython.heap_lock()
try:
b + b'01'
except MemoryError:
print('MemoryError: bytearray.__add__')
micropython.heap_unlock()
# bytearray iadd
b = bytearray(4)
micropython.heap_lock()
try:
b += b'01234567'
except MemoryError:
print('MemoryError: bytearray.__iadd__')
micropython.heap_unlock()
print(b)
# bytearray append
b = bytearray(4)
micropython.heap_lock()
try:
for i in range(100):
b.append(1)
except MemoryError:
print('MemoryError: bytearray.append')
micropython.heap_unlock()
# bytearray extend
b = bytearray(4)
micropython.heap_lock()
try:
b.extend(b'01234567')
except MemoryError:
print('MemoryError: bytearray.extend')
micropython.heap_unlock()
# bytearray get with slice
b = bytearray(4)
micropython.heap_lock()
try:
b[sl]
except MemoryError:
print('MemoryError: bytearray subscr get')
micropython.heap_unlock()
# extend bytearray using slice subscr
b = bytearray(4)
micropython.heap_lock()
try:
b[sl] = b'01234567'
except MemoryError:
print('MemoryError: bytearray subscr grow')
micropython.heap_unlock()
print(b)

Wyświetl plik

@ -0,0 +1,11 @@
MemoryError: bytearray create
MemoryError: bytearray create from bytes
MemoryError: bytearray create from iter
MemoryError: bytearray.__add__
MemoryError: bytearray.__iadd__
bytearray(b'\x00\x00\x00\x00')
MemoryError: bytearray.append
MemoryError: bytearray.extend
MemoryError: bytearray subscr get
MemoryError: bytearray subscr grow
bytearray(b'\x00\x00\x00\x00')

Wyświetl plik

@ -0,0 +1,21 @@
# test handling of failed heap allocation with dict
import micropython
# create dict
x = 1
micropython.heap_lock()
try:
{x:x}
except MemoryError:
print('MemoryError: create dict')
micropython.heap_unlock()
# create dict view
x = {1:1}
micropython.heap_lock()
try:
x.items()
except MemoryError:
print('MemoryError: dict.items')
micropython.heap_unlock()

Wyświetl plik

@ -0,0 +1,2 @@
MemoryError: create dict
MemoryError: dict.items

Wyświetl plik

@ -0,0 +1,36 @@
# test handling of failed heap allocation with list
import micropython
class GetSlice:
def __getitem__(self, idx):
return idx
sl = GetSlice()[:]
# create slice in VM
l = [1, 2, 3]
micropython.heap_lock()
try:
print(l[0:1])
except MemoryError:
print('MemoryError: list index')
micropython.heap_unlock()
# get from list using slice
micropython.heap_lock()
try:
l[sl]
except MemoryError:
print('MemoryError: list get slice')
micropython.heap_unlock()
# extend list using slice subscr
l = [1, 2]
l2 = [3, 4, 5, 6, 7, 8, 9, 10]
micropython.heap_lock()
try:
l[sl] = l2
except MemoryError:
print('MemoryError: list extend slice')
micropython.heap_unlock()
print(l)

Wyświetl plik

@ -0,0 +1,4 @@
MemoryError: list index
MemoryError: list get slice
MemoryError: list extend slice
[1, 2]

Wyświetl plik

@ -0,0 +1,25 @@
# test handling of failed heap allocation with memoryview
import micropython
class GetSlice:
def __getitem__(self, idx):
return idx
sl = GetSlice()[:]
# create memoryview
micropython.heap_lock()
try:
memoryview(b'')
except MemoryError:
print('MemoryError: memoryview create')
micropython.heap_unlock()
# memoryview get with slice
m = memoryview(b'')
micropython.heap_lock()
try:
m[sl]
except MemoryError:
print('MemoryError: memoryview subscr get')
micropython.heap_unlock()

Wyświetl plik

@ -0,0 +1,2 @@
MemoryError: memoryview create
MemoryError: memoryview subscr get

Wyświetl plik

@ -0,0 +1,21 @@
# test handling of failed heap allocation with set
import micropython
# create set
x = 1
micropython.heap_lock()
try:
{x,}
except MemoryError:
print('MemoryError: set create')
micropython.heap_unlock()
# set copy
s = {1, 2}
micropython.heap_lock()
try:
s.copy()
except MemoryError:
print('MemoryError: set copy')
micropython.heap_unlock()

Wyświetl plik

@ -0,0 +1,2 @@
MemoryError: set create
MemoryError: set copy

Wyświetl plik

@ -0,0 +1,12 @@
# test handling of failed heap allocation with tuple
import micropython
# create tuple
x = 1
micropython.heap_lock()
try:
(x,)
except MemoryError:
print('MemoryError: tuple create')
micropython.heap_unlock()

Wyświetl plik

@ -0,0 +1 @@
MemoryError: tuple create