#include "c_BST.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) // Value data block mapped directly to distinct scalar keys typedef struct { char runtime_state[16]; int scheduling_weight; } ProcessMetrics; int compareIntPids(const void* a, const void* b) { return (*(int*)a - *(int*)b); } c_bool_t test_binary_search_tree(void) { c_BST_t bst; EXPECT_EQ(c_BST_Init(&bst, sizeof(int), sizeof(ProcessMetrics), compareIntPids), C_ERR_OK, "Init failed"); int pid1 = 4500; ProcessMetrics m1 = { "RUNNING", 10 }; int pid2 = 1200; ProcessMetrics m2 = { "SLEEPING", 2 }; int pid3 = 8900; ProcessMetrics m3 = { "BLOCKED", 5 }; int pid4 = 3100; ProcessMetrics m4 = { "ZOMBIE", 0 }; // 1. Structural Insertion Pipeline Validation EXPECT_EQ(c_BST_Put(&bst, &pid1, &m1), C_ERR_OK, "Put pid1 failed"); EXPECT_EQ(c_BST_Put(&bst, &pid2, &m2), C_ERR_OK, "Put pid2 failed"); EXPECT_EQ(c_BST_Put(&bst, &pid3, &m3), C_ERR_OK, "Put pid3 failed"); EXPECT_EQ(c_BST_Put(&bst, &pid4, &m4), C_ERR_OK, "Put pid4 failed"); EXPECT_EQ(bst.size, 4, "Tree dimension size tracker calculation inaccurate"); // Check value updating via key collisions ProcessMetrics m1_updated = { "SUSPENDED", 8 }; EXPECT_EQ(c_BST_Put(&bst, &pid1, &m1_updated), C_ERR_OK, "Overwriting element key failed"); EXPECT_EQ(bst.size, 4, "Tree size tracking incremented incorrectly on updating overwrite"); // 2. Data Retrieval Lookup Paths int look_pid = 4500; ProcessMetrics* fetched = (ProcessMetrics*)c_BST_Get(&bst, &look_pid); EXPECT_EQ(fetched != NULL && strcmp(fetched->runtime_state, "SUSPENDED") == 0, C_TRUE, "Lookup retrieved incorrect mapping segment"); int miss_pid = 9999; EXPECT_EQ(c_BST_Get(&bst, &miss_pid) == NULL, C_TRUE, "Key miss lookup did not return NULL"); EXPECT_EQ(c_BST_Contains(&bst, &look_pid), C_TRUE, "Contains failed reporting registered key tracking rules"); // 3. Node Removal & Hibbard Rebalancing Checks // Delete pid1 (4500), which represents a root node with two children (1200 and 8900) EXPECT_EQ(c_BST_Delete(&bst, &pid1), C_ERR_OK, "Node deletion failed"); EXPECT_EQ(c_BST_Contains(&bst, &pid1), C_FALSE, "Deleted node reference remains inside structure"); EXPECT_EQ(bst.size, 3, "Tree size tracking value did not decrement correctly"); // Verify sub-trees remain completely searchable and accessible post structural mutation int verify_child_pid = 3100; ProcessMetrics* child_check = (ProcessMetrics*)c_BST_Get(&bst, &verify_child_pid); EXPECT_EQ(child_check != NULL && strcmp(child_check->runtime_state, "ZOMBIE") == 0, C_TRUE, "Subtree structure broken during root transformation sequences"); c_BST_Destroy(&bst); return C_TRUE; } int main(void) { printf("=== Starting Framework Unit Testing: c_BST ===\n"); if (test_binary_search_tree()) { printf(" [PASS] Binary Search Tree Processing and Deletion Lifecycle Verified Successfully.\n"); } else { printf(" [FAIL] Binary Search Tree Component Encountered Evaluation Errors.\n"); } return 0; }