summaryrefslogtreecommitdiff
path: root/tests/micropython
diff options
context:
space:
mode:
Diffstat (limited to 'tests/micropython')
-rw-r--r--tests/micropython/heapalloc_fail_bytearray.py90
-rw-r--r--tests/micropython/heapalloc_fail_bytearray.py.exp11
-rw-r--r--tests/micropython/heapalloc_fail_dict.py21
-rw-r--r--tests/micropython/heapalloc_fail_dict.py.exp2
-rw-r--r--tests/micropython/heapalloc_fail_list.py36
-rw-r--r--tests/micropython/heapalloc_fail_list.py.exp4
-rw-r--r--tests/micropython/heapalloc_fail_memoryview.py25
-rw-r--r--tests/micropython/heapalloc_fail_memoryview.py.exp2
-rw-r--r--tests/micropython/heapalloc_fail_set.py21
-rw-r--r--tests/micropython/heapalloc_fail_set.py.exp2
-rw-r--r--tests/micropython/heapalloc_fail_tuple.py12
-rw-r--r--tests/micropython/heapalloc_fail_tuple.py.exp1
12 files changed, 227 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)
diff --git a/tests/micropython/heapalloc_fail_bytearray.py.exp b/tests/micropython/heapalloc_fail_bytearray.py.exp
new file mode 100644
index 000000000..57f6202f5
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_bytearray.py.exp
@@ -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')
diff --git a/tests/micropython/heapalloc_fail_dict.py b/tests/micropython/heapalloc_fail_dict.py
new file mode 100644
index 000000000..ba872bfeb
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_dict.py
@@ -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()
diff --git a/tests/micropython/heapalloc_fail_dict.py.exp b/tests/micropython/heapalloc_fail_dict.py.exp
new file mode 100644
index 000000000..70cfc64ba
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_dict.py.exp
@@ -0,0 +1,2 @@
+MemoryError: create dict
+MemoryError: dict.items
diff --git a/tests/micropython/heapalloc_fail_list.py b/tests/micropython/heapalloc_fail_list.py
new file mode 100644
index 000000000..a54bdb6cf
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_list.py
@@ -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)
diff --git a/tests/micropython/heapalloc_fail_list.py.exp b/tests/micropython/heapalloc_fail_list.py.exp
new file mode 100644
index 000000000..0e1637476
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_list.py.exp
@@ -0,0 +1,4 @@
+MemoryError: list index
+MemoryError: list get slice
+MemoryError: list extend slice
+[1, 2]
diff --git a/tests/micropython/heapalloc_fail_memoryview.py b/tests/micropython/heapalloc_fail_memoryview.py
new file mode 100644
index 000000000..3ba9015ff
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_memoryview.py
@@ -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()
diff --git a/tests/micropython/heapalloc_fail_memoryview.py.exp b/tests/micropython/heapalloc_fail_memoryview.py.exp
new file mode 100644
index 000000000..e41a1e6cb
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_memoryview.py.exp
@@ -0,0 +1,2 @@
+MemoryError: memoryview create
+MemoryError: memoryview subscr get
diff --git a/tests/micropython/heapalloc_fail_set.py b/tests/micropython/heapalloc_fail_set.py
new file mode 100644
index 000000000..98e615d64
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_set.py
@@ -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()
diff --git a/tests/micropython/heapalloc_fail_set.py.exp b/tests/micropython/heapalloc_fail_set.py.exp
new file mode 100644
index 000000000..dd749672d
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_set.py.exp
@@ -0,0 +1,2 @@
+MemoryError: set create
+MemoryError: set copy
diff --git a/tests/micropython/heapalloc_fail_tuple.py b/tests/micropython/heapalloc_fail_tuple.py
new file mode 100644
index 000000000..1cd23fb74
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_tuple.py
@@ -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()
diff --git a/tests/micropython/heapalloc_fail_tuple.py.exp b/tests/micropython/heapalloc_fail_tuple.py.exp
new file mode 100644
index 000000000..5bf632d79
--- /dev/null
+++ b/tests/micropython/heapalloc_fail_tuple.py.exp
@@ -0,0 +1 @@
+MemoryError: tuple create