#include "c_LinearProbingHashST.h" #include #include #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) // Struct payload representing active channel properties typedef struct { uint32_t src_address; uint32_t dest_address; uint16_t block_length; } DMAControlBlock; int compareUint16Keys(const void* a, const void* b) { return (*(const uint16_t*)a - *(const uint16_t*)b); } c_bool_t test_linear_probing_hash_st(void) { c_LinearProbingHashST_t lpst; // Start with a small capacity = 4 to guarantee load threshold resize checks fire (triggers at N >= 2) EXPECT_EQ(c_LinearProbingHashST_Init(&lpst, 4, sizeof(uint16_t), sizeof(DMAControlBlock), NULL, compareUint16Keys), C_ERR_OK, "Init failed"); uint16_t ch0 = 12; DMAControlBlock d0 = { 0x20000000, 0x40000000, 512 }; uint16_t ch1 = 44; DMAControlBlock d1 = { 0x20001000, 0x40002000, 1024 }; uint16_t ch2 = 19; DMAControlBlock d2 = { 0x10000000, 0x30000000, 256 }; // 1. Core Data Entry Flows EXPECT_EQ(c_LinearProbingHashST_Put(&lpst, &ch0, &d0), C_ERR_OK, "Put ch0 failed"); EXPECT_EQ(c_LinearProbingHashST_Put(&lpst, &ch1, &d1), C_ERR_OK, "Put ch1 failed (Resizing expansion milestone)"); EXPECT_EQ(c_LinearProbingHashST_Put(&lpst, &ch2, &d2), C_ERR_OK, "Put ch2 failed"); EXPECT_EQ(lpst.N, 3, "Active count verification miscalculated"); // 2. Overwrite Mutation Check DMAControlBlock d0_updated = { 0x20000000, 0x40000000, 2048 }; EXPECT_EQ(c_LinearProbingHashST_Put(&lpst, &ch0, &d0_updated), C_ERR_OK, "Overwriting entry failed"); EXPECT_EQ(lpst.N, 3, "Size increased incorrectly during an overwrite operation"); // 3. Retrieval Lookups uint16_t look_ch = 12; DMAControlBlock* fetched = (DMAControlBlock*)c_LinearProbingHashST_Get(&lpst, &look_ch); EXPECT_EQ(fetched != NULL && fetched->block_length == 2048, C_TRUE, "Lookup retrieved incorrect mapping segment values"); uint16_t miss_ch = 99; EXPECT_EQ(c_LinearProbingHashST_Get(&lpst, &miss_ch) == NULL, C_TRUE, "Key miss lookup did not return NULL"); EXPECT_EQ(c_LinearProbingHashST_Contains(&lpst, &look_ch), C_TRUE, "Contains reporting failure on active keys"); // 4. Verification of Cluster-Repair Mechanics on Delete EXPECT_EQ(c_LinearProbingHashST_Delete(&lpst, &look_ch), C_ERR_OK, "Deletion operation crashed"); EXPECT_EQ(c_LinearProbingHashST_Contains(&lpst, &look_ch), C_FALSE, "Deleted element still reported within lookups"); EXPECT_EQ(lpst.N, 2, "Size tracker did not reduce correctly after deletion"); // Assert that sibling entries pushed past index gaps remain accessible after cluster repair uint16_t look_sibling = 19; EXPECT_EQ(c_LinearProbingHashST_Contains(&lpst, &look_sibling), C_TRUE, "Linear probing gap repair broke adjacent cluster lookups"); c_LinearProbingHashST_Destroy(&lpst); return C_TRUE; } int main(void) { printf("=== Starting Framework Unit Testing: c_LinearProbingHashST ===\n"); if (test_linear_probing_hash_st()) { printf(" [PASS] Linear Probing Hash ST Optimization and Cluster Repair Lifecycles Verified Successfully.\n"); } else { printf(" [FAIL] Linear Probing Hash Table Component Encountered Evaluation Errors.\n"); } return 0; }