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

Function test_heapFuzz

tests/unit/test_heap.c:339–409  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

337}
338
339static void test_heapFuzz(void) {
340 // seed random
341 srand(time(NULL));
342
343 // create a new heap
344 heap_t *heap = Heap_new(cmp, NULL);
345
346 int n = 500;
347 int *elements = array_new(int, n); // all elements ever introduced
348 int *synced_elements = array_new(int, n); // elements synced with heap
349
350 // perform random operations:
351 // 1. introduce a new random element
352 // 2. remove a random element
353 // 3. remove head
354 //
355 // after each operation perform validation against head of heap
356 for(int i = 0; i < 500; i++) {
357 int idx; // index of element to remove
358 int *top; // head of heap
359 int elem; // element to add
360 int op = rand() % 3; // operation to perform
361
362 switch(op) {
363 case 0:
364 // introduce a new random element
365 elem = rand() % 100;
366 array_append(elements, elem);
367 array_append(synced_elements, elem);
368 Heap_offer(&heap, elements + (array_len(elements)-1));
369 break;
370 case 1:
371 // remove random element
372 if(Heap_count(heap) > 0) {
373 // remove a random element
374 idx = rand() % array_len(elements);
375
376 if(Heap_remove_item(heap, elements + idx) != NULL) {
377 remove_value(synced_elements, elements[idx]);
378 }
379 }
380 break;
381 case 2:
382 // pop heap head
383 top = (int*)Heap_poll(heap);
384 if(top != NULL) {
385 remove_value(synced_elements, *top);
386 }
387 break;
388 default:
389 break;
390 }
391
392 //----------------------------------------------------------------------
393 // validate head of heap contains the lowest value in elements array
394 //----------------------------------------------------------------------
395 int heap_count = Heap_count(heap);
396 int arr_count = array_len(synced_elements);

Callers

nothing calls this directly

Calls 11

Heap_newFunction · 0.85
Heap_offerFunction · 0.85
array_lenFunction · 0.85
Heap_countFunction · 0.85
Heap_remove_itemFunction · 0.85
remove_valueFunction · 0.85
Heap_pollFunction · 0.85
Heap_peekFunction · 0.85
find_minFunction · 0.85
Heap_freeFunction · 0.85
array_freeFunction · 0.85

Tested by

no test coverage detected