-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_test.go
More file actions
231 lines (211 loc) · 4.46 KB
/
heap_test.go
File metadata and controls
231 lines (211 loc) · 4.46 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
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
package genericheap_test
import (
"container/heap"
"errors"
"math/rand"
"testing"
"github.com/cpustejovksy/genericheap"
)
func RandomNumbers(size int) []int {
nums := make([]int, size)
for i := range size {
nums[i] = rand.Intn(4096)
}
return nums
}
func TestHeapNew(t *testing.T) {
t.Run("MaxHeap", func(t *testing.T) {
maxHeapProperty := func(parent, child int) bool {
return parent > child
}
nums := RandomNumbers(100)
mh := genericheap.New(nums, maxHeapProperty)
largest := 5000
for mh.Len() > 0 {
v, _ := mh.Pop()
if v > largest {
t.Fatalf("failed")
}
largest = v
}
})
t.Run("MinHeap", func(t *testing.T) {
minHeapProperty := func(parent, child int) bool {
return parent < child
}
nums := RandomNumbers(100)
mh := genericheap.New(nums, minHeapProperty)
smallest := -5000
for mh.Len() > 0 {
v, _ := mh.Pop()
if v < smallest {
t.Fatalf("failed")
}
smallest = v
}
})
}
func TestMaxHeap(t *testing.T) {
maxHeapProperty := func(parent, child int) bool {
return parent > child
}
mh := genericheap.New([]int{}, maxHeapProperty)
nums := RandomNumbers(100)
t.Run("Push", func(t *testing.T) {
for _, num := range nums {
mh.Push(num)
}
})
t.Run("Pop", func(t *testing.T) {
largest := 5000
for mh.Len() > 0 {
v, _ := mh.Pop()
if v > largest {
t.Fatalf("failed")
}
largest = v
}
_, err := mh.Pop()
var check *genericheap.EmptyHeapError
if !errors.As(err, &check) {
t.Fatalf("error of type %T should of type %T", err, &check)
}
})
}
func TestMinHeap(t *testing.T) {
minHeapProperty := func(parent, child int) bool {
return parent < child
}
mh := genericheap.New([]int{}, minHeapProperty)
nums := RandomNumbers(100)
t.Run("Push", func(t *testing.T) {
for _, num := range nums {
mh.Push(num)
}
})
t.Run("Pop", func(t *testing.T) {
smallest := -5000
for mh.Len() > 0 {
v, _ := mh.Pop()
if v < smallest {
t.Fatalf("failed")
}
smallest = v
}
_, err := mh.Pop()
var check *genericheap.EmptyHeapError
if !errors.As(err, &check) {
t.Fatalf("error of type %T should of type %T", err, &check)
}
})
}
func TestHeapAll(t *testing.T) {
minHeapProperty := func(parent, child int) bool {
return parent < child
}
nums := RandomNumbers(10)
mh := genericheap.New(nums, minHeapProperty)
smallest := -5000
for v := range mh.All() {
if v < smallest {
t.Fatalf("failed")
}
smallest = v
}
}
func TestHeapPushPop(t *testing.T) {
minHeapProperty := func(parent, child int) bool {
return parent < child
}
nums := RandomNumbers(100)
mh := genericheap.New(nums, minHeapProperty)
biggerNum := 6000
smallest := -5000
for range nums {
v := mh.PushPop(biggerNum)
if v < smallest {
t.Fatal("failed")
}
smallest = v
biggerNum--
}
for v := range mh.All() {
if v < smallest {
t.Fatal("failed")
}
smallest = v
}
}
var result int
func BenchmarkPushThenPop(b *testing.B) {
nums1 := RandomNumbers(1000)
nums2 := RandomNumbers(1000)
minHeapProperty := func(parent, child int) bool {
return parent < child
}
h := genericheap.New(nums1, minHeapProperty)
for b.Loop() {
for _, num := range nums2 {
h.Push(num)
h.Pop()
}
}
}
func BenchmarkPushPop(b *testing.B) {
nums1 := RandomNumbers(1000)
nums2 := RandomNumbers(1000)
minHeapProperty := func(parent, child int) bool {
return parent < child
}
h := genericheap.New(nums1, minHeapProperty)
for b.Loop() {
for _, num := range nums2 {
h.PushPop(num)
}
}
}
func BenchmarkGenericMinHeap(b *testing.B) {
nums := RandomNumbers(1000)
minHeapProperty := func(parent, child int) bool {
return parent < child
}
h := genericheap.New([]int{}, minHeapProperty)
for b.Loop() {
for _, num := range nums {
h.Push(num)
}
for h.Len() > 0 {
h.Pop()
}
}
}
// An IntHeap is a min-heap of ints.
type IntHeap []int
func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *IntHeap) Push(x any) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(int))
}
func (h *IntHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func BenchmarkContainersMinHeap(b *testing.B) {
nums := RandomNumbers(1000)
h := &IntHeap{}
heap.Init(h)
for b.Loop() {
for _, num := range nums {
heap.Push(h, num)
}
for h.Len() > 0 {
heap.Pop(h)
}
}
}