#include "c_NlpStringList.h" #include #include #define RUN_LIST_TEST(condition, test_name) \ do { \ printf("[TEST] %s... ", test_name); \ if (condition) { \ printf("\033[32mPASSED\033[0m\n"); \ } else { \ printf("\033[31mFAILED\033[0m (at Line %d)\n", __LINE__); \ return C_ERR_FAIL; \ } \ } while(0) /** * @brief Complete unit testing suite for the updated c_NlpStringList component. * @return c_err_t Returns C_ERR_OK if all testing blocks pass. */ c_err_t c_NlpStringList_UnitTest(void) { c_NlpStringList_t list; c_err_t err; printf("==================================================\n"); printf(" STARTING C_NLPSTRINGLIST UNIT TESTING \n"); printf("==================================================\n"); /* 1. Defensive Boundary Parameter Safety Tests */ RUN_LIST_TEST(c_NlpStringList_Init(NULL, 10) == C_ERR_PARAM, "Init handles NULL structure pointer"); RUN_LIST_TEST(c_NlpStringList_AddStr(NULL, "data") == C_ERR_PARAM, "Add safely rejects NULL list context"); RUN_LIST_TEST(c_NlpStringList_AddStr(&list, NULL) == C_ERR_PARAM, "Add safely rejects NULL input strings (before init)"); RUN_LIST_TEST(c_NlpStringList_Remove(NULL, 0) == C_ERR_PARAM, "Remove safely rejects NULL list reference"); /* 2. Custom Configured Capacity Initialization Test */ err = c_NlpStringList_Init(&list, 2); // Initialized with small capacity to ease expansion verification RUN_LIST_TEST(err == C_ERR_OK, "Initialization with explicit capacity returns C_ERR_OK"); RUN_LIST_TEST(list.count == 0, "Structural items counter starts at 0"); RUN_LIST_TEST(list.capacity == 2, "Allocated capacity exactly mirrors input parameter"); RUN_LIST_TEST(list.items != NULL, "Internal cluster storage pointer successfully bound"); /* 3. Sequential Elements Add & Read Validation */ err = c_NlpStringList_AddStr(&list, "Apple"); err |= c_NlpStringList_AddStr(&list, "Banana"); RUN_LIST_TEST(err == C_ERR_OK, "Successfully pushed elements up to capacity limit"); RUN_LIST_TEST(list.count == 2, "Count correctly calculated at 2"); RUN_LIST_TEST(list.capacity == 2, "Capacity remains unchanged before hitting boundary"); RUN_LIST_TEST(strcmp(list.items[0], "Apple") == 0, "Slot 0 retains string 'Apple'"); RUN_LIST_TEST(strcmp(list.items[1], "Banana") == 0, "Slot 1 retains string 'Banana'"); /* 4. Automated Array Expansion and Reallocation Safeguard */ err = c_NlpStringList_AddStr(&list, "Cherry"); // Hits boundary, forcing C_ALLOC internal upscale expansion RUN_LIST_TEST(err == C_ERR_OK, "Pushed 3rd string successfully past boundary limits"); RUN_LIST_TEST(list.count == 3, "Count advanced to 3"); RUN_LIST_TEST(list.capacity == 4, "Capacity doubled exponentially from 2 to 4"); RUN_LIST_TEST(strcmp(list.items[2], "Cherry") == 0, "Post-reallocation element validation holds 'Cherry'"); /* 5. Memory Shift Extraction Performance (Remove Verification) */ // Current setup state: ["Apple", "Banana", "Cherry"] // Test wild indices out of bounds protection RUN_LIST_TEST(c_NlpStringList_Remove(&list, 999) == C_ERR_PARAM, "Rejects wild indices beyond context array boundary"); RUN_LIST_TEST(c_NlpStringList_Remove(&list, list.count) == C_ERR_PARAM, "Rejects index exactly hitting edge limit"); // Remove middle slot element ("Banana") err = c_NlpStringList_Remove(&list, 1); RUN_LIST_TEST(err == C_ERR_OK, "Successfully extracted middle entry at Index 1"); RUN_LIST_TEST(list.count == 2, "Total item count updated smoothly down to 2"); // Verify remaining contents moved left continuously without forming holes RUN_LIST_TEST(strcmp(list.items[0], "Apple") == 0, "Slot 0 continues holding 'Apple'"); RUN_LIST_TEST(strcmp(list.items[1], "Cherry") == 0, "Slot 1 successfully consolidated to 'Cherry'"); // Remove remaining front element ("Apple") err = c_NlpStringList_Remove(&list, 0); RUN_LIST_TEST(err == C_ERR_OK, "Extracted front leading element at Index 0"); RUN_LIST_TEST(list.count == 1, "Count downshifted to 1"); RUN_LIST_TEST(strcmp(list.items[0], "Cherry") == 0, "Slot 0 now rolled over to 'Cherry'"); /* 6. Multi-Byte UTF-8 String Asset Preservation */ err = c_NlpStringList_AddStr(&list, "深度学习与大模型"); RUN_LIST_TEST(err == C_ERR_OK, "Added complex multi-byte Chinese token string"); RUN_LIST_TEST(strcmp(list.items[1], "深度学习与大模型") == 0, "Multi-byte raw tracking array verification holds"); /* 7. Garbage Collection & Prevent Secondary Dangling Freeing */ c_NlpStringList_Destroy(&list); RUN_LIST_TEST(list.items == NULL, "Array storage pointer nullified upon destruction sequence"); RUN_LIST_TEST(list.count == 0, "Counters reset cleanly to 0"); RUN_LIST_TEST(list.capacity == 0, "Capacity indicators initialized to 0"); // Idempotent test validation c_NlpStringList_Destroy(&list); c_NlpStringList_Destroy(NULL); printf("[TEST] Double structural destruction safety... \033[32mPASSED\033[0m\n"); printf("==================================================\n"); printf("\033[32mSUCCESS: ALL REVISED LIST SPECIFICATION TESTS PASSED!\033[0m\n"); printf("==================================================\n"); return C_ERR_OK; } int main(void) { if (c_NlpStringList_UnitTest() != C_ERR_OK) { return -1; } return 0; }