gogame-old/queue/queue_test.go
queue/queue_test.goRaw
1package queue
2
3import (
4 "container/heap"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
10func TestQueue(t *testing.T) {
11 // Some items and their priorities.
12 items := map[string]int64{
13 "id0": 2,
14 "id1": 5,
15 "id2": 4,
16 }
17
18 // Create a priority queue, put the items in it, and
19 // establish the priority queue (heap) invariants.
20 pq := make(PriorityQueue, len(items))
21 i := 0
22 for value, priority := range items {
23 pq[i] = &Item{
24 value: value,
25 priority: priority,
26 index: i,
27 }
28 i++
29 }
30 heap.Init(&pq)
31
32 assert.Equal(t, pq.Look().value, "id0")
33
34 // Insert a new item and then modify its priority.
35 item := &Item{
36 value: "id3",
37 priority: 3,
38 }
39 heap.Push(&pq, item)
40 pq.update(item, item.value, 1)
41 assert.Equal(t, pq.Look().value, "id3")
42
43 // Take the items out; they arrive in increasing priority order.
44 for pq.Len() > 0 {
45 _ = heap.Pop(&pq).(*Item)
46 // fmt.Println("priority", item.priority, ":", item.value)
47 }
48 assert.Nil(t, pq.Look())
49}