MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / test_heapPopulateRand

Function test_heapPopulateRand

tests/unit/test_heap.c:209–266  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

207}
208
209static void test_heapPopulateRand(void) {
210 // seed random
211 srand(time(NULL));
212
213 // create a new heap
214 heap_t *heap = Heap_new(cmp, NULL);
215
216 int n = 100;
217 int elements[n];
218 int sorted_elements[n];
219
220 //--------------------------------------------------------------------------
221 // generate n random numbers within the range 0..999
222 //--------------------------------------------------------------------------
223
224 // insert elements 0..n
225 for(int i = 0; i < n; i++) {
226 elements[i] = rand() % 1000;
227 sorted_elements[i] = elements[i];
228 TEST_ASSERT(Heap_offer(&heap, elements + i) == 0);
229 }
230
231 // validate number of elements in heap
232 TEST_ASSERT(Heap_count(heap) == n);
233
234 // validate expected elements are indeed in heap
235 for(int i = 0; i < n; i++) {
236 TEST_ASSERT(Heap_contains_item(heap, elements + i) == 1);
237 }
238
239 //--------------------------------------------------------------------------
240 // sort elements
241 //--------------------------------------------------------------------------
242
243 qsort(sorted_elements, n, sizeof(int), qsort_cmp);
244
245 //--------------------------------------------------------------------------
246 // validate heap poll ordering
247 //--------------------------------------------------------------------------
248
249 int *elem;
250 for(int i = 0; i < n; i++) {
251 elem = (int*)Heap_peek(heap);
252 TEST_ASSERT(*elem == sorted_elements[i]);
253
254 elem = Heap_poll(heap);
255 TEST_ASSERT(*elem == sorted_elements[i]);
256 }
257
258 //--------------------------------------------------------------------------
259 // validate heap is empty
260 //--------------------------------------------------------------------------
261
262 TEST_ASSERT(Heap_count(heap) == 0);
263
264 // free heap
265 Heap_free(heap);
266}

Callers

nothing calls this directly

Calls 7

Heap_newFunction · 0.85
Heap_offerFunction · 0.85
Heap_countFunction · 0.85
Heap_contains_itemFunction · 0.85
Heap_peekFunction · 0.85
Heap_pollFunction · 0.85
Heap_freeFunction · 0.85

Tested by

no test coverage detected