Files
cAI/cKit/Foundation/c_LinkDQueue.t.c
T
2026-08-10 01:21:15 +08:00

110 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "c_LinkDQueue.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char label[16];
int val;
} Element_t;
void test_log(const char* test_name) {
printf("[PASS] %s\n", test_name);
}
int main() {
printf("==================================================\n");
printf(" 開始執行 c_LinkDQueue 雙端佇列完整測試用例\n");
printf("==================================================\n\n");
c_LinkDQueue_t dq;
c_LinkDQueue_Init(&dq, sizeof(Element_t));
Element_t e1 = {"Node_1", 10};
Element_t e2 = {"Node_2", 20};
Element_t e3 = {"Node_3", 30};
// ==========================================
// 1. 測試雙端推入 (PushHead & PushTail)
// ==========================================
// 先把 e2 推入尾端 (佇列: [e2])
c_LinkDQueue_PushTail(&dq, &e2);
// 把 e1 推入前端 (佇列: [e1, e2])
c_LinkDQueue_PushHead(&dq, &e1);
// 把 e3 推入尾端 (佇列: [e1, e2, e3])
c_LinkDQueue_PushTail(&dq, &e3);
assert(dq.size == 3);
assert(((Element_t*)c_LinkDQueue_PeekHead(&dq))->val == 10);
assert(((Element_t*)c_LinkDQueue_PeekTail(&dq))->val == 30);
test_log("1. 雙端交錯推入與邊界 Peek 驗證成功");
// ==========================================
// 2. 測試迭代器走訪
// ==========================================
c_LinkDQueueIter_t iter;
c_LinkDQueueIter_Init(&iter, &dq);
int idx = 0;
while (c_LinkDQueueIter_HasNext(&iter)) {
Element_t* el = (Element_t*)c_LinkDQueueIter_Next(&iter);
if (idx == 0) assert(el->val == 10);
if (idx == 1) assert(el->val == 20);
if (idx == 2) assert(el->val == 30);
idx++;
}
test_log("2. 迭代器正向走訪順序驗證成功");
// ==========================================
// 3. 測試迭代器刪除中間節點 (Remove e2)
// ==========================================
c_LinkDQueueIter_Init(&iter, &dq);
while (c_LinkDQueueIter_HasNext(&iter)) {
Element_t* el = (Element_t*)c_LinkDQueueIter_Get(&iter);
if (el->val == 20) {
c_LinkDQueueIter_Remove(&iter); // 刪除 Node_2
} else {
c_LinkDQueueIter_Next(&iter);
}
}
assert(dq.size == 2);
// 驗證現在內容只剩下 [e1, e3]
assert(((Element_t*)c_LinkDQueue_PeekHead(&dq))->val == 10);
assert(((Element_t*)c_LinkDQueue_PeekTail(&dq))->val == 30);
test_log("3. 迭代器 O(1) 刪除中間節點暨雙向鏈結維護成功");
// ==========================================
// 4. 測試雙端彈出 (PopHead & PopTail)
// ==========================================
Element_t buf;
// 從前端彈出(應拿到 e1
c_err_t err = c_LinkDQueue_PopHead(&dq, &buf);
assert(err == C_ERR_SUCCESS);
assert(buf.val == 10);
assert(dq.size == 1);
// 從尾端彈出(應拿到 e3
err = c_LinkDQueue_PopTail(&dq, &buf);
assert(err == C_ERR_SUCCESS);
assert(buf.val == 30);
assert(dq.size == 0);
// 驗證完全清空後指標皆重置為 NULL
assert(dq.head == NULL);
assert(dq.tail == NULL);
test_log("4. 雙端彈出與空佇列指標歸零驗證成功");
// ==========================================
// 5. 空佇列防呆測試
// ==========================================
assert(c_LinkDQueue_PopHead(&dq, &buf) == C_ERR_EMPTY);
assert(c_LinkDQueue_PopTail(&dq, &buf) == C_ERR_EMPTY);
assert(c_LinkDQueue_PeekHead(&dq) == NULL);
test_log("5. 空雙端佇列越界防呆成功");
c_LinkDQueue_Destroy(&dq);
printf("\n==================================================\n");
printf(" 恭喜!c_LinkDQueue 所有核心特性單元測試完美通過!\n");
printf("==================================================\n");
return 0;
}