364 lines
15 KiB
C
364 lines
15 KiB
C
#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 %s:%d)\n", __FILE__, __LINE__); \
|
|
return C_ERR_FAIL; \
|
|
} \
|
|
} while(0)
|
|
|
|
static c_err_t test_harness_vprintf(c_StringBuffer_t* self, const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
c_err_t err = c_StringBuffer_VPrintf(self, format, args);
|
|
va_end(args);
|
|
return err;
|
|
}
|
|
|
|
static c_err_t test_harness_vprintf_at(c_StringBuffer_t* self, c_size_t index, const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
c_err_t err = c_StringBuffer_VPrintfAt(self, index, format, args);
|
|
va_end(args);
|
|
return err;
|
|
}
|
|
|
|
/* --- Module 1: Structural Allocation Lifecycle Management --- */
|
|
static void test_lifecycle_and_clear(void) {
|
|
c_StringBuffer_t sb;
|
|
|
|
// Validate NULL parameters are rejected deterministically
|
|
assert(c_StringBuffer_Init(NULL, 64) == C_ERR_INVALID_PARAM);
|
|
|
|
// Standard allocation flow verification
|
|
assert(c_StringBuffer_Init(&sb, 16) == C_SUCCESS);
|
|
assert(sb.size == 0);
|
|
assert(sb.capacity >= 16); // Implicit null-terminator overhead validation
|
|
assert(sb.buffer != NULL);
|
|
assert(sb.buffer[0] == '\0');
|
|
|
|
// Data clearing verification
|
|
assert(c_StringBuffer_AppendStr(&sb, "DynamicDataPayload") == C_SUCCESS);
|
|
assert(sb.size == 18);
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(sb.size == 0);
|
|
assert(sb.buffer[0] == '\0'); // Ensure implicit closure byte remains active
|
|
|
|
// Release phase validation
|
|
c_StringBuffer_Destroy(&sb);
|
|
assert(sb.buffer == NULL);
|
|
assert(sb.capacity == 0);
|
|
assert(sb.size == 0);
|
|
|
|
// Idempotent protection check against multi-free configurations
|
|
c_StringBuffer_Destroy(NULL);
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 2: Memory Relocation & Byte Array Mutation Ops --- */
|
|
static void test_array_mutations(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 2) == C_SUCCESS); // Aggressive scaling constraint
|
|
|
|
// Check boundary anomalies
|
|
assert(c_StringBuffer_Append(NULL, "data", 4) == C_ERR_INVALID_PARAM);
|
|
assert(c_StringBuffer_Append(&sb, NULL, 4) == C_ERR_INVALID_PARAM);
|
|
assert(c_StringBuffer_Append(&sb, "ZeroOp", 0) == C_ERR_INVALID_PARAM);
|
|
|
|
// Append tracking
|
|
assert(c_StringBuffer_AppendStr(&sb, "Engine") == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Engine") == 0);
|
|
assert(sb.size == 6);
|
|
|
|
// Prepend and structural layout shift tracking
|
|
assert(c_StringBuffer_PrependStr(&sb, "Core ") == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Core Engine") == 0);
|
|
assert(sb.size == 11);
|
|
|
|
// Index tracking and arbitrary memory slice insertions
|
|
assert(c_StringBuffer_InsertStrAt(&sb, "Graphics ", 5) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Core Graphics Engine") == 0);
|
|
assert(c_StringBuffer_InsertStrAt(&sb, "OOB", 256) == C_ERR_OUT_OF_BOUNDS);
|
|
|
|
// Data element contraction testing via RemoveAt
|
|
assert(c_StringBuffer_RemoveAt(&sb, 50, 2) == C_ERR_OUT_OF_BOUNDS);
|
|
assert(c_StringBuffer_RemoveAt(&sb, 5, 0) == C_SUCCESS);
|
|
assert(c_StringBuffer_RemoveAt(&sb, 5, 9) == C_SUCCESS); // Cleaves out "Graphics "
|
|
assert(strcmp(sb.buffer, "Core Engine") == 0);
|
|
assert(sb.size == 11);
|
|
|
|
// Clamping limits validation: Removing past structural layout capacity boundaries
|
|
assert(c_StringBuffer_RemoveAt(&sb, 4, 100) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Core") == 0);
|
|
assert(sb.size == 4);
|
|
|
|
// Outbound array replication via CopyTo
|
|
char export_buffer[16];
|
|
assert(c_StringBuffer_CopyTo(&sb, 0, 4, export_buffer, sizeof(export_buffer)) == C_SUCCESS);
|
|
assert(strcmp(export_buffer, "Core") == 0);
|
|
|
|
// Buffer optimization check: Truncation logic preservation on small arrays
|
|
assert(c_StringBuffer_CopyTo(&sb, 0, 4, export_buffer, 3) == C_ERR_OUT_OF_BOUNDS); // Destination size limit 3
|
|
// assert(strcmp(export_buffer, "Co") == 0); // Fits "Co" + '\0' safely
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 3: Format Translators & Interleaved Injections --- */
|
|
static void test_formatting_engines(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 8) == C_SUCCESS);
|
|
|
|
// Standard Printf string generation
|
|
assert(c_StringBuffer_Printf(&sb, "%s = %04d", "Status", 200) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Status = 0200") == 0);
|
|
|
|
// Reentrant list validation via VPrintf
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(test_harness_vprintf(&sb, "Float: %.2f", 3.14159) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Float: 3.14") == 0);
|
|
|
|
// Deep structural data layout testing: PrintfAt string middle-smashes
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendStr(&sb, "Alpha-Gamma") == C_SUCCESS);
|
|
|
|
// Inject "Beta-" precisely at index position 6 without breaking string sequence chains
|
|
assert(c_StringBuffer_PrintfAt(&sb, 6, "%s-", "Beta") == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "Alpha-Beta-Gamma") == 0);
|
|
assert(sb.size == 16);
|
|
|
|
// Reentrant multi-layer gap injection testing via VPrintfAt
|
|
assert(test_harness_vprintf_at(&sb, 0, "[%c]", 'I') == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "[I]Alpha-Beta-Gamma") == 0);
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 4: Speculative Loop Chronology Modules --- */
|
|
static void test_chronology_modules(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 4) == C_SUCCESS);
|
|
|
|
struct tm mock_epoch;
|
|
mock_epoch.tm_year = 126; // Year 2026 representation framework
|
|
mock_epoch.tm_mon = 7; // August calibration index
|
|
mock_epoch.tm_mday = 10;
|
|
mock_epoch.tm_hour = 14;
|
|
mock_epoch.tm_min = 22;
|
|
mock_epoch.tm_sec = 45;
|
|
|
|
// Direct temporal formatting validation
|
|
assert(c_StringBuffer_AppendTimestamp(&sb, "%Y/%m/%d", &mock_epoch) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "2026/08/10") == 0);
|
|
|
|
// Isolated gap injection validation with temporal entities
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendStr(&sb, "EventOccurred") == C_SUCCESS);
|
|
assert(c_StringBuffer_InsertTimestampAt(&sb, 0, "%H:%M:%S ", &mock_epoch) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "14:22:45 EventOccurred") == 0);
|
|
|
|
// Running standard OS system time verification (Ensures dynamic layout executes cleanly)
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendCurrentTimestamp(&sb, "%M", 1) == C_SUCCESS); // UTC trace scan
|
|
assert(sb.size == 2); // Double-digit alignment validation
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 5: Lexical Scanners & Backward Lookups --- */
|
|
static void test_lexical_scanners(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 32) == C_SUCCESS);
|
|
assert(c_StringBuffer_AppendStr(&sb, "ping-pong-ping-pong") == C_SUCCESS);
|
|
|
|
// Linear scanning paths verification
|
|
assert(c_StringBuffer_IndexOfStr(&sb, 0, "pong") == 5);
|
|
assert(c_StringBuffer_IndexOfStr(&sb, 6, "pong") == 15); // Offset search skip boundaries
|
|
assert(c_StringBuffer_IndexOfStr(&sb, 0, "missing") == C_ERR_NOT_FOUND);
|
|
assert(c_StringBuffer_IndexOfChar(&sb, 0, '-') == 4);
|
|
assert(c_StringBuffer_IndexOfChar(&sb, 0, 'x') == C_ERR_NOT_FOUND);
|
|
|
|
// High performance reversed traversal trace verification
|
|
assert(c_StringBuffer_LastIndexOfStr(&sb, 19, "ping") == 10);
|
|
assert(c_StringBuffer_LastIndexOfStr(&sb, 8, "ping") == 0); // Window limits validation
|
|
assert(c_StringBuffer_LastIndexOfChar(&sb, 19, '-') == 14);
|
|
assert(c_StringBuffer_LastIndexOfChar(&sb, 2, '-') == C_ERR_NOT_FOUND);
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 6: Matrix Transformations & Space Cleavers --- */
|
|
static void test_transformations_and_cleavers(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 8) == C_SUCCESS);
|
|
|
|
// Substitute logic metrics path variations
|
|
assert(c_StringBuffer_AppendStr(&sb, "one_two_one") == C_SUCCESS);
|
|
assert(c_StringBuffer_ReplaceStr(&sb, "one", "1") == C_SUCCESS); // Footprint size contraction
|
|
assert(strcmp(sb.buffer, "1_two_1") == 0);
|
|
|
|
assert(c_StringBuffer_ReplaceStr(&sb, "1", "three") == C_SUCCESS); // Footprint size expansion delta
|
|
assert(strcmp(sb.buffer, "three_two_three") == 0);
|
|
|
|
// Whitespace elimination tracking loops
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendStr(&sb, " \r\n\t TokenPayload \t ") == C_SUCCESS);
|
|
|
|
assert(c_StringBuffer_TrimLeft(&sb) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "TokenPayload \t ") == 0);
|
|
|
|
assert(c_StringBuffer_TrimRight(&sb) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "TokenPayload") == 0);
|
|
assert(sb.size == 12);
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 7: Lexical Casers & Coordinate Range Extractions --- */
|
|
static void test_casers_and_extractions(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 16) == C_SUCCESS);
|
|
assert(c_StringBuffer_AppendStr(&sb, "xYz987W") == C_SUCCESS);
|
|
|
|
// Case conversions
|
|
assert(c_StringBuffer_ToUpper(&sb) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "XYZ987W") == 0);
|
|
assert(c_StringBuffer_ToLower(&sb) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "xyz987w") == 0);
|
|
|
|
// In-place byte symmetry reversal loop verification
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendStr(&sb, "radar-test") == C_SUCCESS);
|
|
assert(c_StringBuffer_Reverse(&sb) == C_SUCCESS);
|
|
assert(strcmp(sb.buffer, "tset-radar") == 0);
|
|
|
|
// Slicing metrics via Substr
|
|
c_StringBuffer_Clear(&sb);
|
|
assert(c_StringBuffer_AppendStr(&sb, "Distributed-Architecture") == C_SUCCESS);
|
|
c_StringBuffer_t target_slice;
|
|
assert(c_StringBuffer_Substr(&sb, 12, 12, &target_slice) == C_SUCCESS); // Extract "Architecture"
|
|
assert(strcmp(target_slice.buffer, "Architecture") == 0);
|
|
c_StringBuffer_Destroy(&target_slice);
|
|
|
|
// Coordinate clipping window tests via Slice
|
|
assert(c_StringBuffer_Slice(&sb, 0, 11, &target_slice) == C_SUCCESS); // Extract "Distributed"
|
|
assert(strcmp(target_slice.buffer, "Distributed") == 0);
|
|
c_StringBuffer_Destroy(&target_slice);
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 8: Dual-Scan Pipelines & Structural Join Topologies --- */
|
|
static void test_pipeline_and_joins(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 64) == C_SUCCESS);
|
|
assert(c_StringBuffer_AppendStr(&sb, "Alpha::Beta::::Gamma::") == C_SUCCESS); // Multi-byte delimiter with blanks
|
|
|
|
c_StringBuffer_t* token_array = NULL;
|
|
c_size_t token_count = 0;
|
|
|
|
// Process double-scan split array pipeline
|
|
assert(c_StringBuffer_Split(&sb, "::", &token_array, &token_count) == C_SUCCESS);
|
|
|
|
assert(token_count == 5);
|
|
assert(strcmp(token_array[0].buffer, "Alpha") == 0);
|
|
assert(strcmp(token_array[1].buffer, "Beta") == 0);
|
|
assert(strcmp(token_array[2].buffer, "") == 0); // Gap null evaluation validation
|
|
assert(strcmp(token_array[3].buffer, "Gamma") == 0);
|
|
assert(strcmp(token_array[4].buffer, "") == 0); // Terminal tracking null check
|
|
|
|
// High performance sequential recombination loop testing via Join
|
|
c_StringBuffer_t output_combiner;
|
|
assert(c_StringBuffer_Init(&output_combiner, 8) == C_SUCCESS);
|
|
assert(c_StringBuffer_Join(&output_combiner, token_array, token_count, "=>") == C_SUCCESS);
|
|
assert(strcmp(output_combiner.buffer, "Alpha=>Beta=>=>Gamma=>") == 0);
|
|
|
|
// Double-scan array deallocation teardown routines
|
|
for (c_size_t i = 0; i < token_count; i++) {
|
|
c_StringBuffer_Destroy(&token_array[i]);
|
|
}
|
|
free(token_array);
|
|
c_StringBuffer_Destroy(&output_combiner);
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
/* --- Module 9: Evaluation Comparators & Alphabetical Sort Anchors --- */
|
|
static void test_comparators(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 16) == C_SUCCESS);
|
|
assert(c_StringBuffer_AppendStr(&sb, "Microcontroller-C") == C_SUCCESS);
|
|
|
|
// Equality filters verification
|
|
assert(c_StringBuffer_Equals(&sb, "Microcontroller-C") == 1);
|
|
assert(c_StringBuffer_Equals(&sb, "microcontroller-c") == 0);
|
|
assert(c_StringBuffer_EqualsIgnoreCase(&sb, "microcontroller-c") == 1);
|
|
assert(c_StringBuffer_Equals(&sb, "Microcontroller") == 0);
|
|
|
|
// Lexical lookup evaluation boundaries matching typical strcmp return matrices
|
|
assert(c_StringBuffer_Compare(&sb, "Application") > 0); // M > A
|
|
assert(c_StringBuffer_Compare(&sb, "Microcontroller-C") == 0);
|
|
assert(c_StringBuffer_Compare(&sb, "Zebrafish") < 0); // M < Z
|
|
assert(c_StringBuffer_Compare(&sb, NULL) > 0); // Edge-case null baseline swap check
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
static void test_strtoul_conversion(void) {
|
|
c_StringBuffer_t sb;
|
|
assert(c_StringBuffer_Init(&sb, 32) == C_SUCCESS);
|
|
assert(c_StringBuffer_AppendStr(&sb, "Data: 1024, Hex: 0x2A") == C_SUCCESS);
|
|
|
|
unsigned long parsed_val = 0;
|
|
c_size_t end_idx = 0;
|
|
|
|
// Test 1: Parse Base-10 integer from index position 6 ("1024...")
|
|
assert(c_StringBuffer_strtoul(&sb, 6, 10, &parsed_val, &end_idx) == C_SUCCESS);
|
|
assert(parsed_val == 1024);
|
|
assert(end_idx == 10); // Index point of the trailing comma character
|
|
|
|
// Test 2: Parse Base-16 hexadecimal starting from index position 17 ("0x2A")
|
|
assert(c_StringBuffer_strtoul(&sb, 17, 16, &parsed_val, NULL) == C_SUCCESS);
|
|
assert(parsed_val == 42); // 0x2A translates to decimal 42
|
|
|
|
// Test 3: Attempt conversion from non-numeric text index (invalid param error)
|
|
assert(c_StringBuffer_strtoul(&sb, 0, 10, &parsed_val, NULL) == C_ERR_INVALID_PARAM);
|
|
|
|
c_StringBuffer_Destroy(&sb);
|
|
}
|
|
|
|
|
|
#define RUN_TEST_CASE(test_func) \
|
|
do { \
|
|
printf("[RUNNING] %-40s ... ", #test_func); \
|
|
fflush(stdout); \
|
|
test_func(); \
|
|
printf("[PASSED]\n"); \
|
|
} while (0)
|
|
|
|
int main(int argc, char** argv){
|
|
printf("=====================================================================\n");
|
|
printf(" LAUNCHING C_STRINGBUFFER CORNER-CASE SPECIFICATION VERIFICATION \n");
|
|
printf("=====================================================================\n");
|
|
|
|
RUN_TEST_CASE(test_lifecycle_and_clear);
|
|
RUN_TEST_CASE(test_array_mutations);
|
|
RUN_TEST_CASE(test_formatting_engines);
|
|
RUN_TEST_CASE(test_chronology_modules);
|
|
RUN_TEST_CASE(test_lexical_scanners);
|
|
RUN_TEST_CASE(test_transformations_and_cleavers);
|
|
RUN_TEST_CASE(test_casers_and_extractions);
|
|
RUN_TEST_CASE(test_pipeline_and_joins);
|
|
RUN_TEST_CASE(test_comparators);
|
|
RUN_TEST_CASE(test_strtoul_conversion);
|
|
|
|
printf("=====================================================================\n");
|
|
printf(" [🎉 VERIFIED] Absolute architecture spec checklist matches perfectly. \n");
|
|
printf("=====================================================================\n");
|
|
}
|