开始设计
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#include "c_QuickSort.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
#define EXPECT_TRUE(cond, msg) \
|
||||
do { \
|
||||
if (!(cond)) { printf(" [X] Failed Assertion: %s\n", msg); return false; } \
|
||||
} while(0)
|
||||
|
||||
typedef struct {
|
||||
int id;
|
||||
double performance_index;
|
||||
} TaskMetric;
|
||||
|
||||
int compareTasks(const void* a, const void* b) {
|
||||
const TaskMetric* t1 = (const TaskMetric*)a;
|
||||
const TaskMetric* t2 = (const TaskMetric*)b;
|
||||
if (t1->performance_index < t2->performance_index) return -1;
|
||||
if (t1->performance_index > t2->performance_index) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int compareInts(const void* a, const void* b) {
|
||||
return (*(int*)a - *(int*)b);
|
||||
}
|
||||
|
||||
// Test 1: Sorting completely pre-sorted lists (Guards against worst-case naive pivot selections)
|
||||
bool test_qsort_presorted(void) {
|
||||
int ordered[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
|
||||
c_size_t total = sizeof(ordered) / sizeof(ordered);
|
||||
|
||||
c_QuickSort(ordered, total, sizeof(int), compareInts);
|
||||
|
||||
for (c_size_t i = 0; i < total - 1; i++) {
|
||||
EXPECT_TRUE(ordered[i] <= ordered[i+1], "Pre-sorted sequence tracking error occurred");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Test 2: Dense duplicate key distribution array
|
||||
bool test_qsort_duplicates(void) {
|
||||
TaskMetric metrics[] = {
|
||||
{101, 9.5}, {102, 5.0}, {103, 9.5}, {104, 2.1}, {105, 5.0},
|
||||
{106, 9.5}, {107, 2.1}, {108, 9.5}, {109, 5.0}, {110, 2.1},
|
||||
{111, 5.0}, {112, 9.5}
|
||||
};
|
||||
c_size_t count = sizeof(metrics) / sizeof(metrics);
|
||||
|
||||
c_QuickSort(metrics, count, sizeof(TaskMetric), compareTasks);
|
||||
|
||||
for (c_size_t i = 0; i < count - 1; i++) {
|
||||
EXPECT_TRUE(metrics[i].performance_index <= metrics[i+1].performance_index,
|
||||
"Duplicate float element tracking collapsed under Hoare pointers");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Testing Execution Entry Driver
|
||||
int main(void) {
|
||||
printf("=== Starting Framework Unit Testing: Quicksort ===\n");
|
||||
|
||||
if (test_qsort_presorted()) printf(" [PASS] Test 1: Pre-Sorted Array Pivot Anti-Degradation Passed\n");
|
||||
if (test_qsort_duplicates()) printf(" [PASS] Test 2: Highly Repetitive Element Distribution Sorted Successfully\n");
|
||||
|
||||
printf("=== System Verification Sequence Completed ===\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user