Foundations
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include "c_LinkList.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
typedef struct {
|
||||
char name[4];
|
||||
int score;
|
||||
} Student_t;
|
||||
|
||||
int main() {
|
||||
printf("開始執行 c_LinkList 測試用例...\n");
|
||||
|
||||
c_LinkList_t list;
|
||||
c_LinkList_Init(&list, sizeof(Student_t));
|
||||
|
||||
Student_t s1 = {"AAA", 80};
|
||||
Student_t s2 = {"BBB", 90};
|
||||
Student_t s3 = {"CCC", 95};
|
||||
|
||||
// 1. 測試新增 (頭插法預期順序: CCC -> BBB -> AAA)
|
||||
c_LinkList_Add(&list, &s1);
|
||||
c_LinkList_Add(&list, &s2);
|
||||
c_LinkList_Add(&list, &s3);
|
||||
assert(list.size == 3);
|
||||
|
||||
// 2. 測試迭代器走訪
|
||||
c_LinkListIter_t iter;
|
||||
c_LinkListIter_Init(&iter, &list);
|
||||
|
||||
printf("當前串列內容:\n");
|
||||
while (c_LinkListIter_HasNext(&iter)) {
|
||||
Student_t* s = (Student_t*)c_LinkListIter_Next(&iter);
|
||||
printf(" 學生: %s, 分數: %d\n", s->name, s->score);
|
||||
}
|
||||
|
||||
// 3. 測試一般刪除 (Remove - 透過二進位資料比對刪除 "BBB")
|
||||
Student_t target_remove = {"BBB", 90};
|
||||
c_err_t err = c_LinkList_Remove(&list, &target_remove);
|
||||
assert(err == C_ERR_OK);
|
||||
assert(list.size == 2);
|
||||
|
||||
// 4. 測試迭代器走訪並刪除 (Iter_Remove)
|
||||
c_LinkListIter_Init(&iter, &list);
|
||||
while (c_LinkListIter_HasNext(&iter)) {
|
||||
Student_t* s = (Student_t*)c_LinkListIter_Get(&iter);
|
||||
if (s->score == 80) { // 找到分數 80 的學生 (AAA)
|
||||
c_LinkListIter_Remove(&iter);
|
||||
printf("[Log] 迭代器成功刪除了分數為 80 的學生\n");
|
||||
} else {
|
||||
c_LinkListIter_Next(&iter);
|
||||
}
|
||||
}
|
||||
assert(list.size == 1);
|
||||
|
||||
// 5. 驗證最後留下來的是否為 CCC
|
||||
c_LinkListIter_Init(&iter, &list);
|
||||
Student_t* final_s = (Student_t*)c_LinkListIter_Get(&iter);
|
||||
assert(strcmp(final_s->name, "CCC") == 0);
|
||||
|
||||
// 銷毀資源
|
||||
c_LinkList_Destroy(&list);
|
||||
assert(list.head == NULL);
|
||||
assert(list.size == 0);
|
||||
|
||||
printf("所有測試成功通過!\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user