You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
977 B

  1. package queue
  2. import (
  3. "container/heap"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestQueue(t *testing.T) {
  8. // Some items and their priorities.
  9. items := map[string]int64{
  10. "id0": 2,
  11. "id1": 5,
  12. "id2": 4,
  13. }
  14. // Create a priority queue, put the items in it, and
  15. // establish the priority queue (heap) invariants.
  16. pq := make(PriorityQueue, len(items))
  17. i := 0
  18. for value, priority := range items {
  19. pq[i] = &Item{
  20. value: value,
  21. priority: priority,
  22. index: i,
  23. }
  24. i++
  25. }
  26. heap.Init(&pq)
  27. assert.Equal(t, pq.Look().value, "id0")
  28. // Insert a new item and then modify its priority.
  29. item := &Item{
  30. value: "id3",
  31. priority: 3,
  32. }
  33. heap.Push(&pq, item)
  34. pq.update(item, item.value, 1)
  35. assert.Equal(t, pq.Look().value, "id3")
  36. // Take the items out; they arrive in increasing priority order.
  37. for pq.Len() > 0 {
  38. _ = heap.Pop(&pq).(*Item)
  39. // fmt.Println("priority", item.priority, ":", item.value)
  40. }
  41. assert.Nil(t, pq.Look())
  42. }