forked from dominikbraun/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection_test.go
210 lines (186 loc) · 5.17 KB
/
collection_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
package graph
import (
"testing"
)
func assertPriorityQueue[T comparable](t *testing.T, testName string, q *priorityQueue[T], expectedItems []*priorityItem[T]) {
if q.Len() != len(expectedItems) {
t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", testName, len(expectedItems), q.Len())
}
poppedList := make([]T, q.Len())
for q.Len() > 0 {
poppedItem, _ := q.Pop()
poppedList = append(poppedList, poppedItem)
}
// Each time we pop from the given queue, it will return the item with the smallest priority.
// So we compare the expected item with popped list from the other direction.
n := len(poppedList)
for i, expectedItem := range expectedItems {
comparedToItem := poppedList[n-1-i]
if comparedToItem != expectedItem.value {
t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", testName, expectedItem.value, i, comparedToItem)
}
}
}
func TestPriorityQueue_Push(t *testing.T) {
tests := map[string]struct {
items []int
priorities []float64
expectedPriorityItems []*priorityItem[int]
}{
"queue with 5 elements": {
items: []int{10, 20, 30, 40, 50},
priorities: []float64{6, 8, 2, 7, 5},
expectedPriorityItems: []*priorityItem[int]{
{value: 20, priority: 8},
{value: 40, priority: 7},
{value: 10, priority: 6},
{value: 50, priority: 5},
{value: 30, priority: 2},
},
},
}
for name, test := range tests {
queue := newPriorityQueue[int]()
for i, item := range test.items {
queue.Push(item, test.priorities[i])
}
assertPriorityQueue(t, name, queue, test.expectedPriorityItems)
}
}
func TestPriorityQueue_Pop(t *testing.T) {
tests := map[string]struct {
items []int
priorities []float64
expectedItem int
shouldFail bool
}{
"queue with 5 item": {
items: []int{10, 20, 30, 40, 50},
priorities: []float64{6, 8, 2, 7, 5},
expectedItem: 30,
shouldFail: false,
},
"queue with 1 item": {
items: []int{10},
priorities: []float64{6},
expectedItem: 10,
shouldFail: false,
},
"empty queue": {
items: []int{},
priorities: []float64{},
shouldFail: true,
},
}
for name, test := range tests {
queue := newPriorityQueue[int]()
for i, item := range test.items {
queue.Push(item, test.priorities[i])
}
item, err := queue.Pop()
if test.shouldFail != (err != nil) {
t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err)
}
if item != test.expectedItem {
t.Errorf("%s: item expectancy doesn't match: expected %v, got %v", name, test.expectedItem, item)
}
}
}
func TestPriorityQueue_UpdatePriority(t *testing.T) {
tests := map[string]struct {
items []*priorityItem[int]
expectedPriorityItems []*priorityItem[int]
decreaseItem int
decreasePriority float64
}{
"decrease 30 to priority 5": {
items: []*priorityItem[int]{
{value: 40, priority: 40},
{value: 30, priority: 30},
{value: 20, priority: 20},
{value: 10, priority: 10},
},
decreaseItem: 30,
decreasePriority: 5,
expectedPriorityItems: []*priorityItem[int]{
{value: 40, priority: 40},
{value: 20, priority: 20},
{value: 10, priority: 10},
{value: 30, priority: 5},
},
},
"decrease a non-existent item": {
items: []*priorityItem[int]{
{value: 40, priority: 40},
{value: 30, priority: 30},
{value: 20, priority: 20},
{value: 10, priority: 10},
},
decreaseItem: 50,
decreasePriority: 10,
expectedPriorityItems: []*priorityItem[int]{
{value: 40, priority: 40},
{value: 30, priority: 30},
{value: 20, priority: 20},
{value: 10, priority: 10},
},
},
"increase 10 to priority 100": {
items: []*priorityItem[int]{
{value: 40, priority: 40},
{value: 30, priority: 30},
{value: 20, priority: 20},
{value: 10, priority: 10},
},
decreaseItem: 10,
decreasePriority: 100,
expectedPriorityItems: []*priorityItem[int]{
{value: 10, priority: 100},
{value: 40, priority: 40},
{value: 30, priority: 30},
{value: 20, priority: 20},
},
},
}
for name, test := range tests {
queue := newPriorityQueue[int]()
for _, item := range test.items {
queue.Push(item.value, item.priority)
}
queue.UpdatePriority(test.decreaseItem, test.decreasePriority)
assertPriorityQueue(t, name, queue, test.expectedPriorityItems)
}
}
func TestPriorityQueue_Len(t *testing.T) {
tests := map[string]struct {
items []int
priorities []float64
expectedLen int
}{
"queue with 5 item": {
items: []int{10, 20, 30, 40, 50},
priorities: []float64{6, 8, 2, 7, 5},
expectedLen: 5,
},
"queue with 1 item": {
items: []int{10},
priorities: []float64{6},
expectedLen: 1,
},
"empty queue": {
items: []int{},
priorities: []float64{},
expectedLen: 0,
},
}
for name, test := range tests {
queue := newPriorityQueue[int]()
for i, item := range test.items {
queue.Push(item, test.priorities[i])
}
n := queue.Len()
if n != test.expectedLen {
t.Errorf("%s: length expectancy doesn't match: expected %v, got %v", name, test.expectedLen, n)
}
}
}