-
Notifications
You must be signed in to change notification settings - Fork 3
/
priority_queue_test.go
60 lines (56 loc) · 1.63 KB
/
priority_queue_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
package vibrant
import (
"testing"
)
var priorityFunction = func(data interface{}) uint32 {
return data.(uint32)
}
func TestDefaultPriorityQueue(t *testing.T) {
q := NewPriorityQueue(3, priorityFunction)
testData := [][]uint32{
{1, 2, 3},
{1, 3, 2},
{2, 1, 3},
{2, 3, 1},
{3, 1, 2},
{3, 2, 1},
}
for _, data := range testData {
if q.Len() != 0 {
t.Fatalf("Expected queue to be empty.\n")
}
for _, item := range data {
q.Offer(item)
}
for expected := q.Len(); expected > 0; expected-- {
actual := priorityFunction(q.Poll())
if uint32(expected) != actual {
t.Errorf("Expected item with priority %v instead of %v.\n", expected, actual)
}
}
}
}
func TestDefaultPriorityQueue_MultipleOffer(t *testing.T) {
q := NewPriorityQueue(5, priorityFunction)
q.Offer(uint32(6), uint32(5), uint32(4), uint32(3), uint32(2), uint32(1))
for expected := q.Len(); expected > 0; expected-- {
actual := priorityFunction(q.Poll())
if uint32(expected) != actual {
t.Errorf("Expected item with priority %v instead of %v.\n", expected, actual)
}
}
q.Offer(uint32(1), uint32(2), uint32(3), uint32(4), uint32(5), uint32(6))
for expected := q.Len(); expected > 0; expected-- {
actual := priorityFunction(q.Poll())
if uint32(expected) != actual {
t.Errorf("Expected item with priority %v instead of %v.\n", expected, actual)
}
}
q.Offer(uint32(1), uint32(5), uint32(3), uint32(4), uint32(2), uint32(6))
for expected := q.Len(); expected > 0; expected-- {
actual := priorityFunction(q.Poll())
if uint32(expected) != actual {
t.Errorf("Expected item with priority %v instead of %v.\n", expected, actual)
}
}
}