summaryrefslogtreecommitdiff
path: root/tests/micropython/heapalloc_fail_bytearray.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-04-18 14:34:12 +1000
committerDamien George <damien.p.george@gmail.com>2019-04-18 14:34:12 +1000
commiteb1f81b209f0d13059ebb4fa2ed105a0d6a4b0d0 (patch)
treebe61d020fc09161fc856d3f1516495219b647978 /tests/micropython/heapalloc_fail_bytearray.py
parent4ce0091449052daca592f852a31eece074d34a57 (diff)
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.
Diffstat (limited to 'tests/micropython/heapalloc_fail_bytearray.py')
-rw-r--r--tests/micropython/heapalloc_fail_bytearray.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/tests/micropython/heapalloc_fail_bytearray.py b/tests/micropython/heapalloc_fail_bytearray.py
new file mode 100644
index 000000000..fbf585c7f
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_bytearray.py
@@ -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)