-
Notifications
You must be signed in to change notification settings - Fork 3
/
heapq_test.go
289 lines (248 loc) · 6.84 KB
/
heapq_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
package heapq_test
import (
"cmp"
"math/rand/v2"
"sort"
"testing"
"github.com/creachadair/mds/compare"
"github.com/creachadair/mds/heapq"
"github.com/creachadair/mds/internal/mdtest"
gocmp "github.com/google/go-cmp/cmp"
)
var _ mdtest.Shared[any] = (*heapq.Queue[any])(nil)
var intCompare = cmp.Compare[int]
var revIntCompare = compare.Reversed(cmp.Compare[int])
func TestHeap(t *testing.T) {
t.Run("New", func(t *testing.T) {
runTests(t, heapq.New(intCompare))
})
t.Run("NewWithData", func(t *testing.T) {
buf := make([]int, 0, 64)
runTests(t, heapq.NewWithData(intCompare, buf))
})
}
func runTests(t *testing.T, q *heapq.Queue[int]) {
t.Helper()
check := func(want ...int) {
t.Helper()
var got []int
for v := range q.Each {
got = append(got, v)
}
if diff := gocmp.Diff(want, got); diff != "" {
t.Errorf("Queue contents (+want, -got):\n%s", diff)
t.Logf("Got: %v", got)
t.Logf("Want: %v", want)
}
if len(want) != 0 {
if got := q.Front(); got != want[0] {
t.Errorf("Front: got %v, want %v", got, want[0])
}
}
if got := q.Len(); got != len(want) {
t.Errorf("Len: got %v, want %v", got, len(want))
}
}
checkAdd := func(v, want int) {
if got := q.Add(v); got != want {
t.Errorf("Add(%v): got %v, want %v", v, got, want)
}
}
checkPop := func(want int, wantok bool) {
got, ok := q.Pop()
if got != want || ok != wantok {
t.Errorf("Pop: got (%v, %v), want (%v, %v)", got, ok, want, wantok)
}
}
checkRemove := func(n, want int, wantok bool) {
got, ok := q.Remove(n)
if got != want || ok != wantok {
t.Errorf("Remove(%d): got (%v, %v), want (%v, %v)", n, got, ok, want, wantok)
}
}
check()
checkPop(0, false)
checkAdd(10, 0)
check(10)
checkAdd(5, 0)
check(5, 10)
checkAdd(3, 0)
check(3, 5, 10)
checkAdd(4, 1)
check(3, 4, 10, 5)
checkPop(3, true)
checkPop(4, true)
checkPop(5, true)
checkPop(10, true)
checkPop(0, false)
check()
q.Set([]int{1, 2, 3, 4, 5})
check(1, 2, 3, 4, 5)
checkPop(1, true)
q.Set([]int{1, 2, 3, 4, 5, 6})
checkRemove(0, 1, true)
checkRemove(2, 3, true)
checkRemove(5, 0, false)
q.Clear()
check()
q.Set([]int{15, 3, 9, 4, 8, 2, 11, 20, 11, 17, 1})
check(1, 3, 2, 4, 8, 9, 11, 20, 11, 17, 15) // constructed by hand
if got := extract(q); !sort.IntsAreSorted(got) {
t.Errorf("Queue contents are out of order: %v", got)
}
}
func TestOrder(t *testing.T) {
const inputSize = 5000
const inputRange = 100000
makeInput := func() []int {
input := make([]int, inputSize)
for i := range input {
input[i] = rand.IntN(inputRange) - (inputRange / 2)
}
return input
}
t.Run("Ascending", func(t *testing.T) {
q := heapq.New(intCompare)
q.Set(makeInput())
if got := extract(q); !sort.IntsAreSorted(got) {
t.Errorf("Queue contents are out of order: %v", got)
}
})
t.Run("Descending", func(t *testing.T) {
q := heapq.New(revIntCompare)
q.Set(makeInput())
got := extract(q)
if !sort.IsSorted(sort.Reverse(sort.IntSlice(got))) {
t.Errorf("Queue contents are out of order: %v", got)
}
})
t.Run("Reorder", func(t *testing.T) {
q := heapq.New(intCompare)
q.Set([]int{17, 3, 11, 2, 7, 5, 13})
if got, want := q.Front(), 2; got != want {
t.Errorf("Front: got %v, want %v", got, want)
}
q.Reorder(revIntCompare)
if got, want := q.Front(), 17; got != want {
t.Errorf("Front: got %v, want %v", got, want)
}
got := extract(q)
if !sort.IsSorted(sort.Reverse(sort.IntSlice(got))) {
t.Errorf("Results are out of order: %v", got)
}
})
}
func TestNewWithData(t *testing.T) {
const bufSize = 100 // N.B. must be even, so we can fill halves
// Preallocate a buffer and populate part of it with some data.
buf := make([]int, 0, bufSize)
var want []int
for range bufSize / 2 {
z := rand.IntN(500) - 250
buf = append(buf, z)
want = append(want, z) // keep track of what we added.
}
// Give buf over to the queue, then add more stuff so we can check that the
// queue took over the array correctly.
q := heapq.NewWithData(intCompare, buf)
// Add some more stuff via the queue.
for range bufSize / 2 {
z := rand.IntN(500) - 250
q.Add(z)
want = append(want, z)
}
// Check that the queue used the same array. You are specifically NOT
// supposed to do this, messing with the array outside the queue, but here
// we need to check that the queue did the right thing.
got := buf[:len(want)]
sort.Ints(got)
sort.Ints(want)
if diff := gocmp.Diff(want, got); diff != "" {
t.Errorf("Queue contents (+want, -got):\n%s", diff)
}
}
func TestSort(t *testing.T) {
longIn := make([]int, 50)
for i := range longIn {
longIn[i] = rand.IntN(1000) - 250
}
longOut := make([]int, len(longIn))
copy(longOut, longIn)
sort.Ints(longOut)
lt := func(a, b int) int { return a - b }
gt := func(a, b int) int { return b - a }
_, _ = lt, gt
tests := []struct {
name string
cmp func(a, b int) int
input, want []int
}{
{"Nil", intCompare, nil, nil},
{"Empty", intCompare, []int{}, nil},
{"Single-LT", intCompare, []int{11}, []int{11}},
{"Single-GT", revIntCompare, []int{11}, []int{11}},
{"Ascend", intCompare, []int{9, 1, 4, 11}, []int{1, 4, 9, 11}},
{"Descend", revIntCompare, []int{9, 1, 4, 11}, []int{11, 9, 4, 1}},
{"Long", intCompare, longIn, longOut},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
in := append([]int(nil), tc.input...)
heapq.Sort(tc.cmp, in)
if diff := gocmp.Diff(tc.want, in); diff != "" {
t.Errorf("Sort (-want, +got):\n%s", diff)
}
})
}
}
func TestUpdate(t *testing.T) {
m := make(map[string]int) // tracks the offsets of strings in the queue
up := func(s string, p int) { m[s] = p } // update the offsets map
q := heapq.New(cmp.Compare[string]).Update(up)
// Verify that all the elements know their current offset correctly.
check := func() {
for i := 0; i < q.Len(); i++ {
s, _ := q.Peek(i)
if m[s] != i {
t.Errorf("At pos %d: %s is at %d instead", i, s, m[s])
}
}
}
check() // empty
// Check that Set assigns positions to the elements added.
q.Set([]string{"m", "z", "t", "a", "k", "b"})
check()
// Check that Add updates positions correctly.
q.Add("c")
check()
// Check that we can add an element and remove it by its assigned position.
q.Add("j")
check()
oldp := m["j"]
t.Logf("Added j at pos=%d", oldp)
q.Remove(oldp)
check()
// After removal, the element retains its last position.
if m["j"] != oldp {
t.Errorf("After Remove j: p=%d, want %d", m["j"], oldp)
}
var got []string
for !q.IsEmpty() {
s, _ := q.Pop()
got = append(got, s)
if m[s] != 0 {
t.Errorf("Pop: got %q at p=%d, want p=0", s, m[s])
}
}
if diff := gocmp.Diff(got, []string{"a", "b", "c", "k", "m", "t", "z"}); diff != "" {
t.Errorf("Values (-got, +want):\n%s", diff)
}
}
func extract[T any](q *heapq.Queue[T]) []T {
all := make([]T, 0, q.Len())
for !q.IsEmpty() {
all = append(all, q.Front())
q.Pop()
}
return all
}