175 lines
5.3 KiB
C
175 lines
5.3 KiB
C
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
|
|
|
/*=======Test Runner Used To Run Each Test Below=====*/
|
|
#define RUN_TEST(TestFunc, TestLineNum) \
|
|
{ \
|
|
Unity.CurrentTestName = #TestFunc; \
|
|
Unity.CurrentTestLineNumber = TestLineNum; \
|
|
Unity.NumberOfTests++; \
|
|
if (TEST_PROTECT()) { \
|
|
setUp(); \
|
|
TestFunc(); \
|
|
} \
|
|
if (TEST_PROTECT()) { \
|
|
tearDown(); \
|
|
} \
|
|
UnityConcludeTest(); \
|
|
}
|
|
|
|
/*=======Automagically Detected Files To Include=====*/
|
|
|
|
#include "unity.h"
|
|
#include <setjmp.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <regex.h>
|
|
|
|
/**
|
|
* @brief Test basic regex compile and match functions
|
|
*/
|
|
void test_regex_basic(void)
|
|
{
|
|
regex_t regex;
|
|
int ret;
|
|
|
|
// 测试基本编译功能
|
|
ret = regcomp(®ex, "hello", 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
// 测试完全匹配
|
|
ret = regexec(®ex, "hello", 0, NULL, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
// 测试不匹配
|
|
ret = regexec(®ex, "world", 0, NULL, 0);
|
|
TEST_ASSERT_EQUAL_INT(REG_NOMATCH, ret);
|
|
|
|
// 测试部分匹配
|
|
ret = regexec(®ex, "hello world", 0, NULL, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
// 释放资源
|
|
regfree(®ex);
|
|
}
|
|
|
|
/**
|
|
* @brief Test substring capturing with regex
|
|
*/
|
|
void test_regex_substring(void)
|
|
{
|
|
regex_t regex;
|
|
int ret;
|
|
regmatch_t pmatch[2];
|
|
|
|
// 编译带捕获组的正则表达式
|
|
ret = regcomp(®ex, "hello (world)", REG_EXTENDED);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
// 测试匹配和捕获组
|
|
ret = regexec(®ex, "hello world", 2, pmatch, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
TEST_ASSERT_EQUAL_INT(0, pmatch[0].rm_so);
|
|
TEST_ASSERT_EQUAL_INT(11, pmatch[0].rm_eo);
|
|
TEST_ASSERT_EQUAL_INT(6, pmatch[1].rm_so);
|
|
TEST_ASSERT_EQUAL_INT(11, pmatch[1].rm_eo);
|
|
|
|
// 释放资源
|
|
regfree(®ex);
|
|
}
|
|
|
|
/**
|
|
* @brief Test regex compilation options
|
|
*/
|
|
void test_regex_options(void)
|
|
{
|
|
regex_t regex;
|
|
int ret;
|
|
|
|
// 测试忽略大小写选项
|
|
ret = regcomp(®ex, "hello", REG_ICASE);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
ret = regexec(®ex, "HELLO", 0, NULL, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
regfree(®ex);
|
|
|
|
// 测试扩展正则表达式选项
|
|
ret = regcomp(®ex, "a+b", REG_EXTENDED);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
ret = regexec(®ex, "aaab", 0, NULL, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
regfree(®ex);
|
|
}
|
|
|
|
/**
|
|
* @brief Test regex error handling
|
|
*/
|
|
void test_regex_error(void)
|
|
{
|
|
regex_t regex;
|
|
int ret;
|
|
char errbuf[100];
|
|
|
|
// 测试无效的正则表达式 - 使用更安全的错误模式
|
|
ret = regcomp(®ex, "*a", REG_EXTENDED); // 重复符号在错误位置,更安全的错误模式
|
|
TEST_ASSERT_NOT_EQUAL(0, ret);
|
|
|
|
// 测试错误信息获取
|
|
size_t len = regerror(ret, ®ex, errbuf, sizeof(errbuf));
|
|
TEST_ASSERT_GREATER_THAN(0, len);
|
|
TEST_ASSERT_NOT_EQUAL(0, strlen(errbuf));
|
|
}
|
|
|
|
/**
|
|
* @brief Test complex regex patterns
|
|
*/
|
|
void test_regex_complex(void)
|
|
{
|
|
regex_t regex;
|
|
int ret;
|
|
regmatch_t pmatch[3];
|
|
|
|
// 编译一个更复杂的正则表达式
|
|
ret = regcomp(®ex, "([0-9]+)-([a-z]+)", REG_EXTENDED);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
|
|
// 测试匹配
|
|
ret = regexec(®ex, "123-abc", 3, pmatch, 0);
|
|
TEST_ASSERT_EQUAL_INT(0, ret);
|
|
TEST_ASSERT_EQUAL_INT(0, pmatch[0].rm_so);
|
|
TEST_ASSERT_EQUAL_INT(7, pmatch[0].rm_eo);
|
|
TEST_ASSERT_EQUAL_INT(0, pmatch[1].rm_so);
|
|
TEST_ASSERT_EQUAL_INT(3, pmatch[1].rm_eo);
|
|
TEST_ASSERT_EQUAL_INT(4, pmatch[2].rm_so);
|
|
TEST_ASSERT_EQUAL_INT(7, pmatch[2].rm_eo);
|
|
|
|
regfree(®ex);
|
|
}
|
|
|
|
void setUp(void) {
|
|
// 测试设置函数
|
|
}
|
|
|
|
void tearDown(void) {
|
|
// 测试清理函数
|
|
}
|
|
|
|
/*=======MAIN=====*/
|
|
int main(void)
|
|
{
|
|
UnityBegin("test/components/exclib/test_main.c");
|
|
|
|
RUN_TEST(test_regex_basic, __LINE__);
|
|
RUN_TEST(test_regex_substring, __LINE__);
|
|
RUN_TEST(test_regex_options, __LINE__);
|
|
RUN_TEST(test_regex_error, __LINE__);
|
|
RUN_TEST(test_regex_complex, __LINE__);
|
|
|
|
return (UnityEnd());
|
|
}
|