将配置文件纳入版本管理。
This commit is contained in:
123
plugin/business/pangu-http/src/test_pattern_replace.cpp
Normal file
123
plugin/business/pangu-http/src/test_pattern_replace.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "pattern_replace.h"
|
||||
|
||||
#include <sys/types.h>//fstat
|
||||
#include <sys/ioctl.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
TEST(PatternReplace, Grouping1)
|
||||
{
|
||||
const char* find="(?<name1>John)|(?<name2>李梅梅)|(?<name3>Jake)";
|
||||
const char* replacement="${name1:+Joseph:${name2:+王桃花:Keith}}";
|
||||
const char* input="John loves 李梅梅, 李梅梅 loves Jake and Jake doesn't care about John and 李梅梅.";
|
||||
const char* expect="Joseph loves 王桃花, 王桃花 loves Keith and Keith doesn't care about Joseph and 王桃花.";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
// EXPECT_STREQ(output, expect);
|
||||
printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
TEST(PatternReplace, Grouping2)
|
||||
{
|
||||
const char* find="(?<=中央)(?<ac1>空调)|(?<=家用)(?<ac2>空调)";
|
||||
const char* replacement="${ac1:+Air conditioner:${ac2:+立式空调}}";
|
||||
const char* input="中央空调,家用空调,只有第一个空调会被替换。";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
// EXPECT_STREQ(output, expect);
|
||||
printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
TEST(PatternReplace, Non_Zero_Terminated)
|
||||
{
|
||||
const char* filename="./test_data/facebook_index.html";
|
||||
char fn_replaced[256];
|
||||
const char* find="添加手机号";
|
||||
const char* replacement="Add a Mobile Phone Number";
|
||||
|
||||
FILE* fp=NULL;
|
||||
struct stat file_info;
|
||||
stat(filename, &file_info);
|
||||
size_t input_sz=file_info.st_size;
|
||||
|
||||
fp=fopen(filename,"r");
|
||||
ASSERT_FALSE(fp==NULL);
|
||||
if(fp==NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
char* input=(char*)malloc(input_sz);
|
||||
fread(input,1,input_sz,fp);
|
||||
fclose(fp);
|
||||
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
simple_replace(find, replacement, input, input_sz, &output, &output_sz);
|
||||
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL==memmem(output, output_sz, find, strlen(find)));
|
||||
EXPECT_TRUE(NULL!=memmem(output, output_sz, replacement, strlen(replacement)));
|
||||
snprintf(fn_replaced,sizeof(fn_replaced), "%s.replaced", filename);
|
||||
fp=fopen(fn_replaced, "w");
|
||||
fwrite(output, 1,output_sz, fp);
|
||||
fclose(fp);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternReplace, ASCII)
|
||||
{
|
||||
const char* find="James";
|
||||
const char* replacement="John";
|
||||
const char* input="James, where are you? Alice is calling you.";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL==strstr(output, find));
|
||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||
|
||||
// printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(PatternReplace, UTF8)
|
||||
{
|
||||
const char* find="视频";
|
||||
const char* replacement="短片";
|
||||
const char* input="欢迎来到 Facebook开始添加好友吧!他们的视频、照片和帖子都会显示在这里。";
|
||||
char* output=NULL;
|
||||
size_t output_sz=0;
|
||||
|
||||
simple_replace(find, replacement, input, strlen(input),&output, &output_sz);
|
||||
EXPECT_TRUE(output_sz>0);
|
||||
EXPECT_TRUE(NULL==strstr(output, find));
|
||||
EXPECT_TRUE(NULL!=strstr(output, replacement));
|
||||
|
||||
// printf("%s\n", output);
|
||||
free(output);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user