|
4 | 4 | "errors"
|
5 | 5 | "fmt"
|
6 | 6 | "testing"
|
| 7 | + "time" |
7 | 8 | )
|
8 | 9 |
|
9 | 10 | func TestNewKeyedPriorityQueue_NilCmp(t *testing.T) {
|
@@ -260,6 +261,126 @@ func TestKeyedPriorityQueue_Pop(t *testing.T) {
|
260 | 261 | })
|
261 | 262 | }
|
262 | 263 |
|
| 264 | +func TestKeyedPriorityQueue_BlockingPop(t *testing.T) { |
| 265 | + t.Run("Keys", func(t *testing.T) { |
| 266 | + pq := NewKeyedPriorityQueue[string](func(x, y int) bool { |
| 267 | + return x < y |
| 268 | + }) |
| 269 | + |
| 270 | + items := []struct { |
| 271 | + key string |
| 272 | + val int |
| 273 | + }{ |
| 274 | + {key: "fourth", val: 10}, |
| 275 | + {key: "second", val: 8}, |
| 276 | + {key: "third", val: 9}, |
| 277 | + {key: "first", val: 6}, |
| 278 | + {key: "last", val: 20}, |
| 279 | + } |
| 280 | + |
| 281 | + go func() { |
| 282 | + time.Sleep(10 * time.Millisecond) |
| 283 | + for _, item := range items { |
| 284 | + err := pq.Push(item.key, item.val) |
| 285 | + if err != nil { |
| 286 | + panic(fmt.Sprintf("Push(%v, %v): got unexpected error %v", item.key, item.val, err)) |
| 287 | + } |
| 288 | + } |
| 289 | + }() |
| 290 | + |
| 291 | + testCases := []struct { |
| 292 | + wantKey string |
| 293 | + wantValue int |
| 294 | + wantPeekKey string |
| 295 | + wantPeekValue int |
| 296 | + wantLen int |
| 297 | + }{ |
| 298 | + { |
| 299 | + wantKey: "fourth", //"first", |
| 300 | + wantValue: 10, // 6, |
| 301 | + wantPeekKey: "first", //"second", |
| 302 | + wantPeekValue: 6, // 8, |
| 303 | + wantLen: 4, |
| 304 | + }, |
| 305 | + { |
| 306 | + wantKey: "first", //"second", |
| 307 | + wantValue: 6, // 8, |
| 308 | + wantPeekKey: "second", //"third", |
| 309 | + wantPeekValue: 8, // 9, |
| 310 | + wantLen: 3, |
| 311 | + }, |
| 312 | + } |
| 313 | + |
| 314 | + for _, tc := range testCases { |
| 315 | + t.Run(fmt.Sprintf("%s_%d", tc.wantKey, tc.wantValue), func(t *testing.T) { |
| 316 | + gotKey, gotValue := pq.BlockingPop() |
| 317 | + |
| 318 | + if gotKey != tc.wantKey { |
| 319 | + t.Errorf("pq.BlockingPop(): got key %q; want %q", gotKey, tc.wantKey) |
| 320 | + } |
| 321 | + |
| 322 | + if gotValue != tc.wantValue { |
| 323 | + t.Errorf("pq.BlockingPop(): got value %d; want %d", gotValue, tc.wantValue) |
| 324 | + } |
| 325 | + |
| 326 | + time.Sleep(10 * time.Millisecond) |
| 327 | + |
| 328 | + gotPeekKey, gotPeekValue, ok := pq.Peek() |
| 329 | + if !ok { |
| 330 | + t.Fatal("got no min key and value in the priority queue") |
| 331 | + } |
| 332 | + |
| 333 | + if gotPeekKey != tc.wantPeekKey { |
| 334 | + t.Errorf("pq.Peek(): got key %q; want %q", gotPeekKey, tc.wantPeekKey) |
| 335 | + } |
| 336 | + |
| 337 | + if gotPeekValue != tc.wantPeekValue { |
| 338 | + t.Errorf("pq.Peek(): got value %d; want %d", gotPeekValue, tc.wantPeekValue) |
| 339 | + } |
| 340 | + |
| 341 | + if got := pq.Len(); got != tc.wantLen { |
| 342 | + t.Errorf("pq.Len(): got %d; want %d", got, tc.wantLen) |
| 343 | + } |
| 344 | + }) |
| 345 | + } |
| 346 | + }) |
| 347 | + |
| 348 | + /*t.Run("EmptyPQ", func(t *testing.T) { |
| 349 | + pq := NewKeyedPriorityQueue[string](func(x, y int) bool { |
| 350 | + return x < y |
| 351 | + }) |
| 352 | +
|
| 353 | + items := []struct { |
| 354 | + key string |
| 355 | + val int |
| 356 | + }{ |
| 357 | + {key: "fourth", val: 10}, |
| 358 | + {key: "second", val: 8}, |
| 359 | + {key: "third", val: 9}, |
| 360 | + {key: "first", val: 6}, |
| 361 | + {key: "last", val: 20}, |
| 362 | + } |
| 363 | +
|
| 364 | + go func() { |
| 365 | + time.Sleep(10 * time.Millisecond) |
| 366 | + for _, item := range items { |
| 367 | + err := pq.Push(item.key, item.val) |
| 368 | + if err != nil { |
| 369 | + panic(fmt.Sprintf("Push(%v, %v): got unexpected error %v", item.key, item.val, err)) |
| 370 | + } |
| 371 | + } |
| 372 | + }() |
| 373 | +
|
| 374 | + gotKey, gotValue := pq.BlockingPop() |
| 375 | + if gotKey != items[0].key { |
| 376 | + t.Errorf("pq.BlockingPop(): got key %q; want %q", gotKey, items[0].key) |
| 377 | + } |
| 378 | + if gotValue != items[0].val { |
| 379 | + t.Errorf("pq.BlockingPop(): got value %d; want %d", gotValue, items[0].val) |
| 380 | + } |
| 381 | + })*/ |
| 382 | +} |
| 383 | + |
263 | 384 | func TestKeyedPriorityQueue_Remove(t *testing.T) {
|
264 | 385 | pq := NewKeyedPriorityQueue[string](func(x, y int) bool {
|
265 | 386 | return x < y
|
|
0 commit comments