📄 test.c
字号:
#include <stdlib.h>#include <string.h>#include "pg_wchar.h"#include "regex.h"void usage(char * program){ printf("Usage of %s :\n",program); printf("\t%s regular_expression match_string\n",program);}void test_result(const char * test,const char * result ){#ifdef DEBUG if(result == NULL) fprintf(stderr,"result is NULL\n"); else { if(strcmp(test,result)) { printf("XXXXXXXXXXXXXXXXXXXX ERROR XXXXXXXXXXXXXXXXXXXX!\n");exit(1);} }#endif}void print_error(){#ifdef DEBUG fprintf(stderr,"***************NOT match********************\n");#endif}void test_match_function();void test_match_icase_function();void test_match_mem_leaking();void test_similar_to_function();void test_similar_to_mem_leaking();void test_substr_function();void test_substr_mem_leaking();void test_replace_function();void test_replace_mem_leaking();void test_function(){ test_match_function(); test_match_icase_function(); test_similar_to_function(); test_substr_function(); test_replace_function();}void test_mem_leaking(){ int i = 0; printf("test replace mem leaking\n"); for(;;i++) { test_function(); if(i%10000 == 0) printf("times:%d\n",i); } }int main(int argc,char ** argv){ if(argc >= 3) { int result = pg_regex_match_icase(argv[1],argv[2]); printf("The result is: %d\n",result); } else if(argc >1) { usage(argv[0]); return 1; } else { test_function();// test_mem_leaking(); } return 0;}void test_match_icase_function(){#ifdef DEBUG printf("---------------------pg_regex_match_icase--------------------\n");#endif if(!pg_regex_match_icase("ASDF","***:^(?:ASDF)$")) /*测试特殊的正则表达式*/ print_error(); if(!pg_regex_match_icase("hello world","world")) print_error(); if(!pg_regex_match_icase("HELLO WORLD","world")) /*测试有大小写不同的情况*/ print_error(); if(!pg_regex_match_icase("中国HELLO WORLD","world")) /*测试有大小写不同的情况*/ print_error(); if(!pg_regex_match_icase("中国HELLO WORLD","中国")) /*测试有大小写不同的情况*/ print_error();}void test_match_function(){#ifdef DEBUG printf("-------------------test pg_regex_match--------------------\n");#endif if(!pg_regex_match("ASDF","***:^(?:ASDF)$")) /*测试特殊正则表达式 ***:^(?: ... )$ ,***:表示用ARE匹配*/ print_error(); if(!pg_regex_match("hello ASDF world","(?:ASDF)")) /*测试特殊正则表达式 (?: ... ) */ print_error(); if(!pg_regex_match("hello asdf world","***:(?:ASDF)")) /*测试特殊正则表达式 (?: ... ) */ print_error(); if(!pg_regex_match("ASDF",NULL)) /*测试测试正则表达式为NULL ,NULL和任何的字符串都匹配*/ print_error(); if(!pg_regex_match(NULL,"asdf")) /*测试数据为NULL,除非正则表达式为NULL,否则不匹配 */ print_error(); if(!pg_regex_match(NULL,NULL)) /*测试都为NULL,匹配 */ print_error(); if(!pg_regex_match("hello 中国 world","中国")) print_error(); if(!pg_regex_match("hello中国world","o中国w")) print_error(); if(!pg_regex_match("hello world","")) /*空字符也能匹配*/ print_error(); if(!pg_regex_match("hello world","iasdf")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match("hellolo world","(lo){2}")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match("hellololo world","(lo){2,3}")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match("hellololo world","(lo)?")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match("123456","\\d+")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match(" ","\\s+")) /*测试没有匹配的情况*/ print_error(); if(!pg_regex_match("abc _ 123","\\w+")) /*测试没有匹配的情况*/ print_error();}void test_match_mem_leaking(){ int i = 0; printf("test replace mem leaking\n"); for(;;i++) { test_match_function(); test_match_icase_function(); if(i%10000 == 0) printf("times:%d\n",i); }}void test_similar_to_function(){ #ifdef DEBUG printf("-----------------------test test_similar_to_function----------------------\n");#endif if(! similar_to("hello world","%",NULL)) //测试% print_error(); if(! similar_to("hello world","hello%",NULL)) //% print_error(); if(! similar_to("hello world","___________",NULL)) //测试 _ print_error(); if(! similar_to("hello world","hello",NULL)) //测试 部分匹配,结果返回false print_error(); if(! similar_to("hello world",NULL,NULL)) //正则表达式为NULL,结果返回false print_error(); if(! similar_to(NULL,"hello",NULL)) //数据为NULL,结果返回false print_error(); if(! similar_to(NULL,NULL,NULL)) //全部为NULL,结果返回false print_error(); if(! similar_to("abc","%(a|b)%",NULL)) //测试 代分组子表达式的情况, print_error(); if(! similar_to("\\bac","abac","a")) //测试 代分组子表达式的情况, print_error(); if(! similar_to("\\bc","abc","")) //测试 代分组子表达式的情况, print_error();}void test_similar_to_mem_leaking(){ int i = 0; printf("test similar to mem leaking\n"); for(;;i++) { test_similar_to_function(); if(i%10000 == 0) printf("times:%d\n",i); }}void test_replace_function(){ char * temp;#ifdef DEBUG printf("-----------------------text_regex_replace-------------------------\n");#endif temp = text_regex_replace("hel和loworld","e...",NULL); /*替换字符是NULL 就返回删掉正则表达式的字符串*/ test_result(temp,"howorld"); free(temp); temp = text_regex_replace("hel和loworld",NULL,NULL); /*正则表达式是NULL 就返回原始字符串*/ test_result(temp,"hel和loworld"); free(temp); temp = text_regex_replace(NULL,NULL,NULL); /*原始字符是NULL 就返回NULL*/ test_result(temp,NULL); free(temp); temp = text_regex_replace("hel和loworld","e...",""); /*替换字符是"" 就返回删掉正则表达式的字符串*/ test_result(temp,"howorld"); free(temp); temp = text_regex_replace("hel和loworld","",""); /*正则表达式是"" 就返回原始字符串*/ test_result(temp,"hel和loworld"); free(temp); temp = text_regex_replace("","",""); /*原始字符是"" 就返回""*/ test_result(temp,""); free(temp); temp = text_regex_replace("hlolo hello world","(lo)+","xx"); /*测试带子表达式的替换,符合最长原则*/ test_result(temp,"hxx helxx world"); free(temp); temp = text_regex_replace("hel\tloworld","e...","o我o"); /*带中文和原始字符串含有转义字符*/ test_result(temp,"ho我ooworld"); free(temp); temp = text_regex_replace("hel和loworld","e...","o\\no");/*替换字符串含有转义字符*/ test_result(temp,"ho\\nooworld"); free(temp); temp = text_regex_replace("h你好lo he你中国o 世界","你...","o我o"); /*原始字符,正则表达式,替换字符都含有中文*/ test_result(temp,"ho我o heo我o 世界"); free(temp); temp = text_regex_replace("he你好o he中国o 世界","e...","o我o"); test_result(temp,"ho我o ho我o 世界"); free(temp); temp = text_regex_replace("hello hello world","e...","o我o"); /*替换两个部分*/ test_result(temp,"ho我o ho我o world"); free(temp); temp = text_regex_replace("he你好o he中国o hello hello hello hello hello hello hello hello hello hello 世界","e...","o我o"); test_result(temp,"ho我o ho我o ho我o ho我o ho我o ho我o ho我o ho我o ho我o ho我o ho我o ho我o 世界"); free(temp); }void test_replace_mem_leaking(){ int i = 0; printf("test replace mem leaking\n"); for(;;i++) { test_replace_function(); if(i%10000 == 0) printf("times:%d\n",i); }}void test_substr_function(){ char * result = NULL; result = text_regex_substr("he你好 hello world","e...");/*普通的正则表达式表达式*/ test_result("e你好 ",result); free(result); result = text_regex_substr("hello中 hello 国world","中[ a-z]*国");/*前后都有中文的正则表达式*/ test_result("中 hello 国",result); free(result); result = text_regex_substr("hellolo hello world","(lo)+");/*有分组子表达式的情况1*/ test_result("lo",result); free(result); result = text_regex_substr("hellolo hello world","l(lo)+");/*有分组子表达式的情况*/ test_result("lo",result); free(result); result = text_regex_substr("hellolo hello world","");/*空表达式,返回一个空字符串*/ test_result("",result); free(result); result = text_regex_substr("hellolo hello world","asdf");/*没有匹配的情况,返回NULL*/ test_result("",result); free(result); result = text_regex_substr("goooooole","o+");/*贪婪模式下的+,注意:"o*"将返回的式"" */ test_result("oooooo",result); free(result); result = text_regex_substr("goooooole","o{3,5}");/*贪婪模式下的*,*/ test_result("ooooo",result); free(result); result = text_regex_substr("goooooole","o{3,5}?");/*非贪婪模式下的*,*/ test_result("ooo",result); free(result); result = text_regex_substr("goooooole","o+?");/*非贪婪模式下的*,*/ test_result("o",result); free(result);}void test_substr_mem_leaking(){ int i = 0; printf("test substr mem leaking\n"); for(;;i++) { test_substr_function(); if(i%10000 == 0) printf("times:%d\n",i);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -