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

64 lines
2.3 KiB
C

#include "c_BoyerMoore.h"
#include "c_Test.h"
#include <stdlib.h>
#include <stdio.h>
TEST_CASE(test_c_BoyerMoore_Full) {
c_BoyerMoore_t bm;
// 初始化子串模式匹配器,测试默认 Fallback 降级分配器
c_err_t err = c_BoyerMoore_InitStr(&bm, "EXAMPLE", NULL);
ASSERT_INT_EQ(C_ERR_OK, err);
// 1. 基础常规命中断言
const char* text_normal = "HERE IS A SIMPLE EXAMPLE STR";
c_size_t match_index = 0;
err = c_BoyerMoore_SearchStr(&bm, text_normal, &match_index);
ASSERT_INT_EQ(C_ERR_OK, err);
ASSERT_INT_EQ(17, (int)match_index); // 精确断言子串在主文本中的绝对起始插槽位置为 17
// 2. 🌟【绝杀功能压测】:好后缀与坏字符最大化跨越式跳跃
// 故意构造一个包含大量单调连续字符冲突、传统 KMP 极易发生高频无用回溯的排毒数据
c_BoyerMoore_t bm_toxic;
c_BoyerMoore_InitStr(&bm_toxic, "ANPANMAN", &c_DefaultAllocator);
const char* text_toxic = "ANPANPANPANANPANMAN_CORE_SYS"; // 子串深埋在中后段
match_index = 0;
err = c_BoyerMoore_SearchStr(&bm_toxic, text_toxic, &match_index);
ASSERT_INT_EQ(C_ERR_OK, err);
ASSERT_INT_EQ(11, (int)match_index); // 级联大跨度滑窗跳转定位成功!
// 3. 边界拦截未命中核验
const char* text_fake = "ANPAN_ANPAN_APN_SYSTEM";
ASSERT_INT_EQ(C_ERR_NOTFOUND, c_BoyerMoore_SearchStr(&bm_toxic, text_fake, &match_index));
c_BoyerMoore_Destroy(&bm);
c_BoyerMoore_Destroy(&bm_toxic);
}
TEST_CASE(test_c_BoyerMoore_Toxicity_Defenses) {
c_BoyerMoore_t local_bm;
c_BoyerMoore_InitStr(&local_bm, "A", NULL);
c_size_t idx = 0;
// 4. 验证各类参数非法边界强拦截
ASSERT_INT_EQ(C_ERR_PARAM, c_BoyerMoore_InitStr(NULL, NULL, NULL));
ASSERT_INT_EQ(C_ERR_PARAM, c_BoyerMoore_SearchStr(NULL, "text", &idx));
ASSERT_INT_EQ(C_ERR_PARAM, c_BoyerMoore_SearchStr(&local_bm, NULL, &idx));
c_BoyerMoore_Destroy(&local_bm);
}
// ==========================================
// 5. 主集成入口
// ==========================================
int main(void) {
TEST_START(C_BoyerMoore_HeuristicSearch_TestSuite);
RUN_TEST(test_c_BoyerMoore_Full);
RUN_TEST(test_c_BoyerMoore_Toxicity_Defenses);
TEST_REPORT();
return (g_test_registry.failed_count > 0 ? 1 : 0);
}