-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_binaryheap.py
More file actions
138 lines (113 loc) · 3.56 KB
/
test_binaryheap.py
File metadata and controls
138 lines (113 loc) · 3.56 KB
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
import unittest
from binaryheap import BinaryHeap, rank_based_partition
# class Widget:
# def __init__(self, name, key):
# self.name = name
# self.key = key
# def __lt__(self, other):
# return self.key < other.key
# def __gt__(self, other):
# return self.key > other.key
wa = "a"
wb = "b"
wc = "c"
wd = "d"
we = "e"
very_big_number = 987654321
class TestBinaryHeap(unittest.TestCase):
def setUp(self):
self.heap = BinaryHeap()
# insert:
def test_insert(self):
self.heap.insert(wa, 100)
self.heap._check_rep()
self.assertEqual(self.heap.array(), [wa])
def test_insert_two(self):
self.heap.insert(wa, 100)
self.heap.insert(wb, 50)
self.heap._check_rep()
self.assertEqual(self.heap.array(), [wa, wb])
def test_insert_out_of_order(self):
self.heap.insert(wa, 50)
self.heap.insert(wb, 100)
self.heap._check_rep()
self.assertEqual(self.heap.array(), [wb, wa])
def test_negative_priority(self):
custom_heap = BinaryHeap()
custom_heap.insert(wa, -50)
custom_heap.insert(wb, -100)
self.heap._check_rep()
self.assertEqual(custom_heap.array(), [wa, wb])
def test_insert_many(self):
self.heap.insert(wa, -100)
self.heap.insert(wb, -50)
self.heap.insert(wc, 0)
self.heap.insert(wd, 50)
self.heap.insert(we, 100)
self.assertEqual(set(self.heap.array()), {wa, wb, wc, wd, we})
self.heap._check_rep()
def test_sort(self):
self.heap.insert(wa, -100)
self.heap.insert(wb, 50)
self.heap.insert(wc, 0)
self.heap.insert(wd, -50)
self.heap.insert(we, 100)
self.heap.sort()
self.heap._check_rep()
self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa])
def test_decrease_key(self):
self.heap.insert(wa, -100)
self.heap.insert(wb, 50)
self.heap.insert(wc, 0)
self.heap.insert(wd, -50)
self.heap.insert(we, 100)
self.heap.sort()
self.heap._check_rep()
self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa])
self.heap.change_priority(wb, -2000)
self.assertNotEqual(self.heap.array(), [we, wb, wc, wd, wa])
self.heap._check_rep()
self.heap.sort()
self.assertEqual(self.heap.array(), [we, wc, wd, wa, wb])
self.heap._check_rep()
def test_increase_key(self):
self.heap.insert(wa, -100)
self.heap.insert(wb, 50)
self.heap.insert(wc, 0)
self.heap.insert(wd, -50)
self.heap.insert(we, 100)
self.heap.sort()
self.heap._check_rep()
self.assertEqual(self.heap.array(), [we, wb, wc, wd, wa])
self.heap.change_priority(wb, 2000)
self.assertNotEqual(self.heap.array(), [we, wb, wc, wd, wa])
self.heap._check_rep()
self.heap.sort()
self.assertEqual(self.heap.array(), [wb, we, wc, wd, wa])
def test_max_priority(self):
self.heap.insert(wa, very_big_number)
self.assertEqual(self.heap.max_priority(), very_big_number)
def test_max_priority_multiple(self):
self.heap.insert(wc, 20)
self.heap.insert(wa, very_big_number)
self.heap.insert(we, -500)
self.assertEqual(self.heap.max_priority(), very_big_number)
class TestRankPartition(unittest.TestCase):
def test_trivial(self):
right_endpoints = rank_based_partition(1, 1)
self.assertEqual(right_endpoints, [1])
def test_one_bin(self):
right_endpoints = rank_based_partition(5, 1)
self.assertEqual(right_endpoints, [5])
def test_two_bins(self):
right_endpoints = rank_based_partition(3, 2)
self.assertEqual(right_endpoints, [1, 3])
def test_many_bins(self):
right_endpoints = rank_based_partition(10, 3)
self.assertEqual(right_endpoints, [1, 4, 10])
def test_too_many_bins_regression(self):
"""
This is a regression test.
"""
right_endpoints = rank_based_partition(14, 14)
self.assertEqual(right_endpoints, list(range(1, 15)))