add tests/test_char_convert.c

pull/10/head
Yu Qing 2016-11-26 19:16:02 +08:00
parent 2e1d5eb8d2
commit 2fe5e5fb8f
3 changed files with 61 additions and 8 deletions

View File

@ -43,9 +43,9 @@ int char_converter_init_ex(FastCharConverter *pCharConverter,
int std_space_char_converter_init(FastCharConverter *pCharConverter,
const unsigned char dest_base)
{
#define SPACE_CHAR_PAIR_COUNT 7
#define SPACE_CHAR_PAIR_COUNT1 7
int i;
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT];
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT1];
pairs[0].src = '\0';
pairs[1].src = '\t';
@ -55,17 +55,17 @@ int std_space_char_converter_init(FastCharConverter *pCharConverter,
pairs[5].src = '\r';
pairs[6].src = ' ';
for (i=0; i<SPACE_CHAR_PAIR_COUNT; i++) {
for (i=0; i<SPACE_CHAR_PAIR_COUNT1; i++) {
pairs[i].dest = dest_base + i;
}
return char_converter_init(pCharConverter, pairs, SPACE_CHAR_PAIR_COUNT);
return char_converter_init(pCharConverter, pairs, SPACE_CHAR_PAIR_COUNT1);
}
int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter)
{
#define SPACE_CHAR_PAIR_COUNT 7
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT];
#define SPACE_CHAR_PAIR_COUNT2 8
FastCharPair pairs[SPACE_CHAR_PAIR_COUNT2];
pairs[0].src = '\0'; pairs[0].dest = '0';
pairs[1].src = '\t'; pairs[1].dest = 't';
@ -74,9 +74,10 @@ int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter)
pairs[4].src = '\f'; pairs[4].dest = 'f';
pairs[5].src = '\r'; pairs[5].dest = 'r';
pairs[6].src = ' '; pairs[6].dest = '-';
pairs[7].src = '\\'; pairs[7].dest = '\\';
return char_converter_init_ex(pCharConverter, pairs,
SPACE_CHAR_PAIR_COUNT, FAST_CHAR_OP_ADD_BACKSLASH);
SPACE_CHAR_PAIR_COUNT2, FAST_CHAR_OP_ADD_BACKSLASH);
}
void char_converter_set_pair(FastCharConverter *pCharConverter,

View File

@ -5,7 +5,7 @@ INC_PATH = -I/usr/include/fastcommon
LIB_PATH = -lfastcommon -lpthread
ALL_PRGS = test_allocator test_skiplist test_multi_skiplist test_mblock test_blocked_queue \
test_id_generator test_ini_parser
test_id_generator test_ini_parser test_char_convert
all: $(ALL_PRGS)
.c:

View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include "logger.h"
#include "char_convert_loader.h"
#include "char_converter.h"
int main(int argc, char *argv[])
{
int result;
int count;
int input_len;
int out_len;
char input[8 * 1024];
char output[10 * 1024];
FastCharConverter converter;
if (argc >= 2) {
input_len = snprintf(input, sizeof(input), "%s", argv[1]);
} else {
input_len = read(0, input, sizeof(input) - 1);
if (input_len < 0) {
fprintf(stderr, "read from stdin fail");
return errno;
}
*(input + input_len) = '\0';
}
log_init();
printf("input_len: %d\n%s\n\n", (int)strlen(input), input);
result = std_spaces_add_backslash_converter_init(&converter);
printf("result1: %d\n", result);
if (result != 0) {
return result;
}
char_converter_set_pair_ex(&converter, ' ',
FAST_CHAR_OP_ADD_BACKSLASH, '\'');
count = fast_char_convert(&converter, input, input_len,
output, &out_len, sizeof(output));
printf("count: %d\n", count);
printf("out_len: %d\n%.*s\n", out_len, out_len, output);
return 0;
}