122 lines
3.9 KiB
C
122 lines
3.9 KiB
C
#include "c_SelectionSort.h"
|
||
#include "c_Test.h"
|
||
#include <stdlib.h>
|
||
#include <stdio.h>
|
||
|
||
// ==========================================
|
||
// 1. 测试用例伴生比对器与自定义结构体
|
||
// ==========================================
|
||
|
||
// 带 args 签名的标准整型比对器
|
||
static int sort_compare_ints_with_args(const void* a, const void* b, void* args) {
|
||
(void)args; // 暂不使用参数
|
||
int arg1 = *(const int*)a;
|
||
int arg2 = *(const int*)b;
|
||
if (arg1 < arg2) return -1;
|
||
if (arg1 > arg2) return 1;
|
||
return 0;
|
||
}
|
||
|
||
// 供复杂结构体测试使用的对象
|
||
typedef struct {
|
||
char key[4];
|
||
int score;
|
||
} Student_t;
|
||
|
||
// 针对结构体的带 args 自定义多级比对器
|
||
static int sort_compare_students(const void* a, const void* b, void* args) {
|
||
(void)args;
|
||
const Student_t* s1 = (const Student_t*)a;
|
||
const Student_t* s2 = (const Student_t*)b;
|
||
|
||
// 优先按照分数降序排序
|
||
if (s1->score != s2->score) {
|
||
return s2->score - s1->score;
|
||
}
|
||
// 分数相同时,按照名字字典序升序排列
|
||
return strcmp(s1->key, s2->key);
|
||
}
|
||
|
||
// ==========================================
|
||
// 2. 核心测试用例
|
||
// ==========================================
|
||
|
||
TEST_CASE(test_c_SelectionSort_IntArray) {
|
||
// 1. 准备一个乱序、带重复项的整型数组
|
||
int arr[] = { 29, 10, 14, 37, 14, 8, 20 };
|
||
c_size_t num = sizeof(arr) / sizeof(arr[0]);
|
||
|
||
// 运行带上下文比对的选择排序
|
||
c_SelectionSort(arr, num, sizeof(int), sort_compare_ints_with_args, NULL);
|
||
|
||
// 2. 验证全区间是否呈绝对严格递增排列
|
||
for (c_size_t i = 0; i < num - 1; i++) {
|
||
ASSERT_TRUE(arr[i] <= arr[i + 1]);
|
||
}
|
||
ASSERT_INT_EQ(8, arr[0]); // 全局最小值
|
||
ASSERT_INT_EQ(10, arr[1]);
|
||
ASSERT_INT_EQ(14, arr[2]); // 重复项
|
||
ASSERT_INT_EQ(14, arr[3]);
|
||
ASSERT_INT_EQ(20, arr[4]);
|
||
ASSERT_INT_EQ(29, arr[5]);
|
||
ASSERT_INT_EQ(37, arr[6]); // 全局最大值
|
||
}
|
||
|
||
TEST_CASE(test_c_SelectionSort_StructArray) {
|
||
// 3. 核心测试:针对复杂自定义结构体数组进行多级条件选择重排
|
||
Student_t students[] = {
|
||
{ "Bob", 85 },
|
||
{ "Amy", 95 },
|
||
{ "Doc", 85 },
|
||
{ "Eme", 70 }
|
||
};
|
||
c_size_t num = sizeof(students) / sizeof(students[0]);
|
||
|
||
c_SelectionSort(students, num, sizeof(Student_t), sort_compare_students, NULL);
|
||
|
||
// 4. 验证排序结果(预期结果:Amy/95 -> Bob/85 -> Doc/85 -> Eme/70)
|
||
ASSERT_INT_EQ(95, students[0].score);
|
||
ASSERT_TRUE(strcmp(students[0].key, "Amy") == 0);
|
||
|
||
ASSERT_INT_EQ(85, students[1].score);
|
||
ASSERT_TRUE(strcmp(students[1].key, "Bob") == 0); // 85分相同,B 在 D 前面
|
||
|
||
ASSERT_INT_EQ(85, students[2].score);
|
||
ASSERT_TRUE(strcmp(students[2].key, "Doc") == 0);
|
||
|
||
ASSERT_INT_EQ(70, students[3].score);
|
||
ASSERT_TRUE(strcmp(students[3].key, "Eme") == 0);
|
||
}
|
||
|
||
TEST_CASE(test_c_SelectionSort_EdgeAndStability) {
|
||
int ordered_arr[] = { 1, 2, 3 };
|
||
|
||
// 5. 压测完全有序的数组,验证内部优化机制(不产生任何不必要的物理交换)
|
||
c_SelectionSort(ordered_arr, 3, sizeof(int), sort_compare_ints_with_args, NULL);
|
||
ASSERT_INT_EQ(1, ordered_arr[0]);
|
||
ASSERT_INT_EQ(2, ordered_arr[1]);
|
||
ASSERT_INT_EQ(3, ordered_arr[2]);
|
||
|
||
// 6. 极端空边界安全拦截,验证不发生无符号整数下溢、死循环或段错误
|
||
int single_arr[] = { 888 };
|
||
c_SelectionSort(single_arr, 1, sizeof(int), sort_compare_ints_with_args, NULL);
|
||
c_SelectionSort(NULL, 0, sizeof(int), sort_compare_ints_with_args, NULL);
|
||
ASSERT_INT_EQ(888, single_arr[0]);
|
||
}
|
||
|
||
|
||
|
||
int main(int argc, char** argv){
|
||
TEST_START(Starting Unit Tests);
|
||
|
||
// 运行普通无环境要求的用例
|
||
RUN_TEST(test_c_SelectionSort_IntArray);
|
||
RUN_TEST(test_c_SelectionSort_StructArray);
|
||
RUN_TEST(test_c_SelectionSort_EdgeAndStability);
|
||
|
||
// 打印最终统计报告
|
||
TEST_REPORT();
|
||
|
||
RETURN_TEST_STATUS;
|
||
}
|