开始设计

This commit is contained in:
2026-08-10 01:21:15 +08:00
commit e45398991f
228 changed files with 20827 additions and 0 deletions
+114
View File
@@ -0,0 +1,114 @@
#include "c_StrUtil.h"
#include <stdlib.h>
#include <stdio.h>
void test_log(const char* test_name) {
printf("[PASS] %s\n", test_name);
}
static void test_split(void) {
printf("==================================================\n");
printf(" Starting Tokenizer & ArrayList Integration Tests\n");
printf("==================================================\n\n");
// Initialize your generic ArrayList to accept your structured token elements
c_ArrayList_t token_list;
c_err_t err = c_ArrayList_Init(&token_list, sizeof(c_StrToken_t), 4);
assert(err == C_ERR_SUCCESS);
const char* csv_data = "GND,VCC,TX_PIN,RX_PIN,SPI_CLK";
// Split the comma-delimited configuration values
err = c_StrUtil_Split(&token_list, csv_data, ",");
assert(err == C_ERR_SUCCESS);
// 1. Validate parsed sizing counts matching expected segmentations
assert(token_list.size == 5);
test_log("1. String segmented into 5 individual structural elements");
// 2. Sequential range checking verifying index preservation
c_StrToken_t* t_ptr;
t_ptr = (c_StrToken_t*)c_ArrayList_Get(&token_list, 0);
assert(strcmp(t_ptr->text, "GND") == 0);
t_ptr = (c_StrToken_t*)c_ArrayList_Get(&token_list, 2);
assert(strcmp(t_ptr->text, "TX_PIN") == 0);
t_ptr = (c_StrToken_t*)c_ArrayList_Get(&token_list, 4);
assert(strcmp(t_ptr->text, "SPI_CLK") == 0);
test_log("2. Target indices verify correct sub-string capture sequence");
// 3. Clear container lifecycle properties cleanly
c_ArrayList_Destroy(&token_list);
test_log("3. Array container memory teardown success");
printf("\n==================================================\n");
printf(" Success! Token parsing and container layers fit perfectly!\n");
printf("==================================================\n");
}
int main() {
printf("==================================================\n");
printf(" Starting C String Utility Unit Tests\n");
printf("==================================================\n\n");
char buffer[128];
c_err_t err;
// 1. Test Reverse
strcpy(buffer, "A man a plan a canal Panama");
c_StrUtil_Reverse(buffer);
assert(strcmp(buffer, "amanaP lanac a nalp a nam A") == 0);
test_log("1. In-place string reversal");
// 2. Test Case Conversions
err = c_StrUtil_ToLower(buffer, "C_Language_123!", sizeof(buffer));
assert(err == C_ERR_SUCCESS);
assert(strcmp(buffer, "c_language_123!") == 0);
err = c_StrUtil_ToUpper(buffer, "embedded c", sizeof(buffer));
assert(err == C_ERR_SUCCESS);
assert(strcmp(buffer, "EMBEDDED C") == 0);
test_log("2. Casing conversions");
// 3. Test Trim
err = c_StrUtil_Trim(buffer, " \t Hello World \r\n ", sizeof(buffer));
assert(err == C_ERR_SUCCESS);
assert(strcmp(buffer, "Hello World") == 0);
test_log("3. Whitespace trimming");
// 4. Test Starts/Ends With
assert(c_StrUtil_StartsWith("libcontainer.so", "lib") == C_TRUE);
assert(c_StrUtil_StartsWith("libcontainer.so", "bin") == C_FALSE);
assert(c_StrUtil_EndsWith("document.txt", ".txt") == C_TRUE);
assert(c_StrUtil_EndsWith("document.txt", ".pdf") == C_FALSE);
test_log("4. Prefix/Suffix conditional checks");
// 5. Test Substring
err = c_StrUtil_Substring(buffer, "Microcontroller", 5, 7, sizeof(buffer));
assert(err == C_ERR_SUCCESS);
assert(strcmp(buffer, "control") == 0);
test_log("5. Substring range isolation");
// 6. Test Replace
err = c_StrUtil_Replace(buffer, "The quick brown fox jumps over the lazy dog", "quick brown fox", "slow green turtle", sizeof(buffer));
assert(err == C_ERR_SUCCESS);
assert(strcmp(buffer, "The slow green turtle jumps over the lazy dog") == 0);
test_log("6. Substring template injection matching");
// 7. Overflow Protection
char small_buffer[6];
err = c_StrUtil_ToUpper(small_buffer, "OVERFLOW_TEST", sizeof(small_buffer));
assert(err == C_ERR_INDEX);
assert(strlen(small_buffer) == 5); // Ensure safe null truncation occurred
test_log("7. Buffer overflow bounds interception");
printf("\n==================================================\n");
printf(" Success! String utilities match defensive safety layouts!\n");
printf("==================================================\n");
test_split();
return 0;
}