-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathheap_test.go
244 lines (207 loc) · 4.6 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
package heap_test
import (
"fmt"
"testing"
"github.com/zyedidia/generic/heap"
)
func TestCreateHeapFromSlice(t *testing.T) {
cases := []struct {
name string
array []int
sorted []int
less func(int, int) bool
}{
{
name: "empty",
array: []int{},
less: func(a, b int) bool { return a < b },
},
{
name: "non-empty (minheap)",
array: []int{5, 3, 6, 2, 4, 1},
sorted: []int{1, 2, 3, 4, 5, 6},
less: func(a, b int) bool { return a < b },
},
{
name: "non-empty (maxheap)",
array: []int{5, 7, 9, 2, -1},
sorted: []int{9, 7, 5, 2, -1},
less: func(a, b int) bool { return a > b },
},
}
for _, c := range cases {
t.Run(c.name+" From", func(t *testing.T) {
// copy array
array := make([]int, len(c.array))
copy(array, c.array)
heap := heap.From(c.less, array...)
for i, v := range c.sorted {
peek, ok := heap.Pop()
if !ok {
t.Errorf("pop not ok, idx: %v", i)
}
if peek != v {
t.Errorf("peek not equal, idx: %v", i)
}
}
})
t.Run(c.name+" FromArray", func(t *testing.T) {
heap := heap.FromSlice(c.less, c.array)
for i, v := range c.sorted {
peek, ok := heap.Pop()
if !ok {
t.Errorf("pop not ok, idx: %v", i)
}
if peek != v {
t.Errorf("peek not equal, idx: %v", i)
}
}
})
}
}
func TestBasic(t *testing.T) {
cases := []struct {
name string
data []int
peeksInPush []int
peeksInPop []int
less func(int, int) bool
}{
{
name: "minheap",
data: []int{5, 7, 9, 2, -1},
peeksInPush: []int{5, 5, 5, 2, -1},
peeksInPop: []int{-1, 2, 5, 7, 9},
less: func(a, b int) bool { return a < b },
},
{
name: "maxheap",
data: []int{5, 3, 6, 2, 4, 1},
peeksInPush: []int{5, 5, 6, 6, 6, 6},
peeksInPop: []int{6, 5, 4, 3, 2, 1},
less: func(a, b int) bool { return a > b },
},
{
name: "minheap",
data: []int{9, 10, 8, 7, 9, 4, 8, 4, -1, -1, 2, 3, 5},
peeksInPush: []int{9, 9, 8, 7, 7, 4, 4, 4, -1, -1, -1, -1, -1},
peeksInPop: []int{-1, -1, 2, 3, 4, 4, 5, 7, 8, 8, 9, 9, 10},
less: func(a, b int) bool { return a < b },
},
{
name: "maxheap",
data: []int{1, 5, 4, 7, 0, 8, 4, -1, 9, 2, 3, 5},
peeksInPush: []int{1, 5, 5, 7, 7, 8, 8, 8, 9, 9, 9, 9},
peeksInPop: []int{9, 8, 7, 5, 5, 4, 4, 3, 2, 1, 0, -1},
less: func(a, b int) bool { return a >= b },
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
heap := heap.New(c.less)
if heap.Size() != 0 {
t.Errorf("heap len not 0")
}
// push all elements
for i, v := range c.data {
heap.Push(v)
peek, ok := heap.Peek()
if !ok {
t.Errorf("peek not ok, idx: %v", i)
}
if peek != c.peeksInPush[i] {
t.Errorf("peek not equal, idx: %v", i)
}
}
if heap.Size() != len(c.data) {
t.Errorf("heap len not equal to data len")
}
// pop all elements
for idx, peek := range c.peeksInPop {
v, ok := heap.Pop()
if !ok {
t.Errorf("pop not ok, idx: %v", idx)
}
if v != peek {
t.Errorf("peek not equal, idx: %v", idx)
}
}
if heap.Size() != 0 {
t.Errorf("heap len not 0")
}
})
}
}
func TestPopAndPeekOnEmpty(t *testing.T) {
heap := heap.New(func(a, b int) bool { return a < b })
var v int
var ok bool
_, ok = heap.Peek()
if ok {
t.Errorf("peek returns ok on empty heap")
}
_, ok = heap.Pop()
if ok {
t.Errorf("pop returns ok on empty heap")
}
heap.Push(1)
v, ok = heap.Peek()
if !ok {
t.Errorf("peek not ok on non-empty heap")
}
if v != 1 {
t.Errorf("expect peek %v, but got %v", 1, v)
}
v, ok = heap.Pop()
if !ok {
t.Errorf("pop not ok on non-empty heap")
}
if v != 1 {
t.Errorf("expect pop %v, but got %v", 1, v)
}
}
func Example() {
heap := heap.New(func(a, b int) bool { return a < b })
heap.Push(5)
heap.Push(2)
heap.Push(3)
v, _ := heap.Pop()
fmt.Println(v)
v, _ = heap.Peek()
fmt.Println(v)
// Output:
// 2
// 3
}
func ExampleFrom() {
heap := heap.From(func(a, b int) bool { return a < b }, 5, 2, 3)
v, _ := heap.Pop()
fmt.Println(v)
v, _ = heap.Peek()
fmt.Println(v)
// Output:
// 2
// 3
}
func ExampleFromSlice() {
heap := heap.FromSlice(func(a, b int) bool { return a > b }, []int{-1, 5, 2, 3})
v, _ := heap.Pop()
fmt.Println(v)
v, _ = heap.Peek()
fmt.Println(v)
// Output:
// 5
// 3
}
func ExampleHeap_Pop() {
heap := heap.New(func(a, b int) bool { return a < b })
heap.Push(5)
v, ok := heap.Pop()
fmt.Println(v, ok)
// pop on empty
v, ok = heap.Pop()
fmt.Println(v, ok)
// Output:
// 5 true
// 0 false
}