Files

75 lines
3.0 KiB
C
Raw Permalink Normal View History

2026-08-10 01:21:15 +08:00
#include "c_TreeSet.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)
// Unique complex elements tracked inside the TreeSet structure
typedef struct {
uint32_t device_class;
uint32_t hardware_hash;
} CryptoUID;
int compareCryptoUIDs(const void* a, const void* b) {
const CryptoUID* u1 = (const CryptoUID*)a;
const CryptoUID* u2 = (const CryptoUID*)b;
if (u1->device_class != u2->device_class) {
return (u1->device_class > u2->device_class) - (u1->device_class < u2->device_class);
}
return (u1->hardware_hash > u2->hardware_hash) - (u1->hardware_hash < u2->hardware_hash);
}
c_bool_t test_tree_set_lifecycle(void) {
c_TreeSet_t set;
EXPECT_EQ(c_TreeSet_Init(&set, sizeof(CryptoUID), compareCryptoUIDs), C_ERR_OK, "Init failed");
CryptoUID u0 = { 0x0001, 0xABCDEFAA };
CryptoUID u1 = { 0x0002, 0x12345678 };
CryptoUID u2 = { 0x0001, 0x99999999 }; // Same class as u0, distinct hash
// 1. Core Data Entry Flows & Uniqueness Rejection Checks
EXPECT_EQ(c_TreeSet_Add(&set, &u0), C_ERR_OK, "Add u0 failed");
EXPECT_EQ(c_TreeSet_Add(&set, &u1), C_ERR_OK, "Add u1 failed");
EXPECT_EQ(c_TreeSet_Add(&set, &u2), C_ERR_OK, "Add u2 failed");
EXPECT_EQ(set.size, 3, "Size tracker tracking variable mismatched");
// Enforce Set duplicate rule constraint protection mechanisms
EXPECT_EQ(c_TreeSet_Add(&set, &u0), C_ERR_ALREADY_EXISTS, "Duplicate uniqueness validation bypassed");
EXPECT_EQ(set.size, 3, "Size modified on blocked duplicate insertion");
// 2. Contains Search Lookup Verification
EXPECT_EQ(c_TreeSet_Contains(&set, &u1), C_TRUE, "Contains failed tracking an active registered element");
CryptoUID fake_uid = { 0x0005, 0x00000000 };
EXPECT_EQ(c_TreeSet_Contains(&set, &fake_uid), C_FALSE, "Contains yielded false-positive on unknown keys");
// 3. Balanced Node Deletion Check
EXPECT_EQ(c_TreeSet_Remove(&set, &u1), C_ERR_OK, "Remove execution failed");
EXPECT_EQ(c_TreeSet_Contains(&set, &u1), C_FALSE, "Target node still visible inside index post removal sequence");
EXPECT_EQ(set.size, 2, "Size tracker missed downward structural adjustment steps");
// Confirm neighboring elements remain functional post LLRB rebalancing
EXPECT_EQ(c_TreeSet_Contains(&set, &u2), C_TRUE, "Sibling node split clobbered during black height adjustment cycles");
c_TreeSet_Destroy(&set);
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Unit Testing: c_TreeSet ===\n");
if (test_tree_set_lifecycle()) {
printf(" [PASS] TreeSet Unique Element De-duplication and LLRB Structural Balancing Lifecycles Verified.\n");
} else {
printf(" [FAIL] TreeSet Structure Component Validation Failure.\n");
}
return 0;
}