aboutsummaryrefslogtreecommitdiff
path: root/internal/heap/heap_test.go
blob: 467957029797a469d0305eb4a3e3b2b2e8858f6d (about) (plain) (blame)
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
package heap_test

import (
	"slices"
	"testing"

	internalheap "codeberg.org/lindenii/furgit/internal/heap"
)

func TestHeapAscending(t *testing.T) {
	t.Parallel()

	heap := internalheap.New(func(left, right int) bool {
		return left < right
	})

	for _, value := range []int{5, 1, 4, 3, 2} {
		heap.Push(value)
	}

	var got []int
	for !heap.Empty() {
		value, ok := heap.Pop()
		if !ok {
			t.Fatal("Pop should succeed")
		}

		got = append(got, value)
	}

	want := []int{1, 2, 3, 4, 5}
	if !slices.Equal(got, want) {
		t.Fatalf("pop order = %v, want %v", got, want)
	}
}

func TestHeapPeek(t *testing.T) {
	t.Parallel()

	heap := internalheap.New(func(left, right int) bool {
		return left > right
	})

	if _, ok := heap.Peek(); ok {
		t.Fatal("Peek on empty heap should miss")
	}

	heap.Push(1)
	heap.Push(3)
	heap.Push(2)

	value, ok := heap.Peek()
	if !ok {
		t.Fatal("Peek should succeed")
	}

	if value != 3 {
		t.Fatalf("Peek = %d, want 3", value)
	}

	if heap.Len() != 3 {
		t.Fatalf("Len after Peek = %d, want 3", heap.Len())
	}
}