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

73 lines
2.8 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_BinarySearchST.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 values block mapped to compact numeric lookup keys
typedef struct {
char cluster_name[32];
double operational_load;
int backup_port;
} DNSProfile;
int compareIntKeys(const void* a, const void* b) {
return (*(int*)a - *(int*)b);
}
c_bool_t test_binary_search_st(void) {
c_BinarySearchST_t st;
// Initialize with a capacity of 2 to verify dynamic parallel allocation resize cycles
EXPECT_EQ(c_BinarySearchST_Init(&st, 2, sizeof(int), sizeof(DNSProfile), compareIntKeys), C_ERR_OK, "Init failed");
int k1 = 8080; DNSProfile v1 = { "Primary Cluster", 0.45, 9001 };
int k2 = 4433; DNSProfile v2 = { "Secure Edge Node", 0.12, 9002 };
int k3 = 7021; DNSProfile v3 = { "Fallback Stack", 0.89, 9003 };
// 1. Data Entry Flow
EXPECT_EQ(c_BinarySearchST_Put(&st, &k1, &v1), C_ERR_OK, "Put k1 failed");
EXPECT_EQ(c_BinarySearchST_Put(&st, &k2, &v2), C_ERR_OK, "Put k2 failed");
EXPECT_EQ(c_BinarySearchST_Put(&st, &k3, &v3), C_ERR_OK, "Put k3 failed (Resize step validation)");
EXPECT_EQ(st.size, 3, "Symbol Table size tracker count incorrect");
// 2. Overwrite Verification
DNSProfile v1_updated = { "Primary Cluster v2", 0.52, 9001 };
EXPECT_EQ(c_BinarySearchST_Put(&st, &k1, &v1_updated), C_ERR_OK, "Overwriting entry failed");
EXPECT_EQ(st.size, 3, "Size increased incorrectly during an overwrite operation");
// 3. Data Retrieval lookups
int look_key = 8080;
DNSProfile* fetched = (DNSProfile*)c_BinarySearchST_Get(&st, &look_key);
EXPECT_EQ(fetched != NULL && strcmp(fetched->cluster_name, "Primary Cluster v2") == 0, C_TRUE, "Lookup retrieved incorrect mapping segment");
int miss_key = 9999;
EXPECT_EQ(c_BinarySearchST_Get(&st, &miss_key) == NULL, C_TRUE, "Key miss lookup did not return NULL");
// 4. Deletion Cycles
int delete_key = 4433;
EXPECT_EQ(c_BinarySearchST_Delete(&st, &delete_key), C_ERR_OK, "Deletion operation crashed");
EXPECT_EQ(c_BinarySearchST_Contains(&st, &delete_key), C_FALSE, "Deleted element still reported within lookups");
EXPECT_EQ(st.size, 2, "Size tracker did not reduce correctly after deletion");
c_BinarySearchST_Destroy(&st);
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Unit Testing: c_BinarySearchST ===\n");
if (test_binary_search_st()) {
printf(" [PASS] Symbol Table Parallel Array Processing and Retrieval Verified Successfully.\n");
} else {
printf(" [FAIL] Symbol Table Component Encountered Evaluation Errors.\n");
}
return 0;
}