summaryrefslogtreecommitdiff
path: root/tests/micropython
diff options
context:
space:
mode:
Diffstat (limited to 'tests/micropython')
-rw-r--r--tests/micropython/heapalloc.py2
-rw-r--r--tests/micropython/heapalloc_inst_call.py31
-rw-r--r--tests/micropython/heapalloc_inst_call.py.exp4
-rw-r--r--tests/micropython/heapalloc_traceback.py36
-rw-r--r--tests/micropython/heapalloc_traceback.py.exp5
-rw-r--r--tests/micropython/stack_use.py7
-rw-r--r--tests/micropython/stack_use.py.exp1
-rw-r--r--tests/micropython/viper_import.py10
-rw-r--r--tests/micropython/viper_import.py.exp2
9 files changed, 97 insertions, 1 deletions
diff --git a/tests/micropython/heapalloc.py b/tests/micropython/heapalloc.py
index a651158ca..62f26df6a 100644
--- a/tests/micropython/heapalloc.py
+++ b/tests/micropython/heapalloc.py
@@ -18,7 +18,7 @@ def f3(a, b, c, d):
global_var = 1
def test():
- global global_var
+ global global_var, global_exc
global_var = 2 # set an existing global variable
for i in range(2): # for loop
f1(i) # function call
diff --git a/tests/micropython/heapalloc_inst_call.py b/tests/micropython/heapalloc_inst_call.py
new file mode 100644
index 000000000..3cc497b73
--- /dev/null
+++ b/tests/micropython/heapalloc_inst_call.py
@@ -0,0 +1,31 @@
+# Test that calling clazz.__call__() with up to at least 3 arguments
+# doesn't require heap allocation.
+import micropython
+
+class Foo0:
+ def __call__(self):
+ print("__call__")
+
+class Foo1:
+ def __call__(self, a):
+ print("__call__", a)
+
+class Foo2:
+ def __call__(self, a, b):
+ print("__call__", a, b)
+
+class Foo3:
+ def __call__(self, a, b, c):
+ print("__call__", a, b, c)
+
+f0 = Foo0()
+f1 = Foo1()
+f2 = Foo2()
+f3 = Foo3()
+
+micropython.heap_lock()
+f0()
+f1(1)
+f2(1, 2)
+f3(1, 2, 3)
+micropython.heap_unlock()
diff --git a/tests/micropython/heapalloc_inst_call.py.exp b/tests/micropython/heapalloc_inst_call.py.exp
new file mode 100644
index 000000000..0b06bb2c9
--- /dev/null
+++ b/tests/micropython/heapalloc_inst_call.py.exp
@@ -0,0 +1,4 @@
+__call__
+__call__ 1
+__call__ 1 2
+__call__ 1 2 3
diff --git a/tests/micropython/heapalloc_traceback.py b/tests/micropython/heapalloc_traceback.py
new file mode 100644
index 000000000..808df0225
--- /dev/null
+++ b/tests/micropython/heapalloc_traceback.py
@@ -0,0 +1,36 @@
+# test that we can generate a traceback without allocating
+
+import micropython
+import sys
+import uio
+
+# preallocate exception instance with some room for a traceback
+global_exc = StopIteration()
+try:
+ raise global_exc
+except:
+ pass
+
+def test():
+ global global_exc
+ global_exc.__traceback__ = None
+ try:
+ raise global_exc
+ except StopIteration:
+ print('StopIteration')
+
+# call test() with heap allocation disabled
+micropython.heap_lock()
+test()
+micropython.heap_unlock()
+
+# print the exception that was raised
+buf = uio.StringIO()
+sys.print_exception(global_exc, buf)
+for l in buf.getvalue().split("\n"):
+ # uPy on pyboard prints <stdin> as file, so remove filename.
+ if l.startswith(" File "):
+ l = l.split('"')
+ print(l[0], l[2])
+ else:
+ print(l)
diff --git a/tests/micropython/heapalloc_traceback.py.exp b/tests/micropython/heapalloc_traceback.py.exp
new file mode 100644
index 000000000..9ba10948d
--- /dev/null
+++ b/tests/micropython/heapalloc_traceback.py.exp
@@ -0,0 +1,5 @@
+StopIteration
+Traceback (most recent call last):
+ File , line 18, in test
+StopIteration:
+
diff --git a/tests/micropython/stack_use.py b/tests/micropython/stack_use.py
new file mode 100644
index 000000000..bc714755a
--- /dev/null
+++ b/tests/micropython/stack_use.py
@@ -0,0 +1,7 @@
+# tests stack_use function in micropython module
+import micropython
+
+if not hasattr(micropython, 'stack_use'):
+ print('SKIP')
+else:
+ print(type(micropython.stack_use())) # output varies
diff --git a/tests/micropython/stack_use.py.exp b/tests/micropython/stack_use.py.exp
new file mode 100644
index 000000000..fe37ceaa1
--- /dev/null
+++ b/tests/micropython/stack_use.py.exp
@@ -0,0 +1 @@
+<class 'int'>
diff --git a/tests/micropython/viper_import.py b/tests/micropython/viper_import.py
new file mode 100644
index 000000000..987800744
--- /dev/null
+++ b/tests/micropython/viper_import.py
@@ -0,0 +1,10 @@
+# test import within viper function
+
+@micropython.viper
+def f():
+ import micropython
+ print(micropython.const(1))
+
+ from micropython import const
+ print(const(2))
+f()
diff --git a/tests/micropython/viper_import.py.exp b/tests/micropython/viper_import.py.exp
new file mode 100644
index 000000000..1191247b6
--- /dev/null
+++ b/tests/micropython/viper_import.py.exp
@@ -0,0 +1,2 @@
+1
+2