aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/networkx/utils/tests/test_mapped_queue.py
blob: ca9b7e42072f5aebbf4b794302d06f21f5d8e17c (about) (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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import pytest

from networkx.utils.mapped_queue import MappedQueue, _HeapElement


def test_HeapElement_gtlt():
    bar = _HeapElement(1.1, "a")
    foo = _HeapElement(1, "b")
    assert foo < bar
    assert bar > foo
    assert foo < 1.1
    assert 1 < bar


def test_HeapElement_gtlt_tied_priority():
    bar = _HeapElement(1, "a")
    foo = _HeapElement(1, "b")
    assert foo > bar
    assert bar < foo


def test_HeapElement_eq():
    bar = _HeapElement(1.1, "a")
    foo = _HeapElement(1, "a")
    assert foo == bar
    assert bar == foo
    assert foo == "a"


def test_HeapElement_iter():
    foo = _HeapElement(1, "a")
    bar = _HeapElement(1.1, (3, 2, 1))
    assert list(foo) == [1, "a"]
    assert list(bar) == [1.1, 3, 2, 1]


def test_HeapElement_getitem():
    foo = _HeapElement(1, "a")
    bar = _HeapElement(1.1, (3, 2, 1))
    assert foo[1] == "a"
    assert foo[0] == 1
    assert bar[0] == 1.1
    assert bar[2] == 2
    assert bar[3] == 1
    pytest.raises(IndexError, bar.__getitem__, 4)
    pytest.raises(IndexError, foo.__getitem__, 2)


class TestMappedQueue:
    def setup_method(self):
        pass

    def _check_map(self, q):
        assert q.position == {elt: pos for pos, elt in enumerate(q.heap)}

    def _make_mapped_queue(self, h):
        q = MappedQueue()
        q.heap = h
        q.position = {elt: pos for pos, elt in enumerate(h)}
        return q

    def test_heapify(self):
        h = [5, 4, 3, 2, 1, 0]
        q = self._make_mapped_queue(h)
        q._heapify()
        self._check_map(q)

    def test_init(self):
        h = [5, 4, 3, 2, 1, 0]
        q = MappedQueue(h)
        self._check_map(q)

    def test_incomparable(self):
        h = [5, 4, "a", 2, 1, 0]
        pytest.raises(TypeError, MappedQueue, h)

    def test_len(self):
        h = [5, 4, 3, 2, 1, 0]
        q = MappedQueue(h)
        self._check_map(q)
        assert len(q) == 6

    def test_siftup_leaf(self):
        h = [2]
        h_sifted = [2]
        q = self._make_mapped_queue(h)
        q._siftup(0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftup_one_child(self):
        h = [2, 0]
        h_sifted = [0, 2]
        q = self._make_mapped_queue(h)
        q._siftup(0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftup_left_child(self):
        h = [2, 0, 1]
        h_sifted = [0, 2, 1]
        q = self._make_mapped_queue(h)
        q._siftup(0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftup_right_child(self):
        h = [2, 1, 0]
        h_sifted = [0, 1, 2]
        q = self._make_mapped_queue(h)
        q._siftup(0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftup_multiple(self):
        h = [0, 1, 2, 4, 3, 5, 6]
        h_sifted = [0, 1, 2, 4, 3, 5, 6]
        q = self._make_mapped_queue(h)
        q._siftup(0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftdown_leaf(self):
        h = [2]
        h_sifted = [2]
        q = self._make_mapped_queue(h)
        q._siftdown(0, 0)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftdown_single(self):
        h = [1, 0]
        h_sifted = [0, 1]
        q = self._make_mapped_queue(h)
        q._siftdown(0, len(h) - 1)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_siftdown_multiple(self):
        h = [1, 2, 3, 4, 5, 6, 7, 0]
        h_sifted = [0, 1, 3, 2, 5, 6, 7, 4]
        q = self._make_mapped_queue(h)
        q._siftdown(0, len(h) - 1)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_push(self):
        to_push = [6, 1, 4, 3, 2, 5, 0]
        h_sifted = [0, 2, 1, 6, 3, 5, 4]
        q = MappedQueue()
        for elt in to_push:
            q.push(elt)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_push_duplicate(self):
        to_push = [2, 1, 0]
        h_sifted = [0, 2, 1]
        q = MappedQueue()
        for elt in to_push:
            inserted = q.push(elt)
            assert inserted
        assert q.heap == h_sifted
        self._check_map(q)
        inserted = q.push(1)
        assert not inserted

    def test_pop(self):
        h = [3, 4, 6, 0, 1, 2, 5]
        h_sorted = sorted(h)
        q = self._make_mapped_queue(h)
        q._heapify()
        popped = [q.pop() for _ in range(len(h))]
        assert popped == h_sorted
        self._check_map(q)

    def test_remove_leaf(self):
        h = [0, 2, 1, 6, 3, 5, 4]
        h_removed = [0, 2, 1, 6, 4, 5]
        q = self._make_mapped_queue(h)
        removed = q.remove(3)
        assert q.heap == h_removed

    def test_remove_root(self):
        h = [0, 2, 1, 6, 3, 5, 4]
        h_removed = [1, 2, 4, 6, 3, 5]
        q = self._make_mapped_queue(h)
        removed = q.remove(0)
        assert q.heap == h_removed

    def test_update_leaf(self):
        h = [0, 20, 10, 60, 30, 50, 40]
        h_updated = [0, 15, 10, 60, 20, 50, 40]
        q = self._make_mapped_queue(h)
        removed = q.update(30, 15)
        assert q.heap == h_updated

    def test_update_root(self):
        h = [0, 20, 10, 60, 30, 50, 40]
        h_updated = [10, 20, 35, 60, 30, 50, 40]
        q = self._make_mapped_queue(h)
        removed = q.update(0, 35)
        assert q.heap == h_updated


class TestMappedDict(TestMappedQueue):
    def _make_mapped_queue(self, h):
        priority_dict = {elt: elt for elt in h}
        return MappedQueue(priority_dict)

    def test_init(self):
        d = {5: 0, 4: 1, "a": 2, 2: 3, 1: 4}
        q = MappedQueue(d)
        assert q.position == d

    def test_ties(self):
        d = {5: 0, 4: 1, 3: 2, 2: 3, 1: 4}
        q = MappedQueue(d)
        assert q.position == {elt: pos for pos, elt in enumerate(q.heap)}

    def test_pop(self):
        d = {5: 0, 4: 1, 3: 2, 2: 3, 1: 4}
        q = MappedQueue(d)
        assert q.pop() == _HeapElement(0, 5)
        assert q.position == {elt: pos for pos, elt in enumerate(q.heap)}

    def test_empty_pop(self):
        q = MappedQueue()
        pytest.raises(IndexError, q.pop)

    def test_incomparable_ties(self):
        d = {5: 0, 4: 0, "a": 0, 2: 0, 1: 0}
        pytest.raises(TypeError, MappedQueue, d)

    def test_push(self):
        to_push = [6, 1, 4, 3, 2, 5, 0]
        h_sifted = [0, 2, 1, 6, 3, 5, 4]
        q = MappedQueue()
        for elt in to_push:
            q.push(elt, priority=elt)
        assert q.heap == h_sifted
        self._check_map(q)

    def test_push_duplicate(self):
        to_push = [2, 1, 0]
        h_sifted = [0, 2, 1]
        q = MappedQueue()
        for elt in to_push:
            inserted = q.push(elt, priority=elt)
            assert inserted
        assert q.heap == h_sifted
        self._check_map(q)
        inserted = q.push(1, priority=1)
        assert not inserted

    def test_update_leaf(self):
        h = [0, 20, 10, 60, 30, 50, 40]
        h_updated = [0, 15, 10, 60, 20, 50, 40]
        q = self._make_mapped_queue(h)
        removed = q.update(30, 15, priority=15)
        assert q.heap == h_updated

    def test_update_root(self):
        h = [0, 20, 10, 60, 30, 50, 40]
        h_updated = [10, 20, 35, 60, 30, 50, 40]
        q = self._make_mapped_queue(h)
        removed = q.update(0, 35, priority=35)
        assert q.heap == h_updated