-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityqueue_test.go
89 lines (78 loc) · 1.89 KB
/
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
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
84
85
86
87
88
89
package priorityqueue_test
import (
"testing"
"github.com/jamesrom/priorityqueue"
"github.com/stretchr/testify/assert"
)
type x string
func (s x) Priority() int {
return len(s)
}
func TestNew(t *testing.T) {
q := priorityqueue.New(
x("this"),
x("is"),
x("the"),
x("longest"),
)
assert.EqualValues(t, "longest", q.Pop())
assert.EqualValues(t, "this", q.Pop())
assert.EqualValues(t, "the", q.Pop())
assert.EqualValues(t, "is", q.Pop())
}
func TestNewWithOrderable(t *testing.T) {
q := priorityqueue.NewWithOrderable(1, 2, 3)
assert.Equal(t, 3, q.Pop())
}
func TestNewWithComparator(t *testing.T) {
q := priorityqueue.NewWithComparator(
func(a, b int) bool { return a > b },
1, 2, 3,
)
assert.Equal(t, 3, q.Pop())
assert.Equal(t, 2, q.Pop())
assert.Equal(t, 1, q.Pop())
}
func TestPush(t *testing.T) {
q := priorityqueue.NewWithComparator(
func(a, b float64) bool { return a > b },
1.1, 0.2, 300,
)
q.Push(4.1235489e5)
q.Push(50.314)
assert.Equal(t, 412354.89, q.Pop())
assert.Equal(t, 300.0, q.Pop())
assert.Equal(t, 50.314, q.Pop())
assert.Equal(t, 1.1, q.Pop())
assert.Equal(t, 0.2, q.Pop())
}
func TestPeek(t *testing.T) {
q := priorityqueue.New(
x("this"),
x("is"),
x("the"),
x("longest"),
)
assert.EqualValues(t, "longest", q.Peek())
assert.EqualValues(t, "longest", q.Pop())
assert.EqualValues(t, "this", q.Peek())
assert.EqualValues(t, "this", q.Pop())
assert.EqualValues(t, "the", q.Peek())
assert.EqualValues(t, "the", q.Peek())
assert.EqualValues(t, "the", q.Peek())
assert.EqualValues(t, "the", q.Pop())
assert.EqualValues(t, "is", q.Pop())
}
func TestOverflow(t *testing.T) {
q := priorityqueue.NewWithOrderable(1, 2, 3)
assert.Equal(t, 3, q.Pop())
assert.Equal(t, 2, q.Pop())
assert.Equal(t, 1, q.Pop())
assert.Panics(t, func() {
q.Pop()
})
}
func TestLen(t *testing.T) {
q := priorityqueue.NewWithOrderable(1, 2, 3)
assert.Equal(t, 3, q.Len())
}