forked from shane-kerr/heapdict
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_heap.py
executable file
·163 lines (141 loc) · 4.77 KB
/
test_heap.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python
from __future__ import print_function
import random
import sys
import unittest
import timeit
import tempfile
from heapdict import heapdict
try:
# Python 3
import test.support as test_support
except ImportError:
# Python 2
import test.test_support as test_support
N = 100
class TestHeap(unittest.TestCase):
def make_data(self):
pairs = [(random.random(), random.random()) for i in range(N)]
h = heapdict()
d = {}
for k, v in pairs:
h[k] = v
d[k] = v
pairs.sort(key=lambda x: x[1], reverse=True)
return h, pairs, d
def test_pop(self):
# verify that we raise IndexError on empty heapdict
empty_heap = heapdict()
self.assertRaises(IndexError, empty_heap.pop)
# test adding a bunch of random values at random priorities
h, pairs, _ = self.make_data()
last_priority = 0
while pairs:
v1 = h.pop()
(v2, priority) = pairs.pop()
# confirm that our heapdict is returning things in sorted order
self.assertEqual(v1, v2)
# verify that our items are increasing in priority value
self.assertGreater(priority, last_priority)
last_priority = priority
# make sure that we got everything out of the heap
self.assertEqual(len(h), 0)
# now verify that we raise KeyError if we try to remove something
# by key that is not present
empty_heap = heapdict()
self.assertRaises(KeyError, empty_heap.pop, "missing")
# verify that we do *not* get a KeyError if we specify a default
empty_heap = heapdict()
self.assertEqual(empty_heap.pop("missing", 123), 123)
# confirm that we can get a value by key if it is present
h = heapdict()
h["foo"] = 10
self.assertEqual(len(h), 1)
self.assertEqual(h.pop("foo"), 10)
self.assertEqual(len(h), 0)
# verify that removing keys does the right thing to a heap
h = heapdict()
h["c"] = 30
h["a"] = 10
h["b"] = 20
self.assertEqual(len(h), 3)
self.assertEqual(h.pop("b"), 20)
self.assertEqual(h.pop(), "a")
self.assertEqual(h.pop(), "c")
self.assertEqual(len(h), 0)
def test_popitem(self):
h, pairs, _ = self.make_data()
while pairs:
v = h.popitem()
v2 = pairs.pop()
self.assertEqual(v, v2)
self.assertEqual(len(h), 0)
def test_popitem_ties(self):
h = heapdict()
for i in range(N):
h[i] = 0
for i in range(N):
_, v = h.popitem()
self.assertEqual(v, 0)
h._check_invariants()
def test_peek(self):
h, pairs, _ = self.make_data()
while pairs:
v = h.peekitem()[0]
h.popitem()
v2 = pairs.pop()
self.assertEqual(v, v2[0])
self.assertEqual(len(h), 0)
def test_iter(self):
h, _, d = self.make_data()
self.assertEqual(list(h), list(d))
def test_keys(self):
h, _, d = self.make_data()
self.assertEqual(list(sorted(h.keys())), list(sorted(d.keys())))
def test_values(self):
h, _, d = self.make_data()
self.assertEqual(list(sorted(h.values())), list(sorted(d.values())))
def test_del(self):
h, pairs, _ = self.make_data()
k, v = pairs.pop(N//2)
del h[k]
while pairs:
v = h.popitem()
v2 = pairs.pop()
self.assertEqual(v, v2)
self.assertEqual(len(h), 0)
def test_change(self):
h, pairs, _ = self.make_data()
k, v = pairs[N//2]
h[k] = 0.5
pairs[N//2] = (k, 0.5)
pairs.sort(key=lambda x: x[1], reverse=True)
while pairs:
v = h.popitem()
v2 = pairs.pop()
self.assertEqual(v, v2)
self.assertEqual(len(h), 0)
def test_clear(self):
h, _, _ = self.make_data()
h.clear()
self.assertEqual(len(h), 0)
def test_main(verbose=None):
test_classes = [TestHeap]
test_support.run_unittest(*test_classes)
# verify reference counting
if verbose and hasattr(sys, "gettotalrefcount"):
import gc
counts = [None] * 5
for i in range(len(counts)):
test_support.run_unittest(*test_classes)
gc.collect()
counts[i] = sys.gettotalrefcount()
print(counts)
if __name__ == "__main__":
test_main(verbose=True)
originout = sys.stdout
with tempfile.TemporaryFile(mode="w") as tmp:
sys.stdout = tmp
total = min(timeit.repeat("test_main()", number=20, repeat=3, setup="from __main__ import test_main"))
sys.stdout = originout
print("Timeit: {} s".format(total))