TestHeap_AddIfNotPresent tests Heap.AddIfNotPresent and ensures that heap invariant is preserved after adding items.
(t *testing.T)
| 146 | // TestHeap_AddIfNotPresent tests Heap.AddIfNotPresent and ensures that heap |
| 147 | // invariant is preserved after adding items. |
| 148 | func TestHeap_AddIfNotPresent(t *testing.T) { |
| 149 | h := NewHeap(testHeapObjectKeyFunc, compareInts) |
| 150 | h.AddIfNotPresent(mkHeapObj("foo", 10)) |
| 151 | h.AddIfNotPresent(mkHeapObj("bar", 1)) |
| 152 | h.AddIfNotPresent(mkHeapObj("baz", 11)) |
| 153 | h.AddIfNotPresent(mkHeapObj("zab", 30)) |
| 154 | h.AddIfNotPresent(mkHeapObj("foo", 13)) // This is not added. |
| 155 | |
| 156 | if len := len(h.data.items); len != 4 { |
| 157 | t.Errorf("unexpected number of items: %d", len) |
| 158 | } |
| 159 | if val := h.data.items["foo"].obj.(testHeapObject).val; val != 10 { |
| 160 | t.Errorf("unexpected value: %d", val) |
| 161 | } |
| 162 | item, err := h.Pop() |
| 163 | if e, a := 1, item.(testHeapObject).val; err != nil || a != e { |
| 164 | t.Fatalf("expected %d, got %d", e, a) |
| 165 | } |
| 166 | item, err = h.Pop() |
| 167 | if e, a := 10, item.(testHeapObject).val; err != nil || a != e { |
| 168 | t.Fatalf("expected %d, got %d", e, a) |
| 169 | } |
| 170 | // bar is already popped. Let's add another one. |
| 171 | h.AddIfNotPresent(mkHeapObj("bar", 14)) |
| 172 | item, err = h.Pop() |
| 173 | if e, a := 11, item.(testHeapObject).val; err != nil || a != e { |
| 174 | t.Fatalf("expected %d, got %d", e, a) |
| 175 | } |
| 176 | item, err = h.Pop() |
| 177 | if e, a := 14, item.(testHeapObject).val; err != nil || a != e { |
| 178 | t.Fatalf("expected %d, got %d", e, a) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // TestHeap_Delete tests Heap.Delete and ensures that heap invariant is |
| 183 | // preserved after deleting items. |
nothing calls this directly
no test coverage detected