summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/uheapq1.py
blob: 7c1fe4e1ec149e8b07f4bf768755500d9269df45 (plain) (blame)
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
try:
    import uheapq as heapq
except:
    try:
        import heapq
    except ImportError:
        print("SKIP")
        raise SystemExit

try:
    heapq.heappop([])
except IndexError:
    print("IndexError")

try:
    heapq.heappush((), 1)
except TypeError:
    print("TypeError")

def pop_and_print(h):
    l = []
    while h:
        l.append(str(heapq.heappop(h)))
    print(' '.join(l))

h = []
heapq.heappush(h, 3)
heapq.heappush(h, 1)
heapq.heappush(h, 2)
print(h)
pop_and_print(h)

h = [4, 3, 8, 9, 10, 2, 7, 11, 5]
heapq.heapify(h)
print(h)
heapq.heappush(h, 1)
heapq.heappush(h, 6)
heapq.heappush(h, 12)
print(h)
pop_and_print(h)