-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_test.go
284 lines (202 loc) · 4.54 KB
/
heap_test.go
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package fibheap
import (
"math/rand"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
// generateMockItems generates mock items with random int values
func generateMockItems(count int) []*mockItem {
items := make([]*mockItem, count)
for i := 0; i < count; i++ {
items[i] = &mockItem{
//nolint:gosec
value: rand.Intn(count),
}
}
return items
}
func TestHeap_Push(t *testing.T) {
t.Parallel()
numItems := 100
items := generateMockItems(numItems)
h := NewHeap()
for _, item := range items {
h.Push(item)
}
assert.Equal(t, uint(numItems), h.size)
}
func TestHeap_Pop(t *testing.T) {
t.Parallel()
item := &mockItem{}
h := NewHeap()
h.Push(item)
popped := h.Pop()
assert.Equal(t, item, popped)
assert.Equal(t, uint(0), h.size)
assert.Nil(t, h.Pop())
}
func TestHeap_Peek(t *testing.T) {
t.Parallel()
t.Run("empty heap", func(t *testing.T) {
t.Parallel()
h := NewHeap()
assert.Equal(t, uint(0), h.Size())
assert.Nil(t, h.Peek())
})
t.Run("min heap", func(t *testing.T) {
t.Parallel()
items := generateMockItems(10)
h := NewHeap()
// Find min and add items
min := items[0]
for _, item := range items {
if item.value < min.value {
min = item
}
h.Push(item)
}
assert.Equal(t, uint(len(items)), h.Size())
assert.Equal(t, min, h.Peek())
})
t.Run("max heap", func(t *testing.T) {
t.Parallel()
items := generateMockItems(10)
h := NewHeap()
// Find min and add items
max := items[0]
for _, item := range items {
item := item
// Set it to be a max heap
item.lessFn = func(otherRaw Item) bool {
other, _ := otherRaw.(*mockItem)
return item.value >= other.value
}
if item.value > max.value {
max = item
}
h.Push(item)
}
assert.Equal(t, uint(len(items)), h.Size())
peeked, _ := h.Peek().(*mockItem)
assert.Equal(t, max.value, peeked.value)
})
}
func TestHeap_Merge(t *testing.T) {
t.Parallel()
items := []*mockItem{
{
value: 10,
},
{
value: 5,
},
}
heaps := []*Heap{
{
entry: newNode(items[0]),
size: 1,
},
{
entry: newNode(items[1]),
size: 1,
},
}
// Merge the heaps
heaps[0].Merge(heaps[1])
// Make sure the heap size increased
heap := heaps[0]
assert.Equal(t, uint(2), heap.Size())
assert.Equal(t, items[1], heap.Pop())
}
func TestHeap_Merge_Empty(t *testing.T) {
t.Parallel()
item := &mockItem{}
h := NewHeap()
h.Push(item)
assertCommon := func() {
assert.Equal(t, uint(1), h.Size())
assert.Equal(t, item, h.Peek())
}
assertCommon()
// Empty heap
h.Merge(NewHeap())
assertCommon()
// Invalid heap
h.Merge(nil)
assertCommon()
}
func TestHeap_Merge_AssignEntry(t *testing.T) {
t.Parallel()
item := &mockItem{}
h := NewHeap()
heapToMerge := NewHeap()
heapToMerge.Push(item)
assert.Equal(t, uint(1), heapToMerge.Size())
// Merge the heaps
h.Merge(heapToMerge)
// Make sure the heap size increased
assert.Equal(t, uint(1), h.Size())
assert.Equal(t, item, h.Pop())
}
func TestHeap_PushPop(t *testing.T) {
t.Parallel()
t.Run("min heap", func(t *testing.T) {
t.Parallel()
h := NewHeap()
items := generateMockItems(1000)
minSorted := make([]*mockItem, len(items))
copy(minSorted, items)
// Sort the array ascending
sort.SliceStable(minSorted, func(i, j int) bool {
return minSorted[i].value < minSorted[j].value
})
for _, item := range items {
h.Push(item)
}
// Pop them off one by one
for _, item := range minSorted {
popped, _ := h.Pop().(*mockItem)
assert.Equal(t, item.value, popped.value)
}
assert.Equal(t, uint(0), h.Size())
})
t.Run("max heap", func(t *testing.T) {
t.Parallel()
items := generateMockItems(1000)
h := NewHeap()
maxSorted := make([]*mockItem, len(items))
copy(maxSorted, items)
// Sort the array ascending
sort.SliceStable(maxSorted, func(i, j int) bool {
return maxSorted[i].value >= maxSorted[j].value
})
for _, item := range items {
item := item
// Set it to be a max heap
item.lessFn = func(otherRaw Item) bool {
other, _ := otherRaw.(*mockItem)
return item.value >= other.value
}
h.Push(item)
}
// Pop them off one by one
for _, item := range maxSorted {
popped, _ := h.Pop().(*mockItem)
assert.Equal(t, item.value, popped.value)
}
assert.Equal(t, uint(0), h.Size())
})
}
func TestHeap_Clear(t *testing.T) {
t.Parallel()
items := generateMockItems(1000)
h := NewHeap()
for _, item := range items {
h.Push(item)
}
assert.Equal(t, uint(len(items)), h.Size())
h.Clear()
assert.Equal(t, uint(0), h.Size())
assert.Nil(t, h.entry)
}