summaryrefslogtreecommitdiff
path: root/tests/micropython/heapalloc_iter.py
blob: 3315d78c38f9444db438c7e31be3a53da689ab3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()