forked from aQuaYi/LeetCode-in-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPriorityQueue_test.go
53 lines (45 loc) · 976 Bytes
/
PriorityQueue_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
package kit
import (
"container/heap"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_priorityQueue(t *testing.T) {
ast := assert.New(t)
// Some items and their priorities.
items := map[string]int{
"banana": 2, "apple": 1, "pear": 3,
}
// Create a priority queue, put the items in it, and
// establish the priority queue (heap) invariants.
pq := make(PQ, len(items))
i := 0
for value, priority := range items {
pq[i] = &entry{
key: value,
priority: priority,
index: i,
}
i++
}
heap.Init(&pq)
// Insert a new item and then modify its priority.
it := &entry{
key: "orange",
priority: 5,
}
heap.Push(&pq, it)
pq.update(it, it.key, 0)
// Some items and their priorities.
expected := []string{
"orange",
"apple",
"banana",
"pear",
}
// Take the items out; they arrive in decreasing priority order.
for pq.Len() > 0 {
it := heap.Pop(&pq).(*entry)
ast.Equal(expected[it.priority], it.key)
}
}