Files
cKit/String/c_RabinKarp.t.c
T
2026-08-31 22:49:42 +08:00

66 lines
2.2 KiB
C

#include "c_RabinKarp.h"
#include "c_Test.h"
#include <stdlib.h>
#include <stdio.h>
TEST_CASE(test_c_RabinKarp_Full) {
c_RabinKarp_t rk;
// 初始化子串模式匹配器,测试默认 Fallback 降级分配器
c_err_t err = c_RabinKarp_Init(&rk, "ABRACADABRA", NULL);
ASSERT_INT_EQ(C_ERR_OK, err);
// 1. 验证常规居中匹配
const char* text_normal = "IN A BIG ABRACADABRA TEXT STREAM";
c_size_t match_index = 0;
err = c_RabinKarp_Search(&rk, text_normal, &match_index);
ASSERT_INT_EQ(C_ERR_OK, err);
ASSERT_INT_EQ(9, (int)match_index); // 精确验证子串在主文本中的绝对起始物理插槽下标为 9
// 2. 🌟【拉斯维加斯深度压测】:
// 故意构造一个开头、中段充斥着大量高度相似哈希冲突干扰项、主子串深埋末尾的数据流
c_RabinKarp_t rk_toxic;
c_RabinKarp_Init(&rk_toxic, "C_CORE", &c_DefaultAllocator);
const char* text_toxic = "C_C0RE_A_C_C0RE_B_C_CORE_SYS";
match_index = 0;
err = c_RabinKarp_Search(&rk_toxic, text_toxic, &match_index);
ASSERT_INT_EQ(C_ERR_OK, err);
ASSERT_INT_EQ(18, (int)match_index); // 指纹滑动状态机过滤掉高全等欺骗,成功精确定位!
// 3. 边界未命中核验
const char* text_fake = "C_C0RE_A_C_C0RE_B_SYSTEM_ERR";
ASSERT_INT_EQ(C_ERR_NOTFOUND, c_RabinKarp_Search(&rk_toxic, text_fake, &match_index));
c_RabinKarp_Destroy(&rk);
c_RabinKarp_Destroy(&rk_toxic);
}
TEST_CASE(test_c_RabinKarp_Toxicity_Defenses) {
c_RabinKarp_t local_rk;
c_RabinKarp_Init(&local_rk, "A", NULL);
c_size_t idx = 0;
// 4. 验证入参非法强拦截线
ASSERT_INT_EQ(C_ERR_PARAM, c_RabinKarp_Init(NULL, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_RabinKarp_Search(NULL, "text", &idx));
ASSERT_INT_EQ(C_ERR_PARAM, c_RabinKarp_Search(&local_rk, NULL, &idx));
c_RabinKarp_Destroy(&local_rk);
}
// ==========================================
// 5. 主集成入口
// ==========================================
int main(void) {
TEST_START(C_RabinKarp_RollingHash_TestSuite);
RUN_TEST(test_c_RabinKarp_Full);
RUN_TEST(test_c_RabinKarp_Toxicity_Defenses);
TEST_REPORT();
return (g_test_registry.failed_count > 0 ? 1 : 0);
}