Files
cAI/cKit/Sort/c_IndexMaxPQ.t.c
T
2026-08-10 01:21:15 +08:00

97 lines
4.6 KiB
C

#include "c_IndexMaxPQ.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 struct data block payload
typedef struct {
int task_id;
float score; // Primary tracking element for MaxPQ (higher level extracted first)
} TelemetryTask;
int compareTelemetryTasks(const void* a, const void* b) {
const TelemetryTask* t1 = (const TelemetryTask*)a;
const TelemetryTask* t2 = (const TelemetryTask*)b;
if (t1->score < t2->score) return -1;
if (t1->score > t2->score) return 1;
return 0;
}
c_bool_t test_framework_index_max_pq(void) {
c_IndexMaxPQ_t ipq;
// 1. Parameter Enforcement Validation
EXPECT_EQ(c_IndexMaxPQ_Init(NULL, 10, sizeof(TelemetryTask), compareTelemetryTasks), C_ERR_PARAM, "NULL pointer checking missed");
EXPECT_EQ(c_IndexMaxPQ_Init(&ipq, 0, sizeof(TelemetryTask), compareTelemetryTasks), C_ERR_PARAM, "Zero capacity checking missed");
// Allocate space for 4 telemetry streams (External IDs 0 to 3)
EXPECT_EQ(c_IndexMaxPQ_Init(&ipq, 4, sizeof(TelemetryTask), compareTelemetryTasks), C_ERR_OK, "PQ Init failed");
TelemetryTask t0 = { 5001, 12.5f };
TelemetryTask t1 = { 5002, 98.4f }; // Initial Maximum Element
TelemetryTask t2 = { 5003, 45.2f };
// 2. Data Insertion & State Tracking
EXPECT_EQ(c_IndexMaxPQ_Push(&ipq, 0, &t0), C_ERR_OK, "Push t0 failed");
EXPECT_EQ(c_IndexMaxPQ_Push(&ipq, 1, &t1), C_ERR_OK, "Push t1 failed");
EXPECT_EQ(c_IndexMaxPQ_Push(&ipq, 2, &t2), C_ERR_OK, "Push t2 failed");
EXPECT_EQ(c_IndexMaxPQ_Push(&ipq, 1, &t1), C_ERR_ALREADY_EXISTS, "Duplicate insert guard missed");
EXPECT_EQ(c_IndexMaxPQ_Contains(&ipq, 1), C_TRUE, "Contains failed reporting registered elements");
EXPECT_EQ(c_IndexMaxPQ_Contains(&ipq, 3), C_FALSE, "Contains reported tracking on unused indices");
// 3. Dynamic Key Upgrades (ChangeKey runtime shifts)
// Modify Telemetry Task 0 (t0, external ID: 0) from 12.5f to 105.7f.
// This should instantly shift t0 to the root position of the Max-Heap.
TelemetryTask t0_boosted = { 5001, 105.7f };
EXPECT_EQ(c_IndexMaxPQ_ChangeKey(&ipq, 5, &t0_boosted), C_ERR_PARAM, "OOB Index verification checks missed");
EXPECT_EQ(c_IndexMaxPQ_ChangeKey(&ipq, 3, &t0_boosted), C_ERR_NOT_FOUND, "Unregistered Index modification check missed");
EXPECT_EQ(c_IndexMaxPQ_ChangeKey(&ipq, 0, &t0_boosted), C_ERR_OK, "Valid target key adjustment failed");
// 4. Verification Lookups & Sequential Extraction Lifecycle
c_size_t extracted_ext_id = 999;
TelemetryTask out_buffer;
// Peek Check: Root must now map to External ID 0 (score: 105.7f)
TelemetryTask* peek_ptr = (TelemetryTask*)c_IndexMaxPQ_Peek(&ipq, &extracted_ext_id);
EXPECT_EQ(extracted_ext_id, 0, "Peek resolved incorrect key slot context");
EXPECT_EQ(peek_ptr->score == 105.7f, C_TRUE, "Peek structural memory value extraction wrong");
// Pop 1: Yields Task 0 (ID: 0, Score: 105.7f)
EXPECT_EQ(c_IndexMaxPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_OK, "Pop execution step 1 failed");
EXPECT_EQ(extracted_ext_id, 0, "Re-balancing priority lookup sequence wrong at Pop 1");
// Pop 2: Yields Task 1 (ID: 1, Score: 98.4f)
EXPECT_EQ(c_IndexMaxPQ_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 Task 2 (ID: 2, Score: 45.2f)
EXPECT_EQ(c_IndexMaxPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_OK, "Pop execution step 3 failed");
EXPECT_EQ(extracted_ext_id, 2, "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_IndexMaxPQ_Pop(&ipq, &extracted_ext_id, &out_buffer), C_ERR_EMPTY, "Empty pipeline crash missing exception flag hooks");
c_IndexMaxPQ_Destroy(&ipq);
return C_TRUE;
}
int main(void) {
printf("=== Starting Custom Framework Profiling: c_IndexMaxPQ ===\n");
if (test_framework_index_max_pq()) {
printf(" [PASS] All Indexed Max-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;
}