MCPcopy
hub / github.com/kubernetes/client-go / TestHeap_AddIfNotPresent

Function TestHeap_AddIfNotPresent

tools/cache/heap_test.go:148–180  ·  view source on GitHub ↗

TestHeap_AddIfNotPresent tests Heap.AddIfNotPresent and ensures that heap invariant is preserved after adding items.

(t *testing.T)

Source from the content-addressed store, hash-verified

146// TestHeap_AddIfNotPresent tests Heap.AddIfNotPresent and ensures that heap
147// invariant is preserved after adding items.
148func 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.

Callers

nothing calls this directly

Calls 5

AddIfNotPresentMethod · 0.95
PopMethod · 0.95
NewHeapFunction · 0.85
mkHeapObjFunction · 0.85
ErrorfMethod · 0.65

Tested by

no test coverage detected