-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinomialHeap_test.go
307 lines (264 loc) · 7.21 KB
/
BinomialHeap_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package binomial_heap
import (
"fmt"
"testing"
)
func TestBinomialHeap_Len(t *testing.T) {
heap := New(func(a, b int) int { return a - b })
// Test Len on an empty heap
if len := heap.Len(); len != 0 {
t.Errorf("Len() = %d, want 0", len)
}
// Test Len after pushing elements
heap.Push(5)
heap.Push(3)
heap.Push(7)
if len := heap.Len(); len != 3 {
t.Errorf("Len() = %d, want 3", len)
}
}
func TestBinomialHeap_Push(t *testing.T) {
heap := New(func(a, b int) int { return a - b })
// Test Push and Top
heap.Push(5)
if top := heap.Top(); top != 5 {
t.Errorf("Top() = %d, want 5", top)
}
// Test Push on an empty heap
heap = New(func(a, b int) int { return a - b })
heap.Push(10)
if top := heap.Top(); top != 10 {
t.Errorf("Top() = %d, want 10", top)
}
// Test Push with multiple elements
heap.Push(8)
heap.Push(3)
if top := heap.Top(); top != 10 {
t.Errorf("Top() = %d, want 10", top)
}
// Test Push with elements in descending order
descHeap := New(func(a, b int) int { return b - a })
descHeap.Push(20)
descHeap.Push(15)
descHeap.Push(10)
if top := descHeap.Top(); top != 10 {
t.Errorf("Top() = %d, want 10", top)
}
// Test Push with repeated elements
repeatHeap := New(func(a, b int) int { return a - b })
repeatHeap.Push(5)
repeatHeap.Push(5)
repeatHeap.Push(5)
if top := repeatHeap.Top(); top != 5 {
t.Errorf("Top() = %d, want 5", top)
}
// Test Push with lots of elements
bigHeap := New(func(a, b int) int { return a - b })
for i := 0; i < 100; i++ {
bigHeap.Push(i)
}
for i := 0; i < 100; i++ {
actualTop := bigHeap.Top()
wantTop := 99 - i
if actualTop != wantTop {
t.Errorf("Top() = %d, want %d", actualTop, wantTop)
}
bigHeap.Pop()
}
}
func TestBinomialHeap_Pop(t *testing.T) {
heap := New(func(a, b int) int { return a - b })
// Test Pop after pushing elements
heap.Push(5)
heap.Push(3)
heap.Push(7)
heap.Pop()
if len := heap.Len(); len != 2 {
t.Errorf("Pop(), Len() = %d, want 2", len)
}
if top := heap.Top(); top != 5 {
t.Errorf("Pop(), Top() = %d, want 5", top)
}
// Test Pop until empty
heap.Pop()
heap.Pop()
if len := heap.Len(); len != 0 {
t.Errorf("Multiple Pop(), Len() = %d, want 0", len)
}
}
func TestBinomialHeap_Top(t *testing.T) {
heap := New(func(a, b int) int { return a - b })
// Test Top after pushing elements
heap.Push(5)
heap.Push(3)
heap.Push(7)
if top := heap.Top(); top != 7 {
t.Errorf("Top() = %d, want 7", top)
}
// Test Top after Pop
heap.Pop()
if top := heap.Top(); top != 5 {
t.Errorf("Top() = %d, want 5", top)
}
}
func TestBinomialHeap_Merge(t *testing.T) {
// Test Merge with two non-empty heaps
heap1 := New(func(a, b int) int { return a - b })
heap1.Push(5)
heap1.Push(3)
heap2 := New(func(a, b int) int { return a - b })
heap2.Push(10)
heap2.Push(8)
heap1.Merge(heap2)
if len := heap1.Len(); len != 4 {
t.Errorf("Merge(), Len() = %d, want 4", len)
}
if top := heap1.Top(); top != 10 {
t.Errorf("Merge(), Top() = %d, want 10", top)
}
// Test Merge with one empty heap
heap3 := New(func(a, b int) int { return a - b })
heap3.Push(2)
heap3.Push(4)
heap4 := New(func(a, b int) int { return a - b })
heap4.Merge(heap3)
if len := heap4.Len(); len != 2 {
t.Errorf("Merge(), Len() = %d, want 2", len)
}
if top := heap4.Top(); top != 4 {
t.Errorf("Merge(), Top() = %d, want 4", top)
}
// Test merge two big heaps
bigHeap1 := New(func(a, b int) int { return a - b })
bigHeap2 := New(func(a, b int) int { return a - b })
for i := -100; i < 0; i++ {
bigHeap1.Push(i)
}
for i := 0; i <= 100; i++ {
bigHeap2.Push(i)
}
expectedLen := bigHeap1.Len() + bigHeap2.Len()
bigHeap1.Merge(bigHeap2)
if bigHeap1.Len() != expectedLen {
t.Errorf("Len() = %d, want %d", bigHeap1.Len(), expectedLen)
}
for i := 100; i >= -100; i-- {
actualTop := bigHeap1.Top()
if actualTop != i {
t.Errorf("Top() = %d, want %d", actualTop, i)
}
bigHeap1.Pop()
}
}
// BenchmarkBinomialHeap_Push_Small-16 20313676 54.18 ns/op 24 B/op 1 allocs/op
// BenchmarkBinomialHeap_Push_Small-16 20318802 54.92 ns/op 24 B/op 1 allocs/op
// BenchmarkBinomialHeap_Push_Small-16 20952902 54.23 ns/op 24 B/op 1 allocs/op
func BenchmarkBinomialHeap_Push_Small(b *testing.B) {
heap := New(func(a, b int64) int { return int(a - b) })
b.ResetTimer()
for i := 0; i < b.N; i++ {
heap.Push(int64(i % (b.N/100 + 1)))
}
}
// BenchmarkBinomialHeap_Push_Big-16 6652496 155.1 ns/op 192 B/op 1 allocs/op
// BenchmarkBinomialHeap_Push_Big-16 7082900 155.4 ns/op 192 B/op 1 allocs/op
// BenchmarkBinomialHeap_Push_Big-16 7065980 162.4 ns/op 192 B/op 1 allocs/op
func BenchmarkBinomialHeap_Push_Big(b *testing.B) {
type Large struct {
a int64
b [20]int64
}
heap := New(func(l, r Large) int { return int(l.a - r.a) })
b.ResetTimer()
for i := 0; i < b.N; i++ {
heap.Push(Large{a: int64(i % (b.N/100 + 1))})
}
}
// BenchmarkBinomialHeap_Pop_Small-16 8291036 154.5 ns/op 0 B/op 0 allocs/op
// BenchmarkBinomialHeap_Pop_Small-16 8219092 154.3 ns/op 0 B/op 0 allocs/op
// BenchmarkBinomialHeap_Pop_Small-16 7941328 168.3 ns/op 0 B/op 0 allocs/op
func BenchmarkBinomialHeap_Pop_Small(b *testing.B) {
heap := New(func(a, b int64) int { return int(a - b) })
for i := 0; i < b.N; i++ {
heap.Push(int64(i % (b.N/100 + 1)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
heap.Pop()
}
}
// BenchmarkBinomialHeap_Pop_Big-16 6747440 187.2 ns/op 0 B/op 0 allocs/op
// BenchmarkBinomialHeap_Pop_Big-16 6791820 191.1 ns/op 0 B/op 0 allocs/op
// BenchmarkBinomialHeap_Pop_Big-16 5617261 193.4 ns/op 0 B/op 0 allocs/op
func BenchmarkBinomialHeap_Pop_Big(b *testing.B) {
type Large struct {
a int64
b [20]int64
}
heap := New(func(l, r *Large) int { return int(l.a - r.a) })
for i := 0; i < b.N; i++ {
heap.Push(&Large{a: int64(i % (b.N/100 + 1))})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
heap.Pop()
}
}
func ExampleBinomialHeap_Len() {
heap := New(func(a, b int) int { return a - b })
fmt.Println(heap.Len())
heap.Push(123)
fmt.Println(heap.Len())
// Output:
// 0
// 1
}
func ExampleBinomialHeap_Push() {
heap := New(func(a, b int) int { return a - b })
heap.Push(5)
heap.Push(3)
heap.Push(7)
fmt.Println(heap.Len())
heap.Push(10)
fmt.Println(heap.Len())
fmt.Println(heap.Top())
// Output:
// 3
// 4
// 10
}
func ExampleBinomialHeap_Pop() {
heap := New(func(a, b int) int { return a - b })
heap.Push(5)
heap.Push(3)
heap.Push(7)
fmt.Println(heap.Top())
heap.Pop()
fmt.Println(heap.Top())
// Output:
// 7
// 5
}
func ExampleBinomialHeap_Top() {
heap := New(func(a, b int) int { return a - b })
heap.Push(5)
heap.Push(3)
heap.Push(7)
fmt.Println(heap.Top())
// Output: 7
}
func ExampleBinomialHeap_Merge() {
// Merge two binomial heaps and print the top element after merging
heap1 := New(func(a, b int) int { return a - b })
heap1.Push(5)
heap1.Push(3)
heap2 := New(func(a, b int) int { return a - b })
heap2.Push(10)
heap2.Push(8)
heap1.Merge(heap2)
fmt.Println(heap1.Len())
fmt.Println(heap1.Top())
// Output:
// 4
// 10
}