/
pqueue_test.go
213 lines (183 loc) · 4.09 KB
/
pqueue_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2011 Graham Anderson <graham@andtech.eu>.
//
// All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pqueue_test
import (
"container/heap"
"fmt"
pq "github.com/gnanderson/pqueue"
"math/rand"
"sort"
"testing"
)
type message struct {
importance int
}
func (m *message) Priority() int {
return m.importance
}
func initCollection() *pq.Queue {
q := pq.NewQueue()
for i := 0; i < 100; i++ {
q.Push(&message{rand.Int()})
}
return q
}
func initSmallCollection(n int) *pq.Queue {
q := pq.NewQueue()
for i := 0; i < 10; i++ {
q.Push(&message{rand.Intn(n)})
}
return q
}
func TestAddElement(t *testing.T) {
q := initCollection()
fmt.Println("Length:", q.Len())
if q.Len() != 100 {
t.Fatalf("Add() fails, wrong number of elements.")
}
}
func TestLess(t *testing.T) {
q := pq.NewQueue()
q.Push(&message{132})
q.Push(&message{256})
if !q.Less(0, 1) {
t.Fatalf("Less() fails.")
}
}
func TestSwap(t *testing.T) {
q := pq.NewQueue()
q.Push(&message{132})
q.Push(&message{256})
q.Swap(0, 1)
x, _ := q.Member(0)
y, _ := q.Member(1)
if x.Priority() != 256 || y.Priority() != 132 {
t.Fatalf("Swap() fails.")
}
}
func TestSort(t *testing.T) {
q := initCollection()
sort.Sort(q)
for i := 1; i < q.Len(); i++ {
x, err := q.Member(i - 1)
if err != nil {
t.Fatalf("Invalid number of elements")
}
y, err := q.Member(i)
if err != nil {
t.Fatalf("Invalid number of elements")
}
if x.Priority() > y.Priority() {
t.Fatalf("Sort fails, not sorted")
}
}
}
func TestPushSlice(t *testing.T) {
q := initCollection()
tmp := make([]pq.Queueable, 10)
for i := 0; i < 10; i++ {
tmp[i] = &message{rand.Int()}
}
q.AddSlice(tmp)
if q.Len() != 110 {
t.Fatalf("AddSlice() fails, wrong number of elements.")
}
x, _ := heap.Pop(q).(pq.Queueable)
for _, v := range q.Collection() {
if x.Priority() > v.Priority() {
t.Fatalf("AddSlice() fails, Q not min-heaped.")
}
}
}
func TestPopElement(t *testing.T) {
q := pq.NewQueue()
q.Push(&message{128})
q.Push(&message{256})
q.Push(&message{512})
x, ok := q.Pop().(pq.Queueable)
if !ok {
t.Fatalf("Pop() fails.")
}
if x.Priority() != 128 && q.Len() != 2 {
t.Fatalf("Pop() fails, wrong element or Q is wrong size.")
}
}
func TestGetElement(t *testing.T) {
q := initCollection()
for i := 0; i < 100; i++ {
if _, err := q.Member(i); err != nil {
t.Fatalf("Member() fails, wrong number of elements.")
}
}
}
func TestOutOfBounds(t *testing.T) {
q := initCollection()
_, err := q.Member(100)
if err != pq.QERR_OUT_OF_BOUNDS {
t.Fatalf("Member() fails, out of bounds not detected.", q.Len())
}
}
func TestReturnCollection(t *testing.T) {
q := initCollection()
c := q.Collection()
if len(c) != 100 {
t.Fatalf("Collection() fails, wrong number of elements.")
}
for _, v := range q.Collection() {
if v, ok := v.(pq.Queueable); !ok {
t.Fatalf("Collection() fails", v)
}
}
}
func TestHeapInitAndPop(t *testing.T) {
q := initSmallCollection(50)
heap.Init(q)
x, _ := heap.Pop(q).(pq.Queueable)
for _, y := range q.Collection() {
if x.Priority() > y.Priority() {
t.Fatalf("heap.Pop() fails, not min element")
}
}
}
func TestHeapInitAndPush(t *testing.T) {
q := initSmallCollection(200)
heap.Init(q)
heap.Push(q, &message{0})
if q.Len() != 11 {
t.Fatalf("heap.Push() fails, q is wrong size")
}
m, err := q.Member(0)
if err != nil {
t.Fatalf("Error ¬_¬")
}
if m.Priority() != 0 {
t.Fatalf("heap.Push() fails, pushed valued not found at bottom of heap.")
}
}
func TestHeapRemove(t *testing.T) {
q := pq.NewQueue()
q.Push(&message{8})
q.Push(&message{3})
q.Push(&message{9})
q.Push(&message{1})
q.Push(&message{10})
q.Push(&message{2})
q.Push(&message{5})
q.Push(&message{6})
q.Push(&message{7})
q.Push(&message{4})
heap.Init(q)
r := heap.Remove(q, 5).(pq.Queueable)
fmt.Println("removed:", r)
if r.Priority() != 9 {
t.Fatalf("Remove() fails, incorrect member removed.", r.Priority())
}
mb, _ := q.Member(0)
if mb.Priority() != 1 {
t.Fatalf("Remove() fails, unexpected value for min member")
}
}