-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq_test.go
125 lines (93 loc) · 2.9 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
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
package pqueue_test
import (
"testing"
pqueue "github.com/andela-sjames/priorityQueue"
"github.com/stretchr/testify/assert"
)
func TestMaxPriorityQueue(t *testing.T) {
// defaults to max heap if Min option is
// not set to true
maxheap := pqueue.NewHeap(pqueue.Options{})
assert := assert.New(t)
maxheap.InsertPriority("Visit China", 2)
maxheap.InsertPriority("Visit Japan", 7)
maxheap.InsertPriority("Eat Pizza", 2)
maxheap.InsertPriority("Run marathon", 11)
maxheap.InsertPriority("Go Kart", 3)
maxheap.InsertPriority("Ice Skating", 5)
maxheap.InsertPriority("Do Kung Fu", 4)
maxheap.InsertPriority("Get a boyfriend", 10)
t.Run("ShowPriority should return item and priority", func(t *testing.T) {
item, p := maxheap.ShowPriority()
assert.Equal("Run marathon", item)
assert.Equal(11, p)
})
t.Run("Poll an object from the queue", func(t *testing.T) {
maxheap.InsertPriority("Watch Netflix", 13)
maxheap.InsertPriority("Get a boyfriend", 10)
maxheap.InsertPriority("Visit France", 21)
item, p := maxheap.Poll()
assert.Equal("Visit France", item)
assert.Equal(21, p)
})
t.Run("Remove an object from the heap", func(t *testing.T) {
l := maxheap.Length()
assert.Equal(10, l)
done := maxheap.Remove(11)
l = maxheap.Length()
assert.Equal(true, done)
assert.Equal(9, l)
})
t.Run("Heap table can be returned", func(t *testing.T) {
v := maxheap.ShowHashTable()
assert.NotNil(v)
})
t.Run("Heap content can be shown", func(t *testing.T) {
s := maxheap.ShowHeap()
assert.NotNil(s)
})
}
func TestMinPriorityQueue(t *testing.T) {
// Set Min option to true for minheap
minheap := pqueue.NewHeap(pqueue.Options{
Min: true,
})
assert := assert.New(t)
minheap.InsertPriority("Visit China", 2)
minheap.InsertPriority("Visit Japan", 7)
minheap.InsertPriority("Eat Pizza", 2)
minheap.InsertPriority("Run marathon", 11)
minheap.InsertPriority("Go Kart", 3)
minheap.InsertPriority("Ice Skating", 5)
minheap.InsertPriority("Do Kung Fu", 4)
minheap.InsertPriority("Get a boyfriend", 10)
t.Run("ShowPriority should return item and priority", func(t *testing.T) {
item, p := minheap.ShowPriority()
assert.Equal("Visit China", item)
assert.Equal(2, p)
})
t.Run("Poll an object from the queue", func(t *testing.T) {
minheap.InsertPriority("Watch Netflix", 13)
minheap.InsertPriority("Get a boyfriend", 10)
minheap.InsertPriority("Visit France", 21)
item, p := minheap.Poll()
assert.Equal("Visit China", item)
assert.Equal(2, p)
})
t.Run("Remove an object from the heap", func(t *testing.T) {
l := minheap.Length()
assert.Equal(10, l)
done := minheap.Remove(11)
l = minheap.Length()
assert.Equal(true, done)
assert.Equal(9, l)
})
t.Run("Heap table can be returned", func(t *testing.T) {
v := minheap.ShowHashTable()
assert.NotNil(v)
})
t.Run("Heap content can be shown", func(t *testing.T) {
s := minheap.ShowHeap()
assert.NotNil(s)
})
}