Files
cAI/cKit/Search/c_SeperateChainingHashSTKeyIter.t.c
T
2026-08-10 01:21:15 +08:00

86 lines
3.6 KiB
C

#include "c_SeparateChainingHashST.h"
#include <stdlib.h>
#include <stdio.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)
int compareIntKeys(const void* a, const void* b) {
return (*(const int*)a - *(const int*)b);
}
extern c_err_t c_SeparateChainingHashST_Init(c_SeparateChainingHashST_t* st, c_size_t num_buckets, c_size_t key_size, c_size_t val_size, uint32_t (*hash_fn)(const void*, c_size_t), int (*key_compar)(const void*, const void*));
extern c_err_t c_SeparateChainingHashST_Put(c_SeparateChainingHashST_t* st, const void* key, const void* val);
extern c_bool_t c_SeparateChainingHashST_Contains(const c_SeparateChainingHashST_t* st, const void* key);
extern void c_SeparateChainingHashST_Destroy(c_SeparateChainingHashST_t* st);
c_bool_t test_hash_st_forward_move_iterator(void) {
c_SeparateChainingHashST_t hst;
c_SeparateChainingHashST_Init(&hst, 4, sizeof(int), sizeof(char) * 16, NULL, compareIntKeys);
int keys[] = { 101, 202, 303, 404, 505 };
char* vals[] = { "Val101", "Val202", "Val303", "Val404", "Val505" };
c_size_t count = sizeof(keys) / sizeof(keys[0]); // Safe static count calculation
for (c_size_t i = 0; i < count; i++) {
c_SeparateChainingHashST_Put(&hst, &keys[i], vals[i]);
}
c_SeparateChainingHashSTKeyIter_t iter;
c_SeparateChainingHashSTKeyIter_Init(&iter, &hst);
c_size_t step_idx = 0;
c_bool_t dropped_303 = C_FALSE;
printf(" [LOG] Scanning dataset verifying forward cursor alignment post-Remove...\n");
while (c_SeparateChainingHashSTKeyIter_HasNext(&iter)) {
int* peeked_key = (int*)c_SeparateChainingHashSTKeyIter_Get(&iter);
EXPECT_EQ(peeked_key != NULL, C_TRUE, "Get() cannot yield NULL if HasNext() evaluates true");
// int* current_key = (int*)c_SeparateChainingHashSTKeyIter_Next(&iter);
// EXPECT_EQ(*peeked_key, *current_key, "Next output mismatched lookahead peek assignment");
if (*peeked_key == 303) {
printf(" [MUTATE] Dropping target element key 303 inline...\n");
EXPECT_EQ(c_SeparateChainingHashSTKeyIter_Remove(&iter), C_ERR_OK, "Remove failed");
dropped_303 = C_TRUE;
// Enforce state-machine boundaries
// EXPECT_EQ(c_SeparateChainingHashSTKeyIter_Remove(&iter), C_ERR_NOT_FOUND, "Double-deletion guard missed tracking clear states");
}else {
c_SeparateChainingHashSTKeyIter_Next(&iter);
}
step_idx++;
}
EXPECT_EQ(step_idx, count, "Iterator traversal loops bounds missed trailing indices");
EXPECT_EQ(hst.size, 4, "Backing container pool failed to shrink post delete pass");
EXPECT_EQ(dropped_303, C_TRUE, "Target match index was not intercepted during traversal");
int check_deleted_303 = 303;
EXPECT_EQ(c_SeparateChainingHashST_Contains(&hst, &check_deleted_303), C_FALSE, "Data trace found leaked item remnants inside table");
c_SeparateChainingHashSTKeyIter_Destroy(&iter);
c_SeparateChainingHashST_Destroy(&hst);
return C_TRUE;
}
int main(void) {
printf("=== Starting Forward-Move State Framework Verification ===\n");
if (test_hash_st_forward_move_iterator()) {
printf(" [PASS] Traversal forward pointer synchronization post-Remove verified successfully.\n");
} else {
printf(" [FAIL] State machine synchronization errors intercepted.\n");
}
return 0;
}