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

78 lines
3.4 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_SeparateChainingHashST.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)\n", msg, (int)(expected), (int)(actual)); \
return C_FALSE; \
} \
} while(0)
// Bulk structural element tracked inside the Hash Table chains
typedef struct {
uint32_t bytes_transferred;
uint16_t keep_alive_timeout;
char encryption_type[8];
} SessionProfile;
int compareIntKeys(const void* a, const void* b) {
return (*(const int*)a - *(const int*)b);
}
c_bool_t test_separate_chaining_hash_st(void) {
c_SeparateChainingHashST_t hst;
// Initialize with 4 bucket links to verify cascading index distribution limits
EXPECT_EQ(c_SeparateChainingHashST_Init(&hst, 4, sizeof(int), sizeof(SessionProfile), NULL, compareIntKeys), C_ERR_OK, "Init failed");
int sock0 = 8400; SessionProfile p0 = { 1024, 60, "TLS1.3" };
int sock1 = 1250; SessionProfile p1 = { 4096, 120, "AES256" };
int sock2 = 9100; SessionProfile p2 = { 0, 30, "NONE" };
// 1. Structural Insertion Pipeline Validation
EXPECT_EQ(c_SeparateChainingHashST_Put(&hst, &sock0, &p0), C_ERR_OK, "Put sock0 failed");
EXPECT_EQ(c_SeparateChainingHashST_Put(&hst, &sock1, &p1), C_ERR_OK, "Put sock1 failed");
EXPECT_EQ(c_SeparateChainingHashST_Put(&hst, &sock2, &p2), C_ERR_OK, "Put sock2 failed");
EXPECT_EQ(hst.size, 3, "Hash symbol table size count validation inaccurate");
// Check value updating via key collisions
SessionProfile p0_updated = { 5500, 60, "TLS1.3" };
EXPECT_EQ(c_SeparateChainingHashST_Put(&hst, &sock0, &p0_updated), C_ERR_OK, "Overwriting element key failed");
EXPECT_EQ(hst.size, 3, "Size tracker tracking variable modified incorrectly on updates");
// 2. Data Retrieval Lookup Paths
int look_sock = 8400;
SessionProfile* fetched = (SessionProfile*)c_SeparateChainingHashST_Get(&hst, &look_sock);
EXPECT_EQ(fetched != NULL && fetched->bytes_transferred == 5500, C_TRUE, "Lookup retrieved incorrect mapping segment values");
int miss_sock = 9999;
EXPECT_EQ(c_SeparateChainingHashST_Get(&hst, &miss_sock) == NULL, C_TRUE, "Lookup returned false positive on missing keys");
EXPECT_EQ(c_SeparateChainingHashST_Contains(&hst, &look_sock), C_TRUE, "Contains reporting failure on active keys");
// 3. Node Removal & Bucket Link Pointer Redirection Checks
EXPECT_EQ(c_SeparateChainingHashST_Delete(&hst, &look_sock), C_ERR_OK, "Node deletion failed");
EXPECT_EQ(c_SeparateChainingHashST_Contains(&hst, &look_sock), C_FALSE, "Deleted node reference remains inside structure");
EXPECT_EQ(hst.size, 2, "Hash size tracker value did not decrement correctly");
// Verify other elements remain completely accessible post chain alteration
int verify_sibling_sock = 1250;
EXPECT_EQ(c_SeparateChainingHashST_Contains(&hst, &verify_sibling_sock), C_TRUE, "Sibling chain element broken during unlinking steps");
c_SeparateChainingHashST_Destroy(&hst);
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Unit Testing: c_SeparateChainingHashST ===\n");
if (test_separate_chaining_hash_st()) {
printf(" [PASS] Separate Chaining Hash ST Processing and Unlinking Lifecycles Verified Successfully.\n");
} else {
printf(" [FAIL] Hash Table Component Encountered Evaluation Errors.\n");
}
return 0;
}