forked from chen3feng/stl4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue_test.go
80 lines (71 loc) · 1.6 KB
/
priority_queue_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
package stl4go
import (
"fmt"
"testing"
)
func TestInterface(t *testing.T) {
_ = (Container)(NewPriorityQueue[int]())
}
func TestNewPriorityQueue(t *testing.T) {
pq := NewPriorityQueue[int]()
expectTrue(t, pq.IsEmpty())
expectEq(t, pq.Len(), 0)
}
func TestNewPriorityQueueOf(t *testing.T) {
pq := NewPriorityQueueOf(5, 4, 3, 2, 1)
expectEq(t, pq.Len(), 5)
}
func TestPriorityQueue_PushPop(t *testing.T) {
less := Less[int]
pq := NewPriorityQueueFunc(less)
for i := 5; i > 0; i-- {
pq.Push(i)
expectFalse(t, pq.IsEmpty())
}
var elements []int
for !pq.IsEmpty() {
elements = append(elements, pq.Pop())
}
expectTrue(t, pq.IsEmpty())
expectTrue(t, IsSorted(elements))
expectEq(t, len(elements), 5)
}
func TestPriorityQueueFunc_PushPop(t *testing.T) {
pq := NewPriorityQueueFunc(Less[int])
for i := 5; i > 0; i-- {
pq.Push(i)
}
var elements []int
for !pq.IsEmpty() {
elements = append(elements, pq.Pop())
}
expectTrue(t, pq.IsEmpty())
expectTrue(t, IsSorted(elements))
expectEq(t, len(elements), 5)
}
func TestPriorityQueue_Clear(t *testing.T) {
pq := NewPriorityQueue[int]()
pq.Clear()
expectTrue(t, pq.IsEmpty())
expectEq(t, pq.Len(), 0)
pq = NewPriorityQueueOf(1, 2, 3)
pq.Clear()
expectTrue(t, pq.IsEmpty())
expectEq(t, pq.Len(), 0)
}
// This example inserts several ints into an IntHeap, checks the minimum,
// and removes them in order of priority.
func ExamplePriorityQueue() {
h := NewPriorityQueue[int]()
h.Push(3)
h.Push(2)
h.Push(1)
h.Push(5)
fmt.Printf("minimum: %d\n", h.Top())
for h.Len() > 0 {
fmt.Printf("%d ", h.Pop())
}
// Output:
// minimum: 1
// 1 2 3 5
}