Files

67 lines
2.7 KiB
C
Raw Permalink Normal View History

2026-08-31 22:49:42 +08:00
#include "c_LSD.h"
#include "c_Test.h"
#include <stdlib.h>
#include <stdio.h>
static uint8_t int32_lsd_extractor(const void* elem, c_size_t d, void* args) {
(void)args;
// 强制转换为无符号 32 位整型值进行物理提取
uint32_t val = *(const uint32_t*)elem;
// 🌟 核心:通过按位右移位位移 (d * 8) 并截断为 8 位,完美抽取对应第 d 个无符号字节
return (uint8_t)((val >> (d << 3)) & 0xFF);
}
TEST_CASE(test_c_LSD_RadixSort_IntLinearFlow) {
// 准备一组带有高频极值碰撞、乱序排布的 32 位整型数组
uint32_t arr[] = { 54321, 12, 987654, 54321, 333, 8, 12, 1000000 };
c_size_t num = sizeof(arr) / sizeof(arr[0]);
// 调用 LSD 基数排序:单体大小为 sizeof(uint32_t)32位整型总定长密钥宽度为 4 字节
// 测试将 allocator 传入 NULL,检验内部默认单例 c_DefaultAllocator 的自适应降级 Fallback 承接力
c_err_t err = c_LSD_RadixSort(arr, num, sizeof(uint32_t), 4, int32_lsd_extractor, NULL, NULL);
ASSERT_INT_EQ(C_ERR_OK, err);
// 验证全区间无损单调非减性排列
for (c_size_t i = 0; i < num - 1; i++) {
if (arr[i] > arr[i + 1]) {
// 利用高精确断言对排序崩溃点实施快速定位亮红灯
ASSERT_INT_EQ(arr[i + 1], arr[i]);
return;
}
}
// 首、尾、重复值插槽下标精准校验
ASSERT_INT_EQ(8, (int)arr[0]);
ASSERT_INT_EQ(12, (int)arr[1]);
ASSERT_INT_EQ(12, (int)arr[2]); // 稳定去重项紧凑排列
ASSERT_INT_EQ(54321, (int)arr[4]);
ASSERT_INT_EQ(1000000, (int)arr[num - 1]); // 尾部必须合拢为绝对最大值
}
TEST_CASE(test_c_LSD_RadixSort_ParamConstraints) {
uint32_t single[] = { 999 };
// 验证各类入参毒参数及极端单元素边界的前置拦截状态码返回值
ASSERT_INT_EQ(C_ERR_PARAM, c_LSD_RadixSort(NULL, 10, sizeof(uint32_t), 4, int32_lsd_extractor, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_LSD_RadixSort(single, 1, 0, 4, int32_lsd_extractor, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_LSD_RadixSort(single, 1, sizeof(uint32_t), 4, NULL, NULL, NULL));
// 单体拦截线:单元素无需排序直接放行返回 C_ERR_OK
ASSERT_INT_EQ(C_ERR_OK, c_LSD_RadixSort(single, 1, sizeof(uint32_t), 4, int32_lsd_extractor, NULL, NULL));
}
// ==========================================
// 5. 主集成运行入口点
// ==========================================
int main(void) {
TEST_START(C_LSD_RadixSort_PolymorphicLinear_TestSuite);
RUN_TEST(test_c_LSD_RadixSort_IntLinearFlow);
RUN_TEST(test_c_LSD_RadixSort_ParamConstraints);
TEST_REPORT();
return (g_test_registry.failed_count > 0 ? 1 : 0);
}