|
1 | 1 | package queue_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "errors"
|
5 | 7 | "reflect"
|
6 | 8 | "sort"
|
@@ -362,6 +364,24 @@ func TestPriority(t *testing.T) {
|
362 | 364 | }
|
363 | 365 | })
|
364 | 366 | })
|
| 367 | + |
| 368 | + t.Run("MarshalJSON", func(t *testing.T) { |
| 369 | + t.Parallel() |
| 370 | + |
| 371 | + elems := []int{3, 2, 1} |
| 372 | + |
| 373 | + priorityQueue := queue.NewPriority(elems, lessAscending) |
| 374 | + |
| 375 | + marshaled, err := json.Marshal(priorityQueue) |
| 376 | + if err != nil { |
| 377 | + t.Fatalf("expected no error, got %v", err) |
| 378 | + } |
| 379 | + |
| 380 | + expectedMarshaled := []byte(`[3,2,1]`) |
| 381 | + if !bytes.Equal(expectedMarshaled, marshaled) { |
| 382 | + t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled) |
| 383 | + } |
| 384 | + }) |
365 | 385 | }
|
366 | 386 |
|
367 | 387 | func FuzzPriority(f *testing.F) {
|
@@ -411,6 +431,22 @@ func FuzzPriority(f *testing.F) {
|
411 | 431 | }
|
412 | 432 |
|
413 | 433 | func BenchmarkPriorityQueue(b *testing.B) {
|
| 434 | + b.Run("MarshalJSON", func(b *testing.B) { |
| 435 | + priorityQueue := queue.NewPriority[int]( |
| 436 | + []int{2, 3, 1, 5, 4}, |
| 437 | + func(elem, otherElem int) bool { |
| 438 | + return elem < otherElem |
| 439 | + }, |
| 440 | + ) |
| 441 | + |
| 442 | + b.ReportAllocs() |
| 443 | + b.ResetTimer() |
| 444 | + |
| 445 | + for i := 0; i <= b.N; i++ { |
| 446 | + _, _ = priorityQueue.MarshalJSON() |
| 447 | + } |
| 448 | + }) |
| 449 | + |
414 | 450 | b.Run("Peek", func(b *testing.B) {
|
415 | 451 | priorityQueue := queue.NewPriority([]int{1}, func(elem, otherElem int) bool {
|
416 | 452 | return elem < otherElem
|
|
0 commit comments