#include "test.h"
#define THIS_FILE "string.c"
#if INCLUDE_STRING_TEST
#ifdef _MSC_VER
# pragma warning(disable: 4204)
#endif
#define HELLO_WORLD "Hello World"
#define HELLO_WORLD_LEN 11
#define JUST_HELLO "Hello"
#define JUST_HELLO_LEN 5
#define UL_VALUE 3456789012UL
#if 1
PJ_INLINE(
int) cmp(
const char *expr,
int i,
int j)
{
int r = !((i>0 && j>0) || (i<0 && j<0) || (i==0 && j==0));
if (r) {
PJ_LOG(3,(THIS_FILE,
" error: %s: expecting %d, got %d", expr, j, i));
}
return r;
}
#else
PJ_INLINE(
int) cmp(
const char *expr,
int i,
int j)
{
return i!=j;
}
#endif
#define C(expr, res) cmp(#expr, expr, res)
static int stricmp_test(void)
{
#define STRTEST(res,res2,S1,S2,code) \
do { \
s1.ptr=S1; s1.slen=(S1)?len:0; \
s2.ptr=S2; s2.slen=(S2)?len:0; \
pj_get_timestamp(&t1); \
if (C(pj_stricmp(&s1,&s2),res)) return code; \
pj_get_timestamp(&t2); \
pj_sub_timestamp(&t2, &t1); \
pj_add_timestamp(&e1, &t2); \
pj_get_timestamp(&t1); \
if (C(pj_stricmp_alnum(&s1,&s2),res)) return code-1; \
pj_get_timestamp(&t2); \
pj_sub_timestamp(&t2, &t1); \
pj_add_timestamp(&e2, &t2); \
if (C(pj_stricmp2(&s1,S2),res2)) return code*10; \
if (C(pj_strnicmp(&s1,&s2,len),res)) return code*100; \
if (C(pj_strnicmp2(&s1,S2,len),res)) return code*1000; \
} while (0)
char *buf;
int len;
#define SNULL 0
len=0;
STRTEST( 0, 0, "","",-500);
STRTEST( 0, 0, SNULL,"",-502);
STRTEST( 0, 0, "",SNULL,-504);
STRTEST( 0, 0, SNULL,SNULL,-506);
STRTEST( 0, -1, "hello","world",-508);
buf = "a""A";
len=1;
STRTEST( 0, -1, "a",buf+0,-510);
STRTEST( 0, 0, "a",buf+1,-512);
STRTEST(-1, -1, "O", "P", -514);
STRTEST(-1, -1, SNULL, "a", -516);
STRTEST( 1, 1, "a", SNULL, -518);
buf = "aa""Aa""aA""AA";
len=2;
STRTEST( 0, -1, "aa",buf+0,-520);
STRTEST( 0, -1, "aa",buf+2,-522);
STRTEST( 0, -1, "aa",buf+4,-524);
STRTEST( 0, 0, "aa",buf+6,-524);
buf = "aaa""Aaa""aAa""aaA""AAa""aAA""AaA""AAA";
len=3;
STRTEST( 0, -1, "aaa",buf+0,-530);
STRTEST( 0, -1, "aaa",buf+3,-532);
STRTEST( 0, -1, "aaa",buf+6,-534);
STRTEST( 0, -1, "aaa",buf+9,-536);
STRTEST( 0, -1, "aaa",buf+12,-538);
STRTEST( 0, -1, "aaa",buf+15,-540);
STRTEST( 0, -1, "aaa",buf+18,-542);
STRTEST( 0, 0, "aaa",buf+21,-534);
len=4;
STRTEST( 0, 0, "aaaa","aaaa",-540);
STRTEST( 0, 0, "aaaa","Aaaa",-542);
STRTEST( 0, 0, "aaaa","aAaa",-544);
STRTEST( 0, 0, "aaaa","aaAa",-546);
STRTEST( 0, 0, "aaaa","aaaA",-548);
STRTEST( 0, 0, "aaaa","AAaa",-550);
STRTEST( 0, 0, "aaaa","aAAa",-552);
STRTEST( 0, 0, "aaaa","aaAA",-554);
STRTEST( 0, 0, "aaaa","AaAa",-556);
STRTEST( 0, 0, "aaaa","aAaA",-558);
STRTEST( 0, 0, "aaaa","AaaA",-560);
STRTEST( 0, 0, "aaaa","AAAa",-562);
STRTEST( 0, 0, "aaaa","aAAA",-564);
STRTEST( 0, 0, "aaaa","AAaA",-566);
STRTEST( 0, 0, "aaaa","AaAA",-568);
STRTEST( 0, 0, "aaaa","AAAA",-570);
buf = "aaaAa""AaaaA""AaAaA""AAAAA";
len=5;
STRTEST( 0, -1, "aaaaa",buf+0,-580);
STRTEST( 0, -1, "aaaaa",buf+5,-582);
STRTEST( 0, -1, "aaaaa",buf+10,-584);
STRTEST( 0, 0, "aaaaa",buf+15,-586);
len=1;
STRTEST( -1, -1, "a", "b", -600);
buf = "ab""ba";
len=2;
STRTEST( -1, -1, "aa", buf+0, -610);
STRTEST( -1, -1, "aa", buf+2, -612);
buf = "aab""aba""baa";
len=3;
STRTEST( -1, -1, "aaa", buf+0, -620);
STRTEST( -1, -1, "aaa", buf+3, -622);
STRTEST( -1, -1, "aaa", buf+6, -624);
buf = "aaab""aaba""abaa""baaa";
len=4;
STRTEST( -1, -1, "aaaa", buf+0, -630);
STRTEST( -1, -1, "aaaa", buf+4, -632);
STRTEST( -1, -1, "aaaa", buf+8, -634);
STRTEST( -1, -1, "aaaa", buf+12, -636);
buf="aaaab""aaaba""aabaa""abaaa""baaaa";
len=5;
STRTEST( -1, -1, "aaaaa", buf+0, -640);
STRTEST( -1, -1, "aaaaa", buf+5, -642);
STRTEST( -1, -1, "aaaaa", buf+10, -644);
STRTEST( -1, -1, "aaaaa", buf+15, -646);
STRTEST( -1, -1, "aaaaa", buf+20, -648);
if (c1 < c2) {
PJ_LOG(3,(
"",
" info: pj_stricmp_alnum is slower than pj_stricmp!"));
}
if (c2 == 0) c2=1;
PJ_LOG(3, (
"",
" time: stricmp=%u, stricmp_alnum=%u (speedup=%d.%02dx)",
c1, c2,
(c1 * 100 / c2) / 100,
(c1 * 100 / c2) % 100));
return 0;
#undef STRTEST
}
static int strcmp_test(void)
{
#define STR_TEST(res,S1,S2,code) \
do { \
s1.ptr=S1; s1.slen=S1?len:0; \
s2.ptr=S2; s2.slen=S2?len:0; \
if (C(pj_strcmp(&s1,&s2),res)) return code; \
if (C(pj_strcmp2(&s1,S2),res)) return code-1; \
if (C(pj_strncmp(&s1,&s2,len),res)) return code-2; \
if (C(pj_strncmp2(&s1,S2,len),res)) return code-3; \
} while (0)
int len;
len=0;
STR_TEST(0, "", "", -400);
STR_TEST(0, SNULL, "", -405);
STR_TEST(0, "", SNULL, -410);
STR_TEST(0, SNULL, SNULL, -415);
STR_TEST(0, "hello", "", -420);
STR_TEST(0, "hello", SNULL, -425);
len = 2;
STR_TEST(0, "12", "12", -430);
STR_TEST(1, "12", "1", -435);
STR_TEST(-1, "1", "12", -440);
STR_TEST(-1, SNULL, "12", -445);
STR_TEST(1, "12", SNULL, -450);
return 0;
#undef STR_TEST
}
int string_test(void)
{
const pj_str_t hello_world = { HELLO_WORLD, HELLO_WORLD_LEN };
const pj_str_t just_hello = { JUST_HELLO, JUST_HELLO_LEN };
enum { RCOUNT = 10, RLEN = 16 };
int i;
if (!pool) return -5;
return -10;
return -20;
return -30;
return -40;
return -50;
return -60;
return -70;
return -80;
return -100;
return -110;
return -200;
return -210;
return -250;
return -270;
return -280;
return -290;
return -291;
return -292;
return -293;
return -294;
if (s4.
ptr == SNULL || *s4.
ptr !=
'A')
return -295;
return -296;
return -297;
for (i=0; i<RCOUNT; ++i) {
int j;
if (!random[i].ptr)
return -320;
for (j=0; j<i; ++j) {
return -330;
}
}
i = strcmp_test();
if (i != 0)
return i;
i = stricmp_test();
if (i != 0)
return i;
return 0;
}
#else
int dummy_string_test;
#endif
unsigned int pj_uint32_t
Definition: types.h:43
#define PJ_LOG(level, arg)
Definition: log.h:106
void * pj_pool_alloc(pj_pool_t *pool, pj_size_t size)
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
unsigned long pj_strtoul(const pj_str_t *str)
pj_str_t pj_str(char *str)
pj_str_t * pj_strdup2(pj_pool_t *pool, pj_str_t *dst, const char *src)
int pj_strnicmp(const pj_str_t *str1, const pj_str_t *str2, pj_size_t len)
int pj_strcmp2(const pj_str_t *str1, const char *str2)
int pj_strcmp(const pj_str_t *str1, const pj_str_t *str2)
int pj_utoa(unsigned long val, char *buf)
void pj_strcat(pj_str_t *dst, const pj_str_t *src)
pj_str_t * pj_strcpy(pj_str_t *dst, const pj_str_t *src)
unsigned long pj_strtoul2(const pj_str_t *str, pj_str_t *endptr, unsigned base)
pj_str_t * pj_strtrim(pj_str_t *str)
char * pj_create_random_string(char *str, pj_size_t length)
int pj_strncmp(const pj_str_t *str1, const pj_str_t *str2, pj_size_t len)
pj_size_t pj_strlen(const pj_str_t *str)
Definition: string.h:281
int pj_stricmp(const pj_str_t *str1, const pj_str_t *str2)
char * pj_strchr(const pj_str_t *str, int chr)
Definition: string.h:480
pj_str_t * pj_strdup(pj_pool_t *pool, pj_str_t *dst, const pj_str_t *src)
pj_status_t pj_thread_sleep(unsigned msec)
pj_uint32_t pj_elapsed_cycle(const pj_timestamp *start, const pj_timestamp *stop)
#define PJ_UNUSED_ARG(arg)
Definition: config.h:1343
#define PJ_INLINE(type)
Definition: config.h:1178
pj_ssize_t slen
Definition: types.h:125
char * ptr
Definition: types.h:122
pj_uint32_t lo
Definition: types.h:142
pj_uint32_t hi
Definition: types.h:141
struct pj_timestamp::@9 u32