-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq_test.go
83 lines (65 loc) · 1.38 KB
/
pq_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
81
82
83
package timeoutmap
import (
"container/heap"
"fmt"
"testing"
)
func check(got, want interface{}) {
if got.(*HeapItem) != want.(*HeapItem) {
fmt.Printf("got: %v, want: %v\n", got, want)
panic("failed")
}
}
func TestPQ(t *testing.T) {
durationPQ := make(PQ, 0)
heap.Init(&durationPQ)
// Push-Pop test cases
heapItem1 := &HeapItem{
value: "item1",
priority: 15,
}
heapItem2 := &HeapItem{
value: "item2",
priority: 20,
}
heapItem3 := &HeapItem{
value: "item3",
priority: 10,
}
heap.Push(&durationPQ, heapItem1)
heap.Push(&durationPQ, heapItem2)
want := heapItem1
got := heap.Pop(&durationPQ)
check(got, want)
// Push-Peek-Pop test cases
heap.Push(&durationPQ, heapItem3)
want = heapItem3
got = durationPQ.Peek()
check(got, want)
got = heap.Pop(&durationPQ)
check(got, want)
want = heapItem2
got = heap.Pop(&durationPQ)
check(got, want)
// Push-Remove-Pop test cases
heap.Push(&durationPQ, heapItem1)
heap.Push(&durationPQ, heapItem2)
heap.Push(&durationPQ, heapItem3)
want = heapItem2
got = durationPQ.RemoveItem(heapItem2)
check(got, want)
want = heapItem3
got = heap.Pop(&durationPQ)
check(got, want)
want = heapItem1
got = heap.Pop(&durationPQ)
check(got, want)
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered")
} else {
panic("panic missing for out of range access")
}
}()
heap.Pop(&durationPQ)
}