Search / Sort

This commit is contained in:
2026-08-30 22:24:45 +08:00
parent 3a4717a9d8
commit 0cb98557da
75 changed files with 7570 additions and 7 deletions
+72
View File
@@ -0,0 +1,72 @@
#include "c_MaxPQ.h"
#include "c_Test.h"
#include <stdlib.h>
#include <stdio.h>
// ==========================================
// 测试辅助:比对器
// ==========================================
static int max_pq_compare_ints(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;
}
TEST_CASE(test_c_MaxPQ_AllocatorFallbackFlow) {
c_MaxPQ_t fallback_pq;
// 🌟 核心投毒测试:最后一个参数故意硬性传入 NULL 分配器!
// 修复后:内部应当优雅放行、完美降级对接 c_DefaultAllocator 并开辟数据成功
c_err_t err = c_MaxPQ_Init(&fallback_pq, 2, sizeof(int), max_pq_compare_ints, NULL, NULL);
ASSERT_INT_EQ(C_ERR_OK, err);
int data[] = { 19, 88, 45, 6, 73 };
c_size_t num = sizeof(data) / sizeof(data[0]);
for (c_size_t i = 0; i < num; i++) {
ASSERT_TRUE(c_MaxPQ_Push(&fallback_pq, &data[i])==C_ERR_OK);
}
ASSERT_INT_EQ((int)num, (int)fallback_pq.size);
int previous_extracted = 999999;
int current_extracted = 0;
// 逐级提取验证大顶堆单调性
while (fallback_pq.size > 0) {
ASSERT_TRUE(c_MaxPQ_Peek(&fallback_pq, &current_extracted)==C_ERR_OK);
int pop_verify = 0;
ASSERT_TRUE(c_MaxPQ_Pop(&fallback_pq, &pop_verify)==C_ERR_OK);
ASSERT_INT_EQ(current_extracted, pop_verify);
ASSERT_TRUE(previous_extracted >= current_extracted);
previous_extracted = current_extracted;
}
ASSERT_INT_EQ(0, (int)fallback_pq.size);
// 彻底解构反初始化
c_MaxPQ_Destroy(&fallback_pq);
}
TEST_CASE(test_c_MaxPQ_ParamConstraints) {
c_MaxPQ_t local_pq;
// 验证关键参数如 self 或 elem_size 缺失时的拦截线依然坚固
ASSERT_INT_EQ(C_ERR_PARAM, c_MaxPQ_Init(NULL, 4, sizeof(int), max_pq_compare_ints, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_MaxPQ_Init(&local_pq, 4, 0, max_pq_compare_ints, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_MaxPQ_Init(&local_pq, 4, sizeof(int), NULL, NULL, NULL));
}
int main(int argc, char** argv){
TEST_START(C_MaxPQ_AllocatorFallback_TestSuite);
RUN_TEST(test_c_MaxPQ_AllocatorFallbackFlow);
RUN_TEST(test_c_MaxPQ_ParamConstraints);
TEST_REPORT();
return (g_test_registry.failed_count > 0 ? 1 : 0);
}