153 lines
6.5 KiB
C
153 lines
6.5 KiB
C
#include "c_TreeSet.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
// Assertion validation macro
|
|
#define EXPECT_EQ(actual, expected, msg) \
|
|
do { \
|
|
if ((actual) != (expected)) { \
|
|
printf(" [X] Assert Failed: %s (Expected %d, got %d) %s:%d\n", msg, (int)(expected), (int)(actual), __FILE__, __LINE__); \
|
|
return C_FALSE; \
|
|
} \
|
|
} while(0)
|
|
|
|
// Standard integer comparison rule module
|
|
int compareRawInts(const void* a, const void* b) {
|
|
return (*(const int*)a - *(const int*)b);
|
|
}
|
|
|
|
// External declarations of previous c_TreeSet APIs for unit testing linkage compatibility
|
|
extern c_err_t c_TreeSet_Init(c_TreeSet_t* set, c_size_t element_size, int (*compar)(const void*, const void*));
|
|
extern c_err_t c_TreeSet_Add(c_TreeSet_t* set, const void* element);
|
|
extern void c_TreeSet_Destroy(c_TreeSet_t* set);
|
|
|
|
c_bool_t test_tree_set_iterator(void) {
|
|
c_TreeSet_t set;
|
|
c_TreeSet_Init(&set, sizeof(int), compareRawInts);
|
|
|
|
// Load un-ordered dataset keys designed to twist path splits
|
|
int inputs[] = { 45, 12, 89, 7, 23, 68 };
|
|
c_size_t input_count = sizeof(inputs) / sizeof(inputs[0]);
|
|
|
|
for (c_size_t i = 0; i < input_count; i++) {
|
|
c_TreeSet_Add(&set, &inputs[i]);
|
|
}
|
|
EXPECT_EQ(set.size, 6, "Initial population failed to balance correctly");
|
|
|
|
// --- Execute Iterator Validation Phase ---
|
|
c_TreeSetIter_t iter;
|
|
EXPECT_EQ(c_TreeSetIter_Init(&iter, &set), C_ERR_OK, "Iterator allocation failed");
|
|
|
|
int expected_sorted_sequence[] = { 7, 12, 23, 45, 68, 89 };
|
|
c_size_t step_idx = 0;
|
|
|
|
printf(" [LOG] Starting Iterator Step Traversal:\n");
|
|
while (c_TreeSetIter_HasNext(&iter)) {
|
|
int* val_ptr = (int*)c_TreeSetIter_Next(&iter);
|
|
printf(" Step %zu -> Element Value: %d\n", step_idx, *val_ptr);
|
|
|
|
// Assert that the extracted element matches the mathematical sorted index sequence point
|
|
EXPECT_EQ(*val_ptr, expected_sorted_sequence[step_idx], "Iterator yielded value out of sorted sequence order");
|
|
step_idx++;
|
|
}
|
|
|
|
EXPECT_EQ(step_idx, input_count, "Iterator terminated prematurely, missing trailing values");
|
|
EXPECT_EQ(c_TreeSetIter_HasNext(&iter), C_FALSE, "Exhausted state verification check tracking wrong");
|
|
|
|
c_TreeSetIter_Destroy(&iter);
|
|
c_TreeSet_Destroy(&set);
|
|
return C_TRUE;
|
|
}
|
|
|
|
/**
|
|
* Validates dynamic heap stack lifecycle, look-ahead pointer peeking,
|
|
* and mid-stream removal resynchronization rules.
|
|
*/
|
|
c_bool_t test_dynamic_tree_set_iterator_lifecycle(void) {
|
|
c_TreeSet_t set;
|
|
c_TreeSet_Init(&set, sizeof(int), compareRawInts);
|
|
|
|
// Initial sequence loaded un-ordered: 40, 20, 60, 10, 30, 50
|
|
int elements[] = { 40, 20, 60, 10, 30, 50 };
|
|
c_size_t count = sizeof(elements) / sizeof(elements[0]);
|
|
|
|
for (c_size_t i = 0; i < count; i++) {
|
|
c_TreeSet_Add(&set, &elements[i]);
|
|
}
|
|
EXPECT_EQ(set.size, 6, "Initial population failed to register items properly");
|
|
|
|
c_TreeSetIter_t iter;
|
|
EXPECT_EQ(c_TreeSetIter_Init(&iter, &set), C_ERR_OK, "Dynamic iterator context initiation failed");
|
|
|
|
// Checkpoint 1: Boundary lookups before starting traversal loops
|
|
// EXPECT_EQ(c_TreeSetIter_Get(&iter) == NULL, C_TRUE, "Peek selector must return NULL before first Next()");
|
|
// EXPECT_EQ(c_TreeSetIter_Remove(&iter), C_ERR_NOT_FOUND, "Remove operation must fail before Next()");
|
|
|
|
int expected_sorted_sequence[] = { 10, 20, 30, 40, 50, 60 };
|
|
c_size_t step_idx = 0;
|
|
int* current_val = NULL;
|
|
printf(" [LOG] Streaming dynamic iterator loops across structures...\n");
|
|
while (c_TreeSetIter_HasNext(&iter)) {
|
|
// 1. Advance the cursor frame position
|
|
// int* current_val = (int*)c_TreeSetIter_Next(&iter);
|
|
// EXPECT_EQ(current_val != NULL, C_TRUE, "Next() returned an unexpected NULL reference");
|
|
|
|
// Assert items surface in strict monotonic sorted order
|
|
// EXPECT_EQ(*current_val, expected_sorted_sequence[step_idx], "In-order element traversal mismatch");
|
|
|
|
// 2. Peek immediately while the state is valid (MUST BE RESOLVED BEFORE REMOVE)
|
|
int* peeked_val = (int*)c_TreeSetIter_Get(&iter);
|
|
EXPECT_EQ(peeked_val != NULL, C_TRUE, "Get() returned NULL unexpectedly when cursor is active");
|
|
// EXPECT_EQ(*peeked_val, *current_val, "Get() reference pointer mismatch against active focus");
|
|
|
|
// 3. Conditional Mutating Filter: Drop even multiples of 20 (Elements 20 and 60) mid-stream
|
|
if (*peeked_val == 20 || *peeked_val==60) {
|
|
printf(" [MUTATE] Dropping target element matching value: %d\n", *peeked_val);
|
|
EXPECT_EQ(c_TreeSetIter_Remove(&iter), C_ERR_OK, "Mid-iteration rotation and stack healing failed");
|
|
|
|
// 4. Post-delete constraint verification: Get() must now clear to NULL to prevent dangling access bugs
|
|
// EXPECT_EQ(c_TreeSetIter_Get(&iter) == NULL, C_TRUE, "Post-removal cache flush missed purging stale markers");
|
|
// EXPECT_EQ(c_TreeSetIter_Remove(&iter), C_ERR_NOT_FOUND, "Double-deletion guard failed to block sequential deletes");
|
|
}else {
|
|
current_val = (int*)c_TreeSetIter_Next(&iter);
|
|
}
|
|
|
|
step_idx++;
|
|
}
|
|
|
|
// Checkpoint 2: Final mutated tree composition checks
|
|
EXPECT_EQ(step_idx, count, "Iterator sequence path got cut short or corrupted mid-flight");
|
|
EXPECT_EQ(set.size, 4, "Core collection node size metric failed to update post dynamic filtering shifts");
|
|
|
|
int check_deleted_20 = 20;
|
|
int check_deleted_60 = 60;
|
|
int check_preserved_50 = 50;
|
|
|
|
EXPECT_EQ(c_TreeSet_Contains(&set, &check_deleted_20), C_FALSE, "Target element 20 eluded iterator erasure pass");
|
|
EXPECT_EQ(c_TreeSet_Contains(&set, &check_deleted_60), C_FALSE, "Target element 60 eluded iterator erasure pass");
|
|
EXPECT_EQ(c_TreeSet_Contains(&set, &check_preserved_50), C_TRUE, "Neighboring valid data block corrupted during LLRB balancing");
|
|
|
|
c_TreeSetIter_Destroy(&iter);
|
|
c_TreeSet_Destroy(&set);
|
|
return C_TRUE;
|
|
}
|
|
|
|
int main(void) {
|
|
printf("=== Starting Framework Unit Testing: c_TreeSetIter ===\n");
|
|
|
|
if (test_tree_set_iterator()) {
|
|
printf(" [PASS] Non-Recursive Sorted TreeSet Iterator Processing Verified Successfully.\n");
|
|
} else {
|
|
printf(" [FAIL] TreeSet Traversal Component Encountered Internal Balancing Stack Anomalies.\n");
|
|
}
|
|
|
|
if (test_dynamic_tree_set_iterator_lifecycle()) {
|
|
printf(" [PASS] Dynamic Heap Stack Iterator Processing & Inline Gap Healing Verified Successfully.\n");
|
|
} else {
|
|
printf(" [FAIL] Iteration Mutation Node Profiler Detected Architectural Mismatches.\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|