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

74 lines
2.7 KiB
C
Raw Normal View History

2026-08-10 01:21:15 +08:00
#include "c_MergeSortBU.h"
#include <stdlib.h>
#include <stdio.h>
#define EXPECT_TRUE(cond, msg) \
do { \
if (!(cond)) { printf(" [X] Failed Assertion: %s\n", msg); return false; } \
} while(0)
// Struct declaration enforcing alignment and tracking order metadata
typedef struct {
int key;
int initial_order;
char metadata[16];
} TestNode;
int compareTestNodes(const void* a, const void* b) {
const TestNode* n1 = (const TestNode*)a;
const TestNode* n2 = (const TestNode*)b;
return (n1->key > n2->key) - (n1->key < n2->key);
}
// Test 1: Sorting arrays with sizes that are not powers of two (e.g., N = 7)
bool test_bu_non_power_of_two(void) {
int datasets[] = { 85, 24, 63, 45, 17, 31, 96 };
c_size_t total = sizeof(datasets) / sizeof(datasets[0]);
// Simple integer comparison handler
int cmp_int(const void* a, const void* b) { return (*(int*)a - *(int*)b); }
c_MergeSortBottomUp(datasets, total, sizeof(int), cmp_int);
for (c_size_t i = 0; i < total - 1; i++) {
EXPECT_TRUE(datasets[i] <= datasets[i+1], "Irregular block boundary sorted sequence tracking crash");
}
return true;
}
// Test 2: Multi-field structural stability checkpoint (N = 6)
bool test_bu_stability(void) {
TestNode records[] = {
{ 40, 0, "Alpha" },
{ 20, 1, "Beta" },
{ 40, 2, "Gamma" },
{ 10, 3, "Delta" },
{ 40, 4, "Zeta" },
{ 20, 5, "Eta" }
};
c_size_t count = sizeof(records) / sizeof(records[0]);
c_MergeSortBottomUp(records, count, sizeof(TestNode), compareTestNodes);
// Assert key orders are strictly sorted
EXPECT_TRUE(records[0].key == 10, "Base index misplacement");
EXPECT_TRUE(records[1].key == 20 && records[2].key == 20, "Middle index sort tracking failed");
EXPECT_TRUE(records[3].key == 40 && records[4].key == 40 && records[5].key == 40, "Tail elements misaligned");
// Assert stability: matching records must keep their initial relative positions
EXPECT_TRUE(records[1].initial_order == 1 && records[2].initial_order == 5, "Stability broken on key value 20");
EXPECT_TRUE(records[3].initial_order == 0 && records[4].initial_order == 2 && records[5].initial_order == 4,
"Stability broken on key value 40");
return true;
}
// Test Driving Runner Subroutine
int main(void) {
printf("=== Starting Framework Unit Testing: Bottom-Up Merge Sort ===\n");
if (test_bu_non_power_of_two()) printf(" [PASS] Test 1: Non-Power-of-Two Odd Dataset Counts Handles Securely\n");
if (test_bu_stability()) printf(" [PASS] Test 2: Iterative Stable Record Processing Validated\n");
printf("=== System Verification Sequence Completed ===\n");
return 0;
}