#include "c_RedBlackBST.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) // Bulk structural element tracked inside the Red-Black table lines typedef struct { uint32_t reset_mask; uint8_t access_rights; } RegConfig; int compareUint32Keys(const void* a, const void* b) { uint32_t k1 = *(const uint32_t*)a; uint32_t k2 = *(const uint32_t*)b; return (k1 > k2) - (k1 < k2); } c_bool_t test_red_black_bst(void) { c_RedBlackBST_t rbbst; EXPECT_EQ(c_RedBlackBST_Init(&rbbst, sizeof(uint32_t), sizeof(RegConfig), compareUint32Keys), C_ERR_OK, "Init failed"); // Sequentially insert data designed to cause worst-case unbalanced degradation in standard BSTs // (Strictly ascending keys: 0x1000 -> 0x2000 -> 0x3000 -> 0x4000) uint32_t reg0 = 0x1000; RegConfig c0 = { 0xFFFFFFFF, 0x01 }; uint32_t reg1 = 0x2000; RegConfig c1 = { 0x00000000, 0x02 }; uint32_t reg2 = 0x3000; RegConfig c2 = { 0x0000FFFF, 0x01 }; uint32_t reg3 = 0x4000; RegConfig c3 = { 0x12345678, 0x03 }; EXPECT_EQ(c_RedBlackBST_Put(&rbbst, ®0, &c0), C_ERR_OK, "Put reg0 failed"); EXPECT_EQ(c_RedBlackBST_Put(&rbbst, ®1, &c1), C_ERR_OK, "Put reg1 failed"); EXPECT_EQ(c_RedBlackBST_Put(&rbbst, ®2, &c2), C_ERR_OK, "Put reg2 failed"); EXPECT_EQ(c_RedBlackBST_Put(&rbbst, ®3, &c3), C_ERR_OK, "Put reg3 failed"); EXPECT_EQ(rbbst.size, 4, "Red-Black size tracking variable mismatched"); // Verify retrieval processing paths are balanced and accessible uint32_t look_reg = 0x3000; RegConfig* fetched = (RegConfig*)c_RedBlackBST_Get(&rbbst, &look_reg); EXPECT_EQ(fetched != NULL && fetched->reset_mask == 0x0000FFFF, C_TRUE, "Lookup retrieved incorrect mapping segment values"); uint32_t missing_reg = 0x5000; EXPECT_EQ(c_RedBlackBST_Get(&rbbst, &missing_reg) == NULL, C_TRUE, "Lookup returned false positive on missing segment rules"); EXPECT_EQ(c_RedBlackBST_Contains(&rbbst, &look_reg), C_TRUE, "Contains reporting failure on active keys"); // Assert that the tree correctly balanced itself. // In a left-leaning red-black tree, inserting 0x1000, 0x2000, 0x3000 in ascending order // forces 0x2000 to become the root node. uint32_t root_key = *(uint32_t*)c_RBBST_NodeKey(rbbst.root); EXPECT_EQ(root_key, 0x2000, "Left-leaning balancing rotation routines failed to adjust root position correctly"); c_RedBlackBST_Destroy(&rbbst); return C_TRUE; } int main(void) { printf("=== Starting Framework Unit Testing: c_RedBlackBST ===\n"); if (test_red_black_bst()) { printf(" [PASS] Left-Leaning Red-Black BST Node Packing and Balancing Routines Verified Successfully.\n"); } else { printf(" [FAIL] Red-Black Tree Architecture Component Validation Errors Detected.\n"); } return 0; }