def test_pushpop(self): heap = XHeap(reversed(ascii_lowercase), key=self.key) for u, l in reversed(list(zip(ascii_uppercase, ascii_lowercase))): popped_item = heap.pushpop(u) heap.check() self.assertEqual(l, popped_item) self.assertSetEqual(set(ascii_uppercase), set(heap))
def test_remove(self): heap = XHeap(reversed(ascii_uppercase), key=self.key) for c in reversed(ascii_uppercase): wanted = set(heap) wanted.remove(c) heap.remove(c) heap.check() self.assertSetEqual(wanted, set(heap)) self.assertSetEqual(set(), set(heap))
def test_pop(self): heap = XHeap(reversed(ascii_uppercase), key=self.key) sorted_items = [] for c in reversed(ascii_uppercase): popped_item = heap.pop() heap.check() self.assertEqual(c, popped_item) sorted_items.append(popped_item) self.assertSequenceEqual(list(reversed(ascii_uppercase)), sorted_items) self.assertSetEqual(set(), set(heap))
def filled_heap(self): heap = XHeap(digits + ascii_uppercase, key=self.key) for c in digits: heap.remove(c) return heap
def test_check(self): XHeap(key=self.key).check() XHeap(ascii_uppercase, key=self.key).check() XHeap(reversed(ascii_uppercase), key=self.key).check()
def test_init(self): self.assertHeap([], [], XHeap(key=self.key)) self.assertHeap([], [], XHeap([], key=self.key)) self.assertHeap(ascii_uppercase, [], XHeap(ascii_uppercase, key=self.key))
def empty_heap(self): return XHeap(key=self.key)
def test_push(self): heap = XHeap([], key=self.key) for c in reversed(ascii_uppercase): heap.push(c) heap.check() self.assertSetEqual(set(ascii_uppercase), set(heap))
def test_peek(self): heap = XHeap(reversed(ascii_uppercase), key=self.key) self.assertEqual('Z', heap.peek())