< Summary - Backend C Tests - Coverage Report (WSL)

Information
Class: heap_c
Assembly: src.backend.core.data_structures
File(s): ./src/backend/core/data_structures/heap.c
Line coverage
91%
Covered lines: 67
Uncovered lines: 6
Coverable lines: 73
Total lines: 137
Line coverage: 91.7%
Branch coverage
70%
Covered branches: 31
Total branches: 44
Branch coverage: 70.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 2/18/2026 - 10:50:55 PM Line coverage: 91.7% (67/73) Branch coverage: 70.4% (31/44) Total lines: 137 2/18/2026 - 10:50:55 PM Line coverage: 91.7% (67/73) Branch coverage: 70.4% (31/44) Total lines: 137

File(s)

./src/backend/core/data_structures/heap.c

#LineLine coverage
 1/* This file is a template for heap.c. Content will be filled by yagiz on 2025-12-29. */
 2#include <stdio.h>
 3#include "heap.h"
 4
 5// Helper: Bubble Up (Moves a node UP to its correct position)
 1096static void bubbleUp(Heap *h, int index) {
 1147  while (index > 0) {
 1058    int parentIndex = (index - 1) / 2;
 9    // Max-Heap Logic: Parent should be greater.
 10    // Tie-Breaker: If Priorities are EQUAL, Older Timestamp (smaller value) wins.
 10511    bool parentIsSmaller = h->data[parentIndex].priority < h->data[index].priority;
 20512    bool equalButNewer = (h->data[parentIndex].priority == h->data[index].priority) &&
 10013                         (h->data[parentIndex].timestamp > h->data[index].timestamp);
 14
 10515    if (parentIsSmaller || equalButNewer) {
 16      // SWAP
 517      Task temp = h->data[parentIndex];
 518      h->data[parentIndex] = h->data[index];
 519      h->data[index] = temp;
 20      // Move up
 521      index = parentIndex;
 22    } else {
 23      break; // Order is correct
 24    }
 25  }
 10926}
 27
 28// Helper: Heapify / Bubble Down (Moves a node DOWN to its correct position)
 1029static void heapify(Heap *h, int index) {
 1030  int largest = index;
 1031  int left = 2 * index + 1;
 1032  int right = 2 * index + 2;
 33
 34  // Check Left Child
 1035  if (left < h->count) {
 336    bool leftIsBigger = h->data[left].priority > h->data[largest].priority;
 337    bool leftEqualButOlder = (h->data[left].priority == h->data[largest].priority) &&
 038                             (h->data[left].timestamp < h->data[largest].timestamp); // Smaller timestamp = Older
 39
 340    if (leftIsBigger || leftEqualButOlder) {
 241      largest = left;
 42    }
 43  }
 44
 45  // Check Right Child
 1046  if (right < h->count) {
 047    bool rightIsBigger = h->data[right].priority > h->data[largest].priority;
 048    bool rightEqualButOlder = (h->data[right].priority == h->data[largest].priority) &&
 049                              (h->data[right].timestamp < h->data[largest].timestamp);
 50
 051    if (rightIsBigger || rightEqualButOlder) {
 052      largest = right;
 53    }
 54  }
 55
 56  // Swap and Recursive Call
 1057  if (largest != index) {
 258    Task temp = h->data[index];
 259    h->data[index] = h->data[largest];
 260    h->data[largest] = temp;
 261    heapify(h, largest);
 62  }
 1063}
 64
 565void initHeap(Heap *h) {
 566  h->count = 0;
 567}
 68
 69// INSERT
 10970bool insertHeap(Heap *h, Task task) {
 10971  if (h->count >= MAX_HEAP_SIZE) {
 172    printf("ERROR: Heap is full! Cannot add Task-%d\n", task.id);
 173    return false;
 74  }
 75
 76  // 1. Add to end
 10877  h->data[h->count] = task;
 78  // 2. Bubble Up
 10879  bubbleUp(h, h->count);
 80  // 3. Increment Count
 10881  h->count++;
 10882  printf("Task Added: ID-%d [P:%d]\n", task.id, task.priority);
 10883  return true;
 84}
 85
 86// EXTRACT MAX
 887bool extractMax(Heap *h, Task *outTask) {
 888  if (h->count == 0) {
 189    printf("ERROR: Heap is empty!\n");
 190    return false;
 91  }
 92
 93  // 1. Get Root (Max Priority)
 794  *outTask = h->data[0];
 95  // 2. Move last element to root
 796  h->data[0] = h->data[h->count - 1];
 797  h->count--;
 98  // 3. Heaterify (Bubble Down)
 799  heapify(h, 0);
 100  // printf("Task Extracted: ID-%d [P:%d]\n", outTask->id, outTask->priority);
 7101  return true;
 102}
 103
 104// UPDATE PRIORITY (Dynamic Update)
 3105bool updatePriority(Heap *h, int task_id, int new_priority) {
 3106  int index = -1;
 107
 108  // 1. Find the task
 5109  for(int i = 0; i < h->count; i++) {
 4110    if(h->data[i].id == task_id) {
 2111      index = i;
 2112      break;
 113    }
 114  }
 115
 3116  if(index == -1) {
 1117    printf("ERROR: Task-%d not found!\n", task_id);
 1118    return false;
 119  }
 120
 2121  int old_priority = h->data[index].priority;
 2122  h->data[index].priority = new_priority;
 2123  printf("Task-%d Priority Updated: %d -> %d\n", task_id, old_priority, new_priority);
 124
 125  // 2. Re-balance
 2126  if (new_priority > old_priority) {
 1127    bubbleUp(h, index);
 128  } else {
 1129    heapify(h, index);
 130  }
 131
 2132  return true;
 133}
 134
 2135int getHeapSize(Heap *h) {
 2136  return h->count;
 137}

Methods/Properties