Files
cAI/cKit/Foundation/c_StringBuffer.t.c
T

134 lines
7.0 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_StringBuffer.h"
#include <stdlib.h>
#include <stdio.h>
#define RUN_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)
c_err_t c_StringBuffer_UnitTest(void) {
c_StringBuffer_t sb;
c_err_t err;
char copy_target[64];
printf("==================================================\n");
printf(" STARTING C_STRINGBUFFER UNIT TESTING \n");
printf("==================================================\n");
/* 1. API Parameter Defensive Checks (Null Guards) */
RUN_TEST(c_StringBuffer_Init(NULL, 16) == C_ERR_PARAM, "Init handles NULL self context");
RUN_TEST(c_StringBuffer_Append(NULL, "a", 1) == C_ERR_PARAM, "Append checks NULL self");
RUN_TEST(c_StringBuffer_Prepend(NULL, "a", 1) == C_ERR_PARAM, "Prepend checks NULL self");
RUN_TEST(c_StringBuffer_InsertAt(NULL, 0, "a", 1) == C_ERR_PARAM, "InsertAt checks NULL self");
RUN_TEST(c_StringBuffer_RemoveAt(NULL, 0, 1) == C_ERR_PARAM, "RemoveAt checks NULL self");
RUN_TEST(c_StringBuffer_CopyTo(NULL, 0, 1, copy_target, 64) == C_ERR_PARAM, "CopyTo checks NULL self");
/* 2. Initialization Test Block (Init) */
err = c_StringBuffer_Init(&sb, 4); // Initialize with small capacity to force upcoming resizing branches
RUN_TEST(err == C_ERR_OK, "Initialization with tiny explicit capacity returns C_ERR_OK");
RUN_TEST(sb.size == 0, "Initial tracked contents data size is 0");
RUN_TEST(sb.capacity == 4, "Initial tracking allocation capacity is 4");
RUN_TEST(sb.buffer != NULL, "Internal tracking byte storage buffer successfully bound");
RUN_TEST(sb.buffer[0] == '\0', "Empty buffer is safely terminated with null byte");
/* 3. Length-bounded Insertion Operations (Append, Prepend, InsertAt) */
// Append test
err = c_StringBuffer_Append(&sb, "Trie", 4);
RUN_TEST(err == C_ERR_OK, "Append bounded segment 'Trie'");
RUN_TEST(sb.size == 4, "Size matches append width");
RUN_TEST(strcmp(sb.buffer, "Trie") == 0, "Buffer contains exact match string 'Trie'");
// Prepend test
err = c_StringBuffer_Prepend(&sb, "Nlp", 3);
RUN_TEST(err == C_ERR_OK, "Prepend bounded segment 'Nlp' to front");
RUN_TEST(sb.size == 7, "Size extended to 7 bytes total");
RUN_TEST(strcmp(sb.buffer, "NlpTrie") == 0, "Buffer shifted correctly into 'NlpTrie'");
// InsertAt test (Middle shifting memory operation)
err = c_StringBuffer_InsertAt(&sb, 3, "_", 1);
RUN_TEST(err == C_ERR_OK, "InsertAt index 3 inserts an underscore character");
RUN_TEST(strcmp(sb.buffer, "Nlp_Trie") == 0, "Memory shifted left/right flawlessly: 'Nlp_Trie'");
/* 4. Exponential Expansion Threshold Guard Check */
// Pushing string past current internal storage boundaries to trigger C_ALLOC resizing
err = c_StringBuffer_Append(&sb, "_DataStructure", 14);
RUN_TEST(err == C_ERR_OK, "Forced exponential buffer reallocation with large string append");
RUN_TEST(sb.size == 22, "Size correctly aggregated up to 22 bytes total");
RUN_TEST(sb.capacity >= 23, "Capacity upscaled cleanly beyond its initial 4-byte threshold limit");
RUN_TEST(strcmp(sb.buffer, "Nlp_Trie_DataStructure") == 0, "Post-reallocation string remains integrated and uncorrupted");
/* 5. Memory Shift Extraction Operations (RemoveAt) */
// Current payload structure: "Nlp_Trie_DataStructure"
// Remove mid segment "_DataStructure" starting at index 8
err = c_StringBuffer_RemoveAt(&sb, 8, 14);
RUN_TEST(err == C_ERR_OK, "RemoveAt clears middle segment '_DataStructure'");
RUN_TEST(sb.size == 8, "Size downshifted cleanly to 8 bytes");
RUN_TEST(strcmp(sb.buffer, "Nlp_Trie") == 0, "Character array closed gaps safely to hold 'Nlp_Trie'");
// Test automatic length clamping over boundary limit edge cases
err = c_StringBuffer_RemoveAt(&sb, 3, 50); // 50 overshoot actual size limits
RUN_TEST(err == C_ERR_OK, "RemoveAt automatically clamps requesting lengths overflowing edge limits");
RUN_TEST(sb.size == 3, "Length updated down to index bounds");
RUN_TEST(strcmp(sb.buffer, "Nlp") == 0, "Buffer contains truncated string 'Nlp'");
/* 6. Explicit String Wrapper Interfaces (AppendStr, PrependStr, InsertStrAt) */
c_StringBuffer_Clear(&sb);
RUN_TEST(sb.size == 0 && sb.buffer[0] == '\0', "Clear flushes buffer structure size indicators cleanly");
err = c_StringBuffer_AppendStr(&sb, "Core");
err |= c_StringBuffer_PrependStr(&sb, "C_");
err |= c_StringBuffer_InsertStrAt(&sb, "Nlp", 2); // Insert "Nlp" into index 2 ("C_Core" -> "C_NlpCore")
RUN_TEST(err == C_ERR_OK, "All explicit string wrapper functions evaluated with valid results");
RUN_TEST(strcmp(sb.buffer, "C_NlpCore") == 0, "String wrapper cascade holds correct output value 'C_NlpCore'");
/* 7. Substring Extraction Pipeline Test (CopyTo) */
// Test slice matching operations
err = c_StringBuffer_CopyTo(&sb, 2, 3, copy_target, sizeof(copy_target));
RUN_TEST(err == C_ERR_OK, "CopyTo safely slices subset out to isolated external layout target");
RUN_TEST(strcmp(copy_target, "Nlp") == 0, "External buffer extracted substring captures token 'Nlp' correctly");
// Test out of bounds inputs rejection properties
RUN_TEST(c_StringBuffer_CopyTo(&sb, 2, 3, copy_target, 2) == C_ERR_PARAM, "CopyTo blocks actions when external buffer is too small");
RUN_TEST(c_StringBuffer_CopyTo(&sb, 999, 1, copy_target, sizeof(copy_target)) == C_ERR_PARAM, "CopyTo blocks crazy out of range index arguments");
/* 8. Multi-Byte UTF-8 String Asset Integrity Checks */
c_StringBuffer_Clear(&sb);
err = c_StringBuffer_AppendStr(&sb, "语言");
err |= c_StringBuffer_PrependStr(&sb, "自然");
err |= c_StringBuffer_AppendStr(&sb, "处理"); // "自然语言处理"
RUN_TEST(err == C_ERR_OK, "Piped raw multi-byte Chinese UTF-8 string tokens through buffer channels");
RUN_TEST(strcmp(sb.buffer, "自然语言处理") == 0, "Raw multi-byte array matches validation configuration stream");
/* 9. Destruction Lifecycle Cleanliness Verification */
c_StringBuffer_Destroy(&sb);
RUN_TEST(sb.buffer == NULL, "Array tracking pointer nullified successfully upon calling destructor");
RUN_TEST(sb.size == 0 && sb.capacity == 0, "Structural trackers set to 0");
// Idempotency execution test sequence
c_StringBuffer_Destroy(&sb);
c_StringBuffer_Destroy(NULL);
printf("[TEST] Double string buffer destruction safety... \033[32mPASSED\033[0m\n");
printf("==================================================\n");
printf("\033[32mSUCCESS: ALL C_STRINGBUFFER TESTS COMPLETED SUCCESSFULLY!\033[0m\n");
printf("==================================================\n");
return C_ERR_OK;
}
int main(int argc, char** argv){
if (c_StringBuffer_UnitTest() != C_ERR_OK) {
return -1;
}
return 0;
}