99 lines
3.7 KiB
C
99 lines
3.7 KiB
C
#include "c_MinHeap.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
typedef struct {
|
|
int priority; // 優先權數值(數值越小,代表權重越高、越優先)
|
|
char task_name[32];
|
|
} HeapTask_t;
|
|
|
|
// 比較回呼函數
|
|
int compare_tasks(const void* a, const void* b) {
|
|
return (((HeapTask_t*)a)->priority - ((HeapTask_t*)b)->priority);
|
|
}
|
|
|
|
void test_log(const char* name) {
|
|
printf("[PASS] %s\n", name);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
printf("==================================================\n");
|
|
printf(" 開始執行 c_MinHeap_t 泛型最小堆單元測試\n");
|
|
printf("==================================================\n\n");
|
|
|
|
c_MinHeap_t heap;
|
|
// 初始容量設為 2 驗證後續動態 realloc 擴容
|
|
c_err_t err = c_MinHeap_Init(&heap, sizeof(HeapTask_t), 2, compare_tasks);
|
|
assert(err == C_ERR_SUCCESS);
|
|
assert(c_MinHeap_IsEmpty(&heap) == C_TRUE);
|
|
test_log("1. 最小堆控制結構體與大小配置初始化成功");
|
|
|
|
// 故意以不規則的順序建立任務
|
|
HeapTask_t t1 = {50, "Task_Low"};
|
|
HeapTask_t t2 = {10, "Task_Urgent"};
|
|
HeapTask_t t3 = {30, "Task_Medium"};
|
|
HeapTask_t t4 = {5, "Task_Critical"}; // 最優先
|
|
|
|
// ==========================================
|
|
// 2. 測試推入與動態擴容 (Push)
|
|
// ==========================================
|
|
c_MinHeap_Push(&heap, &t1);
|
|
c_MinHeap_Push(&heap, &t2);
|
|
// 推入第 3 個元素,此時 size=3 > capacity=2,預期內部會自動動態擴容翻倍為 4
|
|
c_MinHeap_Push(&heap, &t3);
|
|
err = c_MinHeap_Push(&heap, &t4);
|
|
|
|
assert(err == C_ERR_SUCCESS);
|
|
assert(c_MinHeap_GetSize(&heap) == 4);
|
|
assert(heap.capacity == 4); // 驗證動態擴容成功
|
|
test_log("2. 隨機元素推入與陣列自動動態擴容驗證成功");
|
|
|
|
// ==========================================
|
|
// 3. 測試查看堆頂端 (Peek)
|
|
// ==========================================
|
|
// 目前權重最小(最優先)的是 priority=5 的 Task_Critical
|
|
HeapTask_t* p_peek = (HeapTask_t*)c_MinHeap_Peek(&heap);
|
|
assert(p_peek != NULL);
|
|
assert(p_peek->priority == 5);
|
|
assert(strcmp(p_peek->task_name, "Task_Critical") == 0);
|
|
test_log("3. 堆頂端唯讀查看 (Peek) 最優先元素成功");
|
|
|
|
// ==========================================
|
|
// 4. 連續彈出優先級檢驗 (Pop - 使用者自備緩衝區)
|
|
// ==========================================
|
|
HeapTask_t local_buf;
|
|
|
|
// 第一次彈出:預期取得 5
|
|
err = c_MinHeap_Pop(&heap, &local_buf);
|
|
assert(err == C_ERR_SUCCESS);
|
|
assert(local_buf.priority == 5);
|
|
assert(strcmp(local_buf.task_name, "Task_Critical") == 0);
|
|
|
|
// 第二次彈出:預期取得 10
|
|
c_MinHeap_Pop(&heap, &local_buf);
|
|
assert(local_buf.priority == 10);
|
|
assert(strcmp(local_buf.task_name, "Task_Urgent") == 0);
|
|
|
|
// 第三次彈出:預期取得 30
|
|
c_MinHeap_Pop(&heap, &local_buf);
|
|
assert(local_buf.priority == 30);
|
|
|
|
// 第四次彈出:預期取得 50
|
|
c_MinHeap_Pop(&heap, &local_buf);
|
|
assert(local_buf.priority == 50);
|
|
assert(c_MinHeap_IsEmpty(&heap) == C_TRUE);
|
|
|
|
// 第五次彈出:此時堆已完全被清空,預期回傳越界錯誤
|
|
err = c_MinHeap_Pop(&heap, &local_buf);
|
|
assert(err == C_ERR_EMPTY);
|
|
assert(c_MinHeap_Peek(&heap) == NULL);
|
|
test_log("5. 連續 Pop 遞增順序驗證與空堆防呆成功");
|
|
|
|
c_MinHeap_Destroy(&heap);
|
|
test_log("6. 最小堆資源回收成功");
|
|
|
|
printf("\n==================================================\n");
|
|
printf(" 恭喜!c_MinHeap_t 平鋪陣列結構所有操作單元測試完勝!\n");
|
|
printf("==================================================\n");
|
|
return 0;
|
|
} |