TestHeap_BulkAdd tests Heap.BulkAdd functionality and ensures that all the items given to BulkAdd are added to the queue before Pop reads them.
(t *testing.T)
| 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. |
| 110 | func TestHeap_BulkAdd(t *testing.T) { |
| 111 | h := NewHeap(testHeapObjectKeyFunc, compareInts) |
| 112 | const amount = 500 |
| 113 | // Insert items in the heap in opposite orders in a go routine. |
| 114 | go func() { |
| 115 | l := []interface{}{} |
| 116 | for i := amount; i > 0; i-- { |
| 117 | l = append(l, mkHeapObj(string([]rune{'a', rune(i)}), i)) |
| 118 | } |
| 119 | h.BulkAdd(l) |
| 120 | }() |
| 121 | prevNum := -1 |
| 122 | for i := 0; i < amount; i++ { |
| 123 | obj, err := h.Pop() |
| 124 | num := obj.(testHeapObject).val.(int) |
| 125 | // All the items must be sorted. |
| 126 | if err != nil || prevNum >= num { |
| 127 | t.Errorf("got %v out of order, last was %v", obj, prevNum) |
| 128 | } |
| 129 | prevNum = num |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // TestHeapEmptyPop tests that pop returns properly after heap is closed. |
| 134 | func TestHeapEmptyPop(t *testing.T) { |