开始设计
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "c_MergeSort.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)
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Testing Datastructures & Comparison Helper Utilities
|
||||
// ----------------------------------------------------
|
||||
typedef struct {
|
||||
int primary_key; // Used for sorting
|
||||
int original_pos; // Used to test stable tracking properties
|
||||
char payload[64];
|
||||
} StableNode;
|
||||
|
||||
int compareStableNodes(const void* a, const void* b) {
|
||||
const StableNode* n1 = (const StableNode*)a;
|
||||
const StableNode* n2 = (const StableNode*)b;
|
||||
return (n1->primary_key > n2->primary_key) - (n1->primary_key < n2->primary_key);
|
||||
}
|
||||
|
||||
int compareIntegers(const void* a, const void* b) {
|
||||
return (*(int*)a - *(int*)b);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Unit Test Sub-routines
|
||||
// ----------------------------------------------------
|
||||
|
||||
// Test 1: Guard limits on Null pointers and zero size footprints
|
||||
bool test_merge_edge_cases(void) {
|
||||
int* empty_ptr = NULL;
|
||||
c_MergeSort(empty_ptr, 0, sizeof(int), compareIntegers); // Safe escape pipeline
|
||||
|
||||
int singular[] = { 99 };
|
||||
c_MergeSort(singular, 1, sizeof(int), compareIntegers);
|
||||
EXPECT_TRUE(singular[0] == 99, "Singular item modified unexpectedly");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Test 2: Core stability metrics (elements with matching keys preserve initial order)
|
||||
bool test_merge_sorting_stability(void) {
|
||||
StableNode items[] = {
|
||||
{ 10, 1, "First Ten" },
|
||||
{ 5, 2, "Five" },
|
||||
{ 10, 3, "Second Ten" },
|
||||
{ 1, 4, "One" },
|
||||
{ 10, 5, "Third Ten" }
|
||||
};
|
||||
c_size_t count = sizeof(items) / sizeof(items[0]);
|
||||
|
||||
c_MergeSort(items, count, sizeof(StableNode), compareStableNodes);
|
||||
|
||||
// Verify ordering sequence
|
||||
EXPECT_TRUE(items[0].primary_key == 1, "Lowest structural key error");
|
||||
EXPECT_TRUE(items[1].primary_key == 5, "Mid tier sorting position error");
|
||||
|
||||
// Stability checks: Primary keys at index 2, 3, and 4 are all '10'.
|
||||
// Their original_pos fields MUST step cleanly from 1 -> 3 -> 5.
|
||||
EXPECT_TRUE(items[2].original_pos == 1, "Stability Broken on duplicate subset element 1");
|
||||
EXPECT_TRUE(items[3].original_pos == 3, "Stability Broken on duplicate subset element 2");
|
||||
EXPECT_TRUE(items[4].original_pos == 5, "Stability Broken on duplicate subset element 3");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Test 3: Large scaling array to bypass the MERGE_STACK_LIMIT (512 Bytes)
|
||||
bool test_merge_heap_allocation(void) {
|
||||
// 20 items * 72 bytes per StableNode = 1440 Bytes (Exceeds stack buffer threshold)
|
||||
StableNode massive_dataset[20];
|
||||
c_size_t total = 20;
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
massive_dataset[i].primary_key = 20 - i; // Completely inverted structural setup
|
||||
massive_dataset[i].original_pos = i;
|
||||
}
|
||||
|
||||
c_MergeSort(massive_dataset, total, sizeof(StableNode), compareStableNodes);
|
||||
|
||||
// Scan through dataset confirming monotone strictly ascending continuity
|
||||
for (c_size_t i = 0; i < total - 1; i++) {
|
||||
EXPECT_TRUE(massive_dataset[i].primary_key <= massive_dataset[i+1].primary_key,
|
||||
"Continuous stream scaling breakdown under memory swap logic redirection");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Testing Execution Entry Driver
|
||||
// ----------------------------------------------------
|
||||
int main(void) {
|
||||
printf("=== Starting Framework Unit Testing: Top-Down Merge Sort ===\n");
|
||||
|
||||
if (test_merge_edge_cases()) printf(" [PASS] Test 1: Empty Array & Edge Boundary Safety\n");
|
||||
if (test_merge_sorting_stability()) printf(" [PASS] Test 2: Sorting Structural Stability Preservation\n");
|
||||
if (test_merge_heap_allocation()) printf(" [PASS] Test 3: Heap Buffer Alloc Escalation (>512B Testing)\n");
|
||||
|
||||
printf("=== System Verification Sequence Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user