summaryrefslogtreecommitdiff
path: root/tests/micropython/heapalloc_iter.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-02-10 16:09:20 +1100
committerDamien George <damien.p.george@gmail.com>2017-02-16 19:11:34 +1100
commit019048a6dc0783515e7fba4255dd2b22265d1059 (patch)
tree2a39750a493162e7068ac1c15891314f65c3c384 /tests/micropython/heapalloc_iter.py
parent86b3db9cd08c6ec647fae7a1c723e902f47b7e1e (diff)
tests/micropython: Add test for iterating with the heap locked.
Diffstat (limited to 'tests/micropython/heapalloc_iter.py')
-rw-r--r--tests/micropython/heapalloc_iter.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/micropython/heapalloc_iter.py b/tests/micropython/heapalloc_iter.py
new file mode 100644
index 000000000..3315d78c3
--- /dev/null
+++ b/tests/micropython/heapalloc_iter.py
@@ -0,0 +1,49 @@
+# test that iterating doesn't use the heap
+
+try:
+ from micropython import heap_lock, heap_unlock
+except (ImportError, AttributeError):
+ heap_lock = heap_unlock = lambda:0
+import array
+
+def do_iter(l):
+ for i in l:
+ print(i)
+
+def gen_func():
+ yield 1
+ yield 2
+
+# pre-create collections to iterate over
+ba = bytearray(b'123')
+ar = array.array('H', (123, 456))
+t = (1, 2, 3)
+l = [1, 2]
+d = {1:2}
+s = {1}
+fs = frozenset((1,))
+g1 = (100 + x for x in range(2))
+g2 = gen_func()
+
+# test certain builtins with the heap locked
+heap_lock()
+print(all(t))
+print(any(t))
+print(min(t))
+print(max(t))
+print(sum(t))
+heap_unlock()
+
+# test iterating over collections with the heap locked
+heap_lock()
+do_iter(b'123')
+do_iter(ba)
+do_iter(ar)
+do_iter(t)
+do_iter(l)
+do_iter(d)
+do_iter(s)
+do_iter(fs)
+do_iter(g1)
+do_iter(g2)
+heap_unlock()