-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpriorityQueue_test.go
More file actions
47 lines (38 loc) · 973 Bytes
/
priorityQueue_test.go
File metadata and controls
47 lines (38 loc) · 973 Bytes
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
package priorityQueue
import (
"testing"
"fmt"
"math/rand"
"math"
)
type Something struct {
msg string
priority int
}
func (x *Something) HigherPriorityThan(o Interface) bool {
return x.priority > o.(*Something).priority
}
func TestPriorityQueue(t *testing.T) {
const ITEMS = 500000
pq := New()
for i := 0; i < ITEMS; i++ {
priority := rand.Intn(20)
something := &Something{msg: fmt.Sprintf("Priority %d", priority), priority: priority}
pq.Push(something)
}
// Take the items out; they arrive in decreasing priority order.
expected := math.MaxInt64
for i := 0; i < ITEMS; i++ {
item := pq.Pop()
if item == nil {
t.Error("Expecting a value. Got nil")
}
if got := item.(*Something).priority; got >= expected {
t.Errorf("Expecting a priority equal or lower than %d, got %d", expected, got)
}
}
// Now, the queue is empty, so we expect a nil value
if item := pq.Pop(); item != nil {
t.Error("Expecting a nil value.")
}
}