Files
cAI/cKit/Search/c_TreeMapKeyIter.t.c
T

97 lines
4.2 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_TreeMap.h"
#include <stdlib.h>
#include <stdio.h>
#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 compareIntKeys(const void* a, const void* b) {
return (*(const int*)a - *(const int*)b);
}
c_bool_t test_tree_map_key_iterator_lifecycle(void) {
c_TreeMap_t map;
c_TreeMap_Init(&map, sizeof(int), sizeof(char) * 16, compareIntKeys);
// Initial sequence block loaded un-ordered: 400, 200, 600, 100, 300
int keys[] = { 400, 200, 600, 100, 300 };
char* vals[] = { "Data400", "Data200", "Data600", "Data100", "Data300" };
c_size_t count = C_ARRAY_SIZE(keys);
for (c_size_t i = 0; i < count; i++) {
c_TreeMap_Put(&map, &keys[i], vals[i]);
}
EXPECT_EQ(map.size, 5, "Initial map population failed to register items properly");
c_TreeMapKeyIter_t iter;
EXPECT_EQ(c_TreeMapKeyIter_Init(&iter, &map), C_ERR_OK, "Dynamic iterator initialization failed");
// Checkpoint 1: Boundary lookups before starting traversal loops
// EXPECT_EQ(c_TreeMapKeyIter_Get(&iter) == NULL, C_TRUE, "Peek selector must return NULL before first Next()");
// EXPECT_EQ(c_TreeMapKeyIter_Remove(&iter), C_ERR_NOT_FOUND, "Remove operation must fail before Next()");
int expected_sorted_keys[] = { 100, 200, 300, 400, 600 };
c_size_t step_idx = 0;
printf(" [LOG] Streaming dynamic TreeMap Key iterator loops...\n");
while (c_TreeMapKeyIter_HasNext(&iter)) {
// 1. Advance the cursor frame position
// int* current_key = (int*)c_TreeMapKeyIter_Next(&iter);
// EXPECT_EQ(current_key != NULL, C_TRUE, "Next() returned an unexpected NULL reference");
//
// // Assert keys surface in strict monotonic sorted order
// EXPECT_EQ(*current_key, expected_sorted_keys[step_idx], "In-order key traversal mismatch");
// 2. Peek immediately while the state is valid (MUST BE RESOLVED BEFORE REMOVE)
int* peeked_key = (int*)c_TreeMapKeyIter_Get(&iter);
EXPECT_EQ(peeked_key != NULL, C_TRUE, "Get() returned NULL unexpectedly when cursor is active");
// EXPECT_EQ(*peeked_key, *current_key, "Get() reference pointer mismatch against active focus");
// 3. Conditional Mutating Filter: Drop target key 300 mid-stream
if (*peeked_key == 300) {
printf(" [MUTATE] Dropping target map key matching value: %d\n", *peeked_key);
EXPECT_EQ(c_TreeMapKeyIter_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_TreeMapKeyIter_Get(&iter) != NULL, C_TRUE, "Post-removal cache flush missed purging stale markers");
// EXPECT_EQ(c_TreeMapKeyIter_Remove(&iter), C_ERR_NOT_FOUND, "Double-deletion guard failed to block sequential deletes");
}else {
c_TreeMapKeyIter_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(map.size, 4, "Core collection node size metric failed to update post dynamic filtering shifts");
int check_deleted_300 = 300;
int check_preserved_400 = 400;
EXPECT_EQ(c_TreeMap_Contains(&map, &check_deleted_300), C_FALSE, "Target key 300 eluded iterator erasure pass");
EXPECT_EQ(c_TreeMap_Contains(&map, &check_preserved_400), C_TRUE, "Neighboring valid data block corrupted during LLRB balancing");
c_TreeMapKeyIter_Destroy(&iter);
c_TreeMap_Destroy(&map);
return C_TRUE;
}
int main(void) {
printf("=== Starting Dynamic Framework Verification: c_TreeMapKeyIter ===\n");
if (test_tree_map_key_iterator_lifecycle()) {
printf(" [PASS] Dynamic Heap Stack Key Iterator Traversal & Mutation Gaps Healed Successfully.\n");
} else {
printf(" [FAIL] Iteration Mutation Key Profiler Detected Evaluation Errors.\n");
}
return 0;
}