Debian Codegraph @

trees/binaryheap/binaryheap_test.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package binaryheap
6
7 import (
8 "encoding/json"
9 "math/rand"
10 "slices"
11 "strings"
12 "testing"
13 )
14
15 func TestBinaryHeapPush(t *testing.T) {
16 heap := New[int]()
17
18 if actualValue := heap.Empty(); actualValue != true {
19 t.Errorf("Got %v expected %v", actualValue, true)
20 }
21
22 heap.Push(3)
23 heap.Push(2)
24 heap.Push(1)
25
26 if actualValue, expectedValue := heap.Values(), []int{1, 2, 3}; !slices.Equal(actualValue, expectedValue) {
27 t.Errorf("Got %v expected %v", actualValue, expectedValue)
28 }
29 if actualValue := heap.Empty(); actualValue != false {
30 t.Errorf("Got %v expected %v", actualValue, false)
31 }
32 if actualValue := heap.Size(); actualValue != 3 {
33 t.Errorf("Got %v expected %v", actualValue, 3)
34 }
35 if actualValue, ok := heap.Peek(); actualValue != 1 || !ok {
36 t.Errorf("Got %v expected %v", actualValue, 1)
37 }
38 }
39
40 func TestBinaryHeapPushBulk(t *testing.T) {
41 heap := New[int]()
42
43 heap.Push(15, 20, 3, 1, 2)
44
45 if actualValue, expectedValue := heap.Values(), []int{1, 2, 3, 15, 20}; !slices.Equal(actualValue, expectedValue) {
46 t.Errorf("Got %v expected %v", actualValue, expectedValue)
47 }
48 if actualValue, ok := heap.Pop(); actualValue != 1 || !ok {
49 t.Errorf("Got %v expected %v", actualValue, 1)
50 }
51 }
52
53 func TestBinaryHeapPop(t *testing.T) {
54 heap := New[int]()
55
56 if actualValue := heap.Empty(); actualValue != true {
57 t.Errorf("Got %v expected %v", actualValue, true)
58 }
59
60 heap.Push(3)
61 heap.Push(2)
62 heap.Push(1)
63 heap.Pop()
64
65 if actualValue, ok := heap.Peek(); actualValue != 2 || !ok {
66 t.Errorf("Got %v expected %v", actualValue, 2)
67 }
68 if actualValue, ok := heap.Pop(); actualValue != 2 || !ok {
69 t.Errorf("Got %v expected %v", actualValue, 2)
70 }
71 if actualValue, ok := heap.Pop(); actualValue != 3 || !ok {
72 t.Errorf("Got %v expected %v", actualValue, 3)
73 }
74 if actualValue, ok := heap.Pop(); actualValue != 0 || ok {
75 t.Errorf("Got %v expected %v", actualValue, nil)
76 }
77 if actualValue := heap.Empty(); actualValue != true {
78 t.Errorf("Got %v expected %v", actualValue, true)
79 }
80 if actualValue := heap.Values(); len(actualValue) != 0 {
81 t.Errorf("Got %v expected %v", actualValue, "[]")
82 }
83 }
84
85 func TestBinaryHeapRandom(t *testing.T) {
86 heap := New[int]()
87
88 rand.Seed(3)
89 for i := 0; i < 10000; i++ {
90 r := int(rand.Int31n(30))
91 heap.Push(r)
92 }
93
94 prev, _ := heap.Pop()
95 for !heap.Empty() {
96 curr, _ := heap.Pop()
97 if prev > curr {
98 t.Errorf("Heap property invalidated. prev: %v current: %v", prev, curr)
99 }
100 prev = curr
101 }
102 }
103
104 func TestBinaryHeapIteratorOnEmpty(t *testing.T) {
105 heap := New[int]()
106 it := heap.Iterator()
107 for it.Next() {
108 t.Errorf("Shouldn't iterate on empty heap")
109 }
110 }
111
112 func TestBinaryHeapIteratorNext(t *testing.T) {
113 heap := New[int]()
114 heap.Push(3)
115 heap.Push(2)
116 heap.Push(1)
117
118 it := heap.Iterator()
119 count := 0
120 for it.Next() {
121 count++
122 index := it.Index()
123 value := it.Value()
124 switch index {
125 case 0:
126 if actualValue, expectedValue := value, 1; actualValue != expectedValue {
127 t.Errorf("Got %v expected %v", actualValue, expectedValue)
128 }
129 case 1:
130 if actualValue, expectedValue := value, 2; actualValue != expectedValue {
131 t.Errorf("Got %v expected %v", actualValue, expectedValue)
132 }
133 case 2:
134 if actualValue, expectedValue := value, 3; actualValue != expectedValue {
135 t.Errorf("Got %v expected %v", actualValue, expectedValue)
136 }
137 default:
138 t.Errorf("Too many")
139 }
140 if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
141 t.Errorf("Got %v expected %v", actualValue, expectedValue)
142 }
143 }
144 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
145 t.Errorf("Got %v expected %v", actualValue, expectedValue)
146 }
147 }
148
149 func TestBinaryHeapIteratorPrev(t *testing.T) {
150 heap := New[int]()
151 heap.Push(3)
152 heap.Push(2)
153 heap.Push(1)
154
155 it := heap.Iterator()
156 for it.Next() {
157 }
158 count := 0
159 for it.Prev() {
160 count++
161 index := it.Index()
162 value := it.Value()
163 switch index {
164 case 0:
165 if actualValue, expectedValue := value, 1; actualValue != expectedValue {
166 t.Errorf("Got %v expected %v", actualValue, expectedValue)
167 }
168 case 1:
169 if actualValue, expectedValue := value, 2; actualValue != expectedValue {
170 t.Errorf("Got %v expected %v", actualValue, expectedValue)
171 }
172 case 2:
173 if actualValue, expectedValue := value, 3; actualValue != expectedValue {
174 t.Errorf("Got %v expected %v", actualValue, expectedValue)
175 }
176 default:
177 t.Errorf("Too many")
178 }
179 if actualValue, expectedValue := index, 3-count; actualValue != expectedValue {
180 t.Errorf("Got %v expected %v", actualValue, expectedValue)
181 }
182 }
183 if actualValue, expectedValue := count, 3; actualValue != expectedValue {
184 t.Errorf("Got %v expected %v", actualValue, expectedValue)
185 }
186 }
187
188 func TestBinaryHeapIteratorBegin(t *testing.T) {
189 heap := New[int]()
190 it := heap.Iterator()
191 it.Begin()
192 heap.Push(2)
193 heap.Push(3)
194 heap.Push(1)
195 for it.Next() {
196 }
197 it.Begin()
198 it.Next()
199 if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
200 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
201 }
202 }
203
204 func TestBinaryHeapIteratorEnd(t *testing.T) {
205 heap := New[int]()
206 it := heap.Iterator()
207
208 if index := it.Index(); index != -1 {
209 t.Errorf("Got %v expected %v", index, -1)
210 }
211
212 it.End()
213 if index := it.Index(); index != 0 {
214 t.Errorf("Got %v expected %v", index, 0)
215 }
216
217 heap.Push(3)
218 heap.Push(2)
219 heap.Push(1)
220 it.End()
221 if index := it.Index(); index != heap.Size() {
222 t.Errorf("Got %v expected %v", index, heap.Size())
223 }
224
225 it.Prev()
226 if index, value := it.Index(), it.Value(); index != heap.Size()-1 || value != 3 {
227 t.Errorf("Got %v,%v expected %v,%v", index, value, heap.Size()-1, 3)
228 }
229 }
230
231 func TestBinaryHeapIteratorFirst(t *testing.T) {
232 heap := New[int]()
233 it := heap.Iterator()
234 if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
235 t.Errorf("Got %v expected %v", actualValue, expectedValue)
236 }
237 heap.Push(3)
238 heap.Push(2)
239 heap.Push(1)
240 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
241 t.Errorf("Got %v expected %v", actualValue, expectedValue)
242 }
243 if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
244 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
245 }
246 }
247
248 func TestBinaryHeapIteratorLast(t *testing.T) {
249 tree := New[int]()
250 it := tree.Iterator()
251 if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
252 t.Errorf("Got %v expected %v", actualValue, expectedValue)
253 }
254 tree.Push(2)
255 tree.Push(3)
256 tree.Push(1)
257 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
258 t.Errorf("Got %v expected %v", actualValue, expectedValue)
259 }
260 if index, value := it.Index(), it.Value(); index != 2 || value != 3 {
261 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, 3)
262 }
263 }
264
265 func TestBinaryHeapIteratorNextTo(t *testing.T) {
266 // Sample seek function, i.e. string starting with "b"
267 seek := func(index int, value string) bool {
268 return strings.HasSuffix(value, "b")
269 }
270
271 // NextTo (empty)
272 {
273 tree := New[string]()
274 it := tree.Iterator()
275 for it.NextTo(seek) {
276 t.Errorf("Shouldn't iterate on empty list")
277 }
278 }
279
280 // NextTo (not found)
281 {
282 tree := New[string]()
283 tree.Push("xx")
284 tree.Push("yy")
285 it := tree.Iterator()
286 for it.NextTo(seek) {
287 t.Errorf("Shouldn't iterate on empty list")
288 }
289 }
290
291 // NextTo (found)
292 {
293 tree := New[string]()
294 tree.Push("aa")
295 tree.Push("bb")
296 tree.Push("cc")
297 it := tree.Iterator()
298 it.Begin()
299 if !it.NextTo(seek) {
300 t.Errorf("Shouldn't iterate on empty list")
301 }
302 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
303 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
304 }
305 if !it.Next() {
306 t.Errorf("Should go to first element")
307 }
308 if index, value := it.Index(), it.Value(); index != 2 || value != "cc" {
309 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
310 }
311 if it.Next() {
312 t.Errorf("Should not go past last element")
313 }
314 }
315 }
316
317 func TestBinaryHeapIteratorPrevTo(t *testing.T) {
318 // Sample seek function, i.e. string starting with "b"
319 seek := func(index int, value string) bool {
320 return strings.HasSuffix(value, "b")
321 }
322
323 // PrevTo (empty)
324 {
325 tree := New[string]()
326 it := tree.Iterator()
327 it.End()
328 for it.PrevTo(seek) {
329 t.Errorf("Shouldn't iterate on empty list")
330 }
331 }
332
333 // PrevTo (not found)
334 {
335 tree := New[string]()
336 tree.Push("xx")
337 tree.Push("yy")
338 it := tree.Iterator()
339 it.End()
340 for it.PrevTo(seek) {
341 t.Errorf("Shouldn't iterate on empty list")
342 }
343 }
344
345 // PrevTo (found)
346 {
347 tree := New[string]()
348 tree.Push("aa")
349 tree.Push("bb")
350 tree.Push("cc")
351 it := tree.Iterator()
352 it.End()
353 if !it.PrevTo(seek) {
354 t.Errorf("Shouldn't iterate on empty list")
355 }
356 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" {
357 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
358 }
359 if !it.Prev() {
360 t.Errorf("Should go to first element")
361 }
362 if index, value := it.Index(), it.Value(); index != 0 || value != "aa" {
363 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
364 }
365 if it.Prev() {
366 t.Errorf("Should not go before first element")
367 }
368 }
369 }
370
371 func TestBinaryHeapSerialization(t *testing.T) {
372 heap := New[string]()
373
374 heap.Push("c")
375 heap.Push("b")
376 heap.Push("a")
377
378 var err error
379 assert := func() {
380 if actualValue, expectedValue := heap.Values(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
381 t.Errorf("Got %v expected %v", actualValue, expectedValue)
382 }
383 if actualValue := heap.Size(); actualValue != 3 {
384 t.Errorf("Got %v expected %v", actualValue, 3)
385 }
386 if actualValue, ok := heap.Peek(); actualValue != "a" || !ok {
387 t.Errorf("Got %v expected %v", actualValue, "a")
388 }
389 if err != nil {
390 t.Errorf("Got error %v", err)
391 }
392 }
393
394 assert()
395
396 bytes, err := heap.ToJSON()
397 assert()
398
399 err = heap.FromJSON(bytes)
400 assert()
401
402 bytes, err = json.Marshal([]interface{}{"a", "b", "c", heap})
403 if err != nil {
404 t.Errorf("Got error %v", err)
405 }
406
407 intHeap := New[int]()
408 err = json.Unmarshal([]byte(`[1,2,3]`), &intHeap)
409 if err != nil {
410 t.Errorf("Got error %v", err)
411 }
412 if actualValue, expectedValue := intHeap.Values(), []int{1, 2, 3}; !slices.Equal(actualValue, expectedValue) {
413 t.Errorf("Got %v expected %v", actualValue, expectedValue)
414 }
415 }
416
417 func TestBTreeString(t *testing.T) {
418 c := New[int]()
419 c.Push(1)
420 if !strings.HasPrefix(c.String(), "BinaryHeap") {
421 t.Errorf("String should start with container name")
422 }
423 }
424
425 func benchmarkPush(b *testing.B, heap *Heap[int], size int) {
426 for i := 0; i < b.N; i++ {
427 for n := 0; n < size; n++ {
428 heap.Push(n)
429 }
430 }
431 }
432
433 func benchmarkPop(b *testing.B, heap *Heap[int], size int) {
434 for i := 0; i < b.N; i++ {
435 for n := 0; n < size; n++ {
436 heap.Pop()
437 }
438 }
439 }
440
441 func BenchmarkBinaryHeapPop100(b *testing.B) {
442 b.StopTimer()
443 size := 100
444 heap := New[int]()
445 for n := 0; n < size; n++ {
446 heap.Push(n)
447 }
448 b.StartTimer()
449 benchmarkPop(b, heap, size)
450 }
451
452 func BenchmarkBinaryHeapPop1000(b *testing.B) {
453 b.StopTimer()
454 size := 1000
455 heap := New[int]()
456 for n := 0; n < size; n++ {
457 heap.Push(n)
458 }
459 b.StartTimer()
460 benchmarkPop(b, heap, size)
461 }
462
463 func BenchmarkBinaryHeapPop10000(b *testing.B) {
464 b.StopTimer()
465 size := 10000
466 heap := New[int]()
467 for n := 0; n < size; n++ {
468 heap.Push(n)
469 }
470 b.StartTimer()
471 benchmarkPop(b, heap, size)
472 }
473
474 func BenchmarkBinaryHeapPop100000(b *testing.B) {
475 b.StopTimer()
476 size := 100000
477 heap := New[int]()
478 for n := 0; n < size; n++ {
479 heap.Push(n)
480 }
481 b.StartTimer()
482 benchmarkPop(b, heap, size)
483 }
484
485 func BenchmarkBinaryHeapPush100(b *testing.B) {
486 b.StopTimer()
487 size := 100
488 heap := New[int]()
489 b.StartTimer()
490 benchmarkPush(b, heap, size)
491 }
492
493 func BenchmarkBinaryHeapPush1000(b *testing.B) {
494 b.StopTimer()
495 size := 1000
496 heap := New[int]()
497 for n := 0; n < size; n++ {
498 heap.Push(n)
499 }
500 b.StartTimer()
501 benchmarkPush(b, heap, size)
502 }
503
504 func BenchmarkBinaryHeapPush10000(b *testing.B) {
505 b.StopTimer()
506 size := 10000
507 heap := New[int]()
508 for n := 0; n < size; n++ {
509 heap.Push(n)
510 }
511 b.StartTimer()
512 benchmarkPush(b, heap, size)
513 }
514
515 func BenchmarkBinaryHeapPush100000(b *testing.B) {
516 b.StopTimer()
517 size := 100000
518 heap := New[int]()
519 for n := 0; n < size; n++ {
520 heap.Push(n)
521 }
522 b.StartTimer()
523 benchmarkPush(b, heap, size)
524 }