Files
cAI/cKit/Sort/c_IndexMinPQ.t.c
T

110 lines
4.8 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_IndexMinPQ.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Unit test validation driver macro
#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 data block node mapping path costs
typedef struct {
float node_weight;
int edge_id;
} PathNode;
// Framework Comparator for PathNode structures (Min-Heap optimization logic)
int comparePathCosts(const void* a, const void* b) {
const PathNode* p1 = (const PathNode*)a;
const PathNode* p2 = (const PathNode*)b;
if (p1->node_weight < p2->node_weight) return -1;
if (p1->node_weight > p2->node_weight) return 1;
return 0;
}
/**
* Functional Test Profile validating Param checking, Duplicate Guards,
* and ChangeKey structural re-balancing routines.
*/
c_bool_t test_framework_index_min_pq(void) {
c_IndexMinPQ_t ipq;
// 1. Parameter Enforcement Validation Boundary Checks
EXPECT_EQ(c_IndexMinPQ_Init(NULL, 10, sizeof(PathNode), comparePathCosts), C_ERR_PARAM, "NULL pointer checking missed");
EXPECT_EQ(c_IndexMinPQ_Init(&ipq, 0, sizeof(PathNode), comparePathCosts), C_ERR_PARAM, "Zero capacity checking missed");
// Allocate space for up to 4 concurrent vertices (External IDs: 0 to 3)
EXPECT_EQ(c_IndexMinPQ_Init(&ipq, 4, sizeof(PathNode), comparePathCosts), C_ERR_OK, "PQ Init failed");
PathNode v0 = { 35.6f, 100 };
PathNode v1 = { 14.2f, 101 };
PathNode v2 = { 88.1f, 102 };
// 2. State Insert Tracking
EXPECT_EQ(c_IndexMinPQ_Push(&ipq, 0, &v0), C_ERR_OK, "Push v0 failed");
EXPECT_EQ(c_IndexMinPQ_Push(&ipq, 1, &v1), C_ERR_OK, "Push v1 failed");
EXPECT_EQ(c_IndexMinPQ_Push(&ipq, 2, &v2), C_ERR_OK, "Push v2 failed");
// Check duplication guard safety logic
EXPECT_EQ(c_IndexMinPQ_Push(&ipq, 1, &v1), C_ERR_ALREADY_EXISTS, "Duplicate insert guard missed");
EXPECT_EQ(c_IndexMinPQ_Contains(&ipq, 1), C_TRUE, "Contains failed reporting registered elements");
EXPECT_EQ(c_IndexMinPQ_Contains(&ipq, 3), C_FALSE, "Contains reported tracking on unused indices");
// 3. Dynamic Key Upgrades (ChangeKey runtime shifts)
// Vertex 2 (v2) currently has a cost of 88.1f. Let's decrease it to 5.2f.
// This action must shift it straight up to the head of the Min-Priority Queue.
PathNode v2_optimized = { 5.2f, 102 };
EXPECT_EQ(c_IndexMinPQ_ChangeKey(&ipq, 5, &v2_optimized), C_ERR_PARAM, "OOB Index verification checks missed");
EXPECT_EQ(c_IndexMinPQ_ChangeKey(&ipq, 3, &v2_optimized), C_ERR_NOT_FOUND, "Unregistered Index modification check missed");
EXPECT_EQ(c_IndexMinPQ_ChangeKey(&ipq, 2, &v2_optimized), C_ERR_OK, "Valid target key adjustment failed");
// 4. Verification Lookups & Sequential Extraction Lifecycle
c_size_t extracted_ext_id = 999;
PathNode out_buffer;
// Peek Check: Root must now resolve to External ID 2 (cost 5.2f)
PathNode* peek_ptr = (PathNode*)c_IndexMinPQ_Peek(&ipq, &extracted_ext_id);
EXPECT_EQ(extracted_ext_id, 2, "Peek resolved incorrect key slot context");
EXPECT_EQ(peek_ptr->node_weight == 5.2f, C_TRUE, "Peek structural memory value extraction wrong");
// Pop 1: Yields Vertex 2 (ID: 2, Weight: 5.2f)
EXPECT_EQ(c_IndexMinPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_OK, "Pop execution step 1 failed");
EXPECT_EQ(extracted_ext_id, 2, "Re-balancing priority lookup sequence wrong at Pop 1");
// Pop 2: Yields Vertex 1 (ID: 1, Weight: 14.2f)
EXPECT_EQ(c_IndexMinPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_OK, "Pop execution step 2 failed");
EXPECT_EQ(extracted_ext_id, 1, "Re-balancing priority lookup sequence wrong at Pop 2");
// Pop 3: Yields Vertex 0 (ID: 0, Weight: 35.6f)
EXPECT_EQ(c_IndexMinPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_OK, "Pop execution step 3 failed");
EXPECT_EQ(extracted_ext_id, 0, "Re-balancing priority lookup sequence wrong at Pop 3");
// 5. Empty Boundary Assert Cleanups
EXPECT_EQ(ipq.size, 0, "Tracking metric registers missed reset zero flags");
EXPECT_EQ(c_IndexMinPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_EMPTY, "Empty pipeline crash missing exception flag hooks");
c_IndexMinPQ_Destroy(&ipq);
return C_TRUE;
}
int main(void) {
printf("=== Starting Custom Framework Profiling: c_IndexMinPQ ===\n");
if (test_framework_index_min_pq()) {
printf(" [PASS] All Indexed Min-Priority Queue Architectural Requirements Verified Successfully.\n");
} else {
printf(" [FAIL] Architectural Verification Pipeline Failure Detected.\n");
}
printf("=== All Indexed Priority Queue Tests Completed ===\n");
return 0;
}