summaryrefslogtreecommitdiff
path: root/tests/micropython/viper_misc3.py
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2023-02-09 15:32:15 +1100
committerDamien George <damien@micropython.org>2023-02-09 16:12:25 +1100
commit41ed01f1394ea608bf9d055120d671e2765cd9b7 (patch)
tree15337158fd299c36ec21513bae5144dd5947c643 /tests/micropython/viper_misc3.py
parentd99ebb310c1d6cdc87f3fa9a913149d1cd9c474a (diff)
tests/micropython: Split viper_misc test into two files.
So it can run on targets with low memory, eg esp8266. Also enable the viper_4args() sub-test, which is now supported. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'tests/micropython/viper_misc3.py')
-rw-r--r--tests/micropython/viper_misc3.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/micropython/viper_misc3.py b/tests/micropython/viper_misc3.py
new file mode 100644
index 000000000..7b211e5dd
--- /dev/null
+++ b/tests/micropython/viper_misc3.py
@@ -0,0 +1,96 @@
+# Miscellaneous viper tests.
+
+import micropython
+
+
+# a for loop
+@micropython.viper
+def viper_for(a: int, b: int) -> int:
+ total = 0
+ for x in range(a, b):
+ total += x
+ return total
+
+
+print(viper_for(10, 10000))
+
+
+# accessing a global
+@micropython.viper
+def viper_access_global():
+ global gl
+ gl = 1
+ return gl
+
+
+print(viper_access_global(), gl)
+
+
+# calling print with object and int types
+@micropython.viper
+def viper_print(x, y: int):
+ print(x, y + 1)
+
+
+viper_print(1, 2)
+
+
+# convert constants to objects in tuple
+@micropython.viper
+def viper_tuple_consts(x):
+ return (x, 1, False, True)
+
+
+print(viper_tuple_consts(0))
+
+
+# making a tuple from an object and an int
+@micropython.viper
+def viper_tuple(x, y: int):
+ return (x, y + 1)
+
+
+print(viper_tuple(1, 2))
+
+
+# making a list from an object and an int
+@micropython.viper
+def viper_list(x, y: int):
+ return [x, y + 1]
+
+
+print(viper_list(1, 2))
+
+
+# making a set from an object and an int
+@micropython.viper
+def viper_set(x, y: int):
+ return {x, y + 1}
+
+
+print(sorted(list(viper_set(1, 2))))
+
+
+# raising an exception
+@micropython.viper
+def viper_raise(x: int):
+ raise OSError(x)
+
+
+try:
+ viper_raise(1)
+except OSError as e:
+ print(repr(e))
+
+
+# calling GC after defining the function
+@micropython.viper
+def viper_gc() -> int:
+ return 1
+
+
+print(viper_gc())
+import gc
+
+gc.collect()
+print(viper_gc())