-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpq_test.go
77 lines (65 loc) · 1.82 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
package lazylru
import (
"testing"
heap "github.com/TriggerMail/lazylru/containers/heap"
"github.com/stretchr/testify/require"
)
func TestPopEmpty(t *testing.T) {
pq := itemPQ[string, int]{}
require.Nil(t, pq.Pop())
}
func TestPushPop(t *testing.T) {
pq := itemPQ[string, int]{}
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 0,
key: "schlage",
})
pqi := heap.Pop[*item[string, int]](&pq)
require.Equal(t, "schlage", pqi.key)
require.Equal(t, 13, pqi.value)
}
func TestPushPopOrdered(t *testing.T) {
pq := itemPQ[string, int]{}
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 0,
key: "schlage",
})
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 1,
key: "kwikset",
})
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 2,
key: "abloy",
})
require.Equal(t, "schlage", heap.Pop[*item[string, int]](&pq).key)
require.Equal(t, "kwikset", heap.Pop[*item[string, int]](&pq).key)
require.Equal(t, "abloy", heap.Pop[*item[string, int]](&pq).key)
}
func TestPushPopUpdate(t *testing.T) {
pq := itemPQ[string, int]{}
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 0,
key: "schlage",
})
heap.Push[*item[string, int]](&pq, &item[string, int]{
value: 13,
insertNumber: 2,
key: "abloy",
})
kwi := &item[string, int]{
value: 13,
insertNumber: 1,
key: "kwikset",
}
heap.Push[*item[string, int]](&pq, kwi)
pq.update(kwi, 3)
require.Equal(t, "schlage", heap.Pop[*item[string, int]](&pq).key)
require.Equal(t, "abloy", heap.Pop[*item[string, int]](&pq).key)
require.Equal(t, "kwikset", heap.Pop[*item[string, int]](&pq).key)
}