Files

68 lines
2.7 KiB
C
Raw Permalink Normal View History

2026-08-10 01:21:15 +08:00
#include "c_TreeMap.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)
// Complex value element structure mapped inside the balanced tree rows
typedef struct {
uint64_t physical_address;
c_bool_t dirty_flag;
} CacheLine;
int compareStringKeys(const void* a, const void* b) {
return strcmp((const char*)a, (const char*)b);
}
c_bool_t test_tree_map_lifecycle(void) {
c_TreeMap_t map;
// Keys are plain strings (fixed width char arrays for raw memory copying safety)
EXPECT_EQ(c_TreeMap_Init(&map, 16, sizeof(CacheLine), compareStringKeys), C_ERR_OK, "Init failed");
char k1[16] = "TAG_BLOCK_0"; CacheLine v1 = { 0x00100000, C_FALSE };
char k2[16] = "TAG_BLOCK_1"; CacheLine v2 = { 0x00200000, C_TRUE };
char k3[16] = "TAG_BLOCK_2"; CacheLine v3 = { 0x00300000, C_FALSE };
// 1. Core Data Entry Flows
EXPECT_EQ(c_TreeMap_Put(&map, k1, &v1), C_ERR_OK, "Put k1 failed");
EXPECT_EQ(c_TreeMap_Put(&map, k2, &v2), C_ERR_OK, "Put k2 failed");
EXPECT_EQ(c_TreeMap_Put(&map, k3, &v3), C_ERR_OK, "Put k3 failed");
EXPECT_EQ(map.size, 3, "Size tracker tracking variable incorrect");
// 2. Lookup Operational Paths
char look_key[16] = "TAG_BLOCK_1";
CacheLine* fetched = (CacheLine*)c_TreeMap_Get(&map, look_key);
EXPECT_EQ(fetched != NULL && fetched->physical_address == 0x00200000, C_TRUE, "Lookup fetched wrong data");
EXPECT_EQ(c_TreeMap_Contains(&map, look_key), C_TRUE, "Contains failed reporting true key matching status");
// 3. Balanced Node Deletion Check (Exercises structural transformations)
EXPECT_EQ(c_TreeMap_Remove(&map, look_key), C_ERR_OK, "Remove execution failed");
EXPECT_EQ(c_TreeMap_Contains(&map, look_key), C_FALSE, "Target key element still visible after remove sequence");
EXPECT_EQ(map.size, 2, "Size tracker failed downward matching reductions");
// Confirm neighboring branches stay preserved and functional post structural balance manipulation
char verify_key[16] = "TAG_BLOCK_2";
EXPECT_EQ(c_TreeMap_Contains(&map, verify_key), C_TRUE, "Sibling node dropped out of bounds during map deletion");
c_TreeMap_Destroy(&map);
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Unit Testing: c_TreeMap ===\n");
if (test_tree_map_lifecycle()) {
printf(" [PASS] TreeMap Balanced Insertion, Retrieval, and Complex Hibbard Deletion Lifecycles Verified.\n");
} else {
printf(" [FAIL] TreeMap Structure Component Validation Failure.\n");
}
return 0;
}