-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
49 lines (43 loc) · 920 Bytes
/
queue_test.go
File metadata and controls
49 lines (43 loc) · 920 Bytes
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
package timing
import (
"container/heap"
"fmt"
"testing"
)
func TestQueue(t *testing.T) {
items := map[string]uint32{
"banana": 3, "apple": 2, "pear": 4, "pear2": 4,
}
// Create a priority queue, put the items in it, and
// establish the priority queue (heap) invariants.
pq := make(Queue, len(items))
i := 0
for param, when := range items {
pq[i] = &Item{
Param: param,
Timed: when,
}
i++
}
heap.Init(&pq)
// Insert a new item and then modify its priority.
item := &Item{
Event: "orange",
Param: "xatarstiarsd",
Timed: 13,
}
heap.Push(&pq, item)
// Insert a new item and then modify its priority.
item = &Item{
Event: "banana",
Param: "xatarstiarsd",
Timed: 9,
}
heap.Push(&pq, item)
// Take the items out; they arrive in decreasing priority order.
for pq.Len() > 0 {
item := heap.Pop(&pq).(*Item)
fmt.Printf("%+v\n", item)
}
// x := heap.Pop(&pq) // panic
}