Debian Codegraph @

collection_test.go

raw symbols SCIP
1 package graph
2
3 import (
4 "reflect"
5 "testing"
6 )
7
8 func TestPriorityQueue_Push(t *testing.T) {
9 tests := map[string]struct {
10 items []int
11 priorities []float64
12 expectedPriorityItems []*priorityItem[int]
13 }{
14 "queue with 5 elements": {
15 items: []int{10, 20, 30, 40, 50},
16 priorities: []float64{6, 8, 2, 7, 5},
17 expectedPriorityItems: []*priorityItem[int]{
18 {value: 20, priority: 8},
19 {value: 40, priority: 7},
20 {value: 10, priority: 6},
21 {value: 50, priority: 5},
22 {value: 30, priority: 2},
23 },
24 },
25 }
26
27 for name, test := range tests {
28 queue := newPriorityQueue[int]()
29
30 for i, item := range test.items {
31 queue.Push(item, test.priorities[i])
32 }
33
34 if queue.Len() != len(test.expectedPriorityItems) {
35 t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len())
36 }
37
38 popped := make([]int, queue.Len())
39
40 for queue.Len() > 0 {
41 item, _ := queue.Pop()
42 popped = append(popped, item)
43 }
44
45 n := len(popped)
46
47 for i, item := range test.expectedPriorityItems {
48 poppedItem := popped[n-1-i]
49 if item.value != poppedItem {
50 t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem)
51 }
52 }
53 }
54 }
55
56 func TestPriorityQueue_Pop(t *testing.T) {
57 tests := map[string]struct {
58 items []int
59 priorities []float64
60 expectedItem int
61 shouldFail bool
62 }{
63 "queue with 5 item": {
64 items: []int{10, 20, 30, 40, 50},
65 priorities: []float64{6, 8, 2, 7, 5},
66 expectedItem: 30,
67 shouldFail: false,
68 },
69 "queue with 1 item": {
70 items: []int{10},
71 priorities: []float64{6},
72 expectedItem: 10,
73 shouldFail: false,
74 },
75 "empty queue": {
76 items: []int{},
77 priorities: []float64{},
78 shouldFail: true,
79 },
80 }
81
82 for name, test := range tests {
83 queue := newPriorityQueue[int]()
84
85 for i, item := range test.items {
86 queue.Push(item, test.priorities[i])
87 }
88
89 item, err := queue.Pop()
90
91 if test.shouldFail != (err != nil) {
92 t.Fatalf("%s: error expectancy doesn't match: expected %v, got %v (error: %v)", name, test.shouldFail, (err != nil), err)
93 }
94
95 if item != test.expectedItem {
96 t.Errorf("%s: item expectancy doesn't match: expected %v, got %v", name, test.expectedItem, item)
97 }
98 }
99 }
100
101 func TestPriorityQueue_UpdatePriority(t *testing.T) {
102 tests := map[string]struct {
103 items []*priorityItem[int]
104 expectedPriorityItems []*priorityItem[int]
105 decreaseItem int
106 decreasePriority float64
107 }{
108 "decrease 30 to priority 5": {
109 items: []*priorityItem[int]{
110 {value: 40, priority: 40},
111 {value: 30, priority: 30},
112 {value: 20, priority: 20},
113 {value: 10, priority: 10},
114 },
115 decreaseItem: 30,
116 decreasePriority: 5,
117 expectedPriorityItems: []*priorityItem[int]{
118 {value: 40, priority: 40},
119 {value: 20, priority: 20},
120 {value: 10, priority: 10},
121 {value: 30, priority: 5},
122 },
123 },
124 "decrease a non-existent item": {
125 items: []*priorityItem[int]{
126 {value: 40, priority: 40},
127 {value: 30, priority: 30},
128 {value: 20, priority: 20},
129 {value: 10, priority: 10},
130 },
131 decreaseItem: 50,
132 decreasePriority: 10,
133 expectedPriorityItems: []*priorityItem[int]{
134 {value: 40, priority: 40},
135 {value: 30, priority: 30},
136 {value: 20, priority: 20},
137 {value: 10, priority: 10},
138 },
139 },
140 "increase 10 to priority 100": {
141 items: []*priorityItem[int]{
142 {value: 40, priority: 40},
143 {value: 30, priority: 30},
144 {value: 20, priority: 20},
145 {value: 10, priority: 10},
146 },
147 decreaseItem: 10,
148 decreasePriority: 100,
149 expectedPriorityItems: []*priorityItem[int]{
150 {value: 10, priority: 100},
151 {value: 40, priority: 40},
152 {value: 30, priority: 30},
153 {value: 20, priority: 20},
154 },
155 },
156 }
157
158 for name, test := range tests {
159 queue := newPriorityQueue[int]()
160
161 for _, item := range test.items {
162 queue.Push(item.value, item.priority)
163 }
164
165 queue.UpdatePriority(test.decreaseItem, test.decreasePriority)
166
167 if queue.Len() != len(test.expectedPriorityItems) {
168 t.Fatalf("%s: item length expectancy doesn't match: expected %v, got %v", name, len(test.expectedPriorityItems), queue.Len())
169 }
170
171 popped := make([]int, queue.Len())
172
173 for queue.Len() > 0 {
174 item, _ := queue.Pop()
175 popped = append(popped, item)
176 }
177
178 n := len(popped)
179
180 for i, item := range test.expectedPriorityItems {
181 poppedItem := popped[n-1-i]
182 if item.value != poppedItem {
183 t.Errorf("%s: item doesn't match: expected %v at index %d, got %v", name, item.value, i, poppedItem)
184 }
185 }
186 }
187 }
188
189 func TestPriorityQueue_Len(t *testing.T) {
190 tests := map[string]struct {
191 items []int
192 priorities []float64
193 expectedLen int
194 }{
195 "queue with 5 item": {
196 items: []int{10, 20, 30, 40, 50},
197 priorities: []float64{6, 8, 2, 7, 5},
198 expectedLen: 5,
199 },
200 "queue with 1 item": {
201 items: []int{10},
202 priorities: []float64{6},
203 expectedLen: 1,
204 },
205 "empty queue": {
206 items: []int{},
207 priorities: []float64{},
208 expectedLen: 0,
209 },
210 }
211
212 for name, test := range tests {
213 queue := newPriorityQueue[int]()
214
215 for i, item := range test.items {
216 queue.Push(item, test.priorities[i])
217 }
218
219 n := queue.Len()
220
221 if n != test.expectedLen {
222 t.Errorf("%s: length expectancy doesn't match: expected %v, got %v", name, test.expectedLen, n)
223 }
224 }
225 }
226
227 func Test_stackImpl_push(t *testing.T) {
228 type args[T any] struct {
229 t T
230 }
231 type testCase[T any] struct {
232 name string
233 s stackImpl[T]
234 args args[T]
235 }
236 tests := []testCase[int]{
237 {
238 "push 1",
239 stackImpl[int]{
240 elements: make([]int, 0),
241 },
242 args[int]{
243 t: 1,
244 },
245 },
246 }
247 for _, tt := range tests {
248 t.Run(tt.name, func(t *testing.T) {
249 tt.s.push(tt.args.t)
250 })
251 }
252 }
253
254 func Test_stackImpl_pop(t *testing.T) {
255 type testCase[T any] struct {
256 name string
257 s stackImpl[T]
258 want T
259 wantErr bool
260 }
261 tests := []testCase[int]{
262 {
263 "pop element",
264 stackImpl[int]{
265 elements: []int{1},
266 },
267 1,
268 false,
269 },
270 {
271 "pop element from empty stack",
272 stackImpl[int]{
273 elements: []int{},
274 },
275 0,
276 true,
277 },
278 }
279 for _, tt := range tests {
280 t.Run(tt.name, func(t *testing.T) {
281 got, err := tt.s.pop()
282 if (err != nil) != tt.wantErr {
283 t.Errorf("pop() error = %v, wantErr %v", err, tt.wantErr)
284 return
285 }
286 if !reflect.DeepEqual(got, tt.want) {
287 t.Errorf("pop() got = %v, want %v", got, tt.want)
288 }
289 })
290 }
291 }
292
293 func Test_stackImpl_top(t *testing.T) {
294 type testCase[T any] struct {
295 name string
296 s stackImpl[T]
297 want T
298 wantErr bool
299 }
300 tests := []testCase[int]{
301 {
302 "top element",
303 stackImpl[int]{
304 elements: []int{1},
305 },
306 1,
307 false,
308 },
309 {
310 "top element of empty stack",
311 stackImpl[int]{
312 elements: []int{},
313 },
314 0,
315 true,
316 },
317 }
318 for _, tt := range tests {
319 t.Run(tt.name, func(t *testing.T) {
320 got, err := tt.s.top()
321 if (err != nil) != tt.wantErr {
322 t.Errorf("top() error = %v, wantErr %v", err, tt.wantErr)
323 return
324 }
325 if !reflect.DeepEqual(got, tt.want) {
326 t.Errorf("top() got = %v, want %v", got, tt.want)
327 }
328 })
329 }
330 }
331
332 func Test_stackImpl_isEmpty(t *testing.T) {
333 type testCase[T any] struct {
334 name string
335 s stackImpl[T]
336 want bool
337 }
338 tests := []testCase[int]{
339 {
340 "empty",
341 stackImpl[int]{
342 elements: []int{},
343 },
344 true,
345 },
346 {
347 "not empty",
348 stackImpl[int]{
349 elements: []int{1},
350 },
351 false,
352 },
353 }
354 for _, tt := range tests {
355 t.Run(tt.name, func(t *testing.T) {
356 if got := tt.s.isEmpty(); got != tt.want {
357 t.Errorf("isEmpty() = %v, want %v", got, tt.want)
358 }
359 })
360 }
361 }
362
363 func Test_stackImpl_forEach(t *testing.T) {
364 type args[T any] struct {
365 f func(T)
366 }
367 type testCase[T any] struct {
368 name string
369 s stackImpl[T]
370 args args[T]
371 }
372 tests := []testCase[int]{
373 {
374 name: "forEach",
375 s: stackImpl[int]{
376 elements: []int{1, 2, 3, 4, 5, 6},
377 },
378 args: args[int]{
379 f: func(i int) {
380 },
381 },
382 },
383 }
384 for _, tt := range tests {
385 t.Run(tt.name, func(t *testing.T) {
386 tt.s.forEach(tt.args.f)
387 })
388 }
389 }