Tests Heap.Add and ensures that heap invariant is preserved after adding items.
(t *testing.T)
| 78 | |
| 79 | // Tests Heap.Add and ensures that heap invariant is preserved after adding items. |
| 80 | func TestHeap_Add(t *testing.T) { |
| 81 | h := NewHeap(testHeapObjectKeyFunc, compareInts) |
| 82 | h.Add(mkHeapObj("foo", 10)) |
| 83 | h.Add(mkHeapObj("bar", 1)) |
| 84 | h.Add(mkHeapObj("baz", 11)) |
| 85 | h.Add(mkHeapObj("zab", 30)) |
| 86 | h.Add(mkHeapObj("foo", 13)) // This updates "foo". |
| 87 | |
| 88 | item, err := h.Pop() |
| 89 | if e, a := 1, item.(testHeapObject).val; err != nil || a != e { |
| 90 | t.Fatalf("expected %d, got %d", e, a) |
| 91 | } |
| 92 | item, err = h.Pop() |
| 93 | if e, a := 11, item.(testHeapObject).val; err != nil || a != e { |
| 94 | t.Fatalf("expected %d, got %d", e, a) |
| 95 | } |
| 96 | h.Delete(mkHeapObj("baz", 11)) // Nothing is deleted. |
| 97 | h.Add(mkHeapObj("foo", 14)) // foo is updated. |
| 98 | item, err = h.Pop() |
| 99 | if e, a := 14, item.(testHeapObject).val; err != nil || a != e { |
| 100 | t.Fatalf("expected %d, got %d", e, a) |
| 101 | } |
| 102 | item, err = h.Pop() |
| 103 | if e, a := 30, item.(testHeapObject).val; err != nil || a != e { |
| 104 | t.Fatalf("expected %d, got %d", e, a) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // TestHeap_BulkAdd tests Heap.BulkAdd functionality and ensures that all the |
| 109 | // items given to BulkAdd are added to the queue before Pop reads them. |