TestHeap_Update tests Heap.Update and ensures that heap invariant is preserved after adding items.
(t *testing.T)
| 227 | // TestHeap_Update tests Heap.Update and ensures that heap invariant is |
| 228 | // preserved after adding items. |
| 229 | func TestHeap_Update(t *testing.T) { |
| 230 | h := NewHeap(testHeapObjectKeyFunc, compareInts) |
| 231 | h.Add(mkHeapObj("foo", 10)) |
| 232 | h.Add(mkHeapObj("bar", 1)) |
| 233 | h.Add(mkHeapObj("bal", 31)) |
| 234 | h.Add(mkHeapObj("baz", 11)) |
| 235 | |
| 236 | // Update an item to a value that should push it to the head. |
| 237 | h.Update(mkHeapObj("baz", 0)) |
| 238 | if h.data.queue[0] != "baz" || h.data.items["baz"].index != 0 { |
| 239 | t.Fatalf("expected baz to be at the head") |
| 240 | } |
| 241 | item, err := h.Pop() |
| 242 | if e, a := 0, item.(testHeapObject).val; err != nil || a != e { |
| 243 | t.Fatalf("expected %d, got %d", e, a) |
| 244 | } |
| 245 | // Update bar to push it farther back in the queue. |
| 246 | h.Update(mkHeapObj("bar", 100)) |
| 247 | if h.data.queue[0] != "foo" || h.data.items["foo"].index != 0 { |
| 248 | t.Fatalf("expected foo to be at the head") |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // TestHeap_Get tests Heap.Get. |
| 253 | func TestHeap_Get(t *testing.T) { |