Ignore:
Timestamp:
Jan 9, 2006 5:20:59 PM (18 years ago)
Author:
bennylp
Message:

Fixed bugs and added tests to handle NULL and zero length strings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pjlib-test/string.c

    r77 r118  
    4848 *  - pj_strtoul() 
    4949 *  - pj_create_random_string() 
    50  * 
     50 *  - ... and mode.. 
    5151 * 
    5252 * This file is <b>pjlib-test/string.c</b> 
     
    6767static int stricmp_test(void) 
    6868{ 
     69/* This specificly tests and benchmark pj_stricmp(), pj_stricmp_alnum(). 
     70 * In addition, it also tests pj_stricmp2(), pj_strnicmp(), and  
     71 * pj_strnicmp2(). 
     72 */ 
    6973#define STRTEST(res,S1,S2,code) \ 
    7074            do { \ 
    71                 s1.ptr=S1; s1.slen=len; \ 
    72                 s2.ptr=S2; s2.slen=len; \ 
     75                s1.ptr=S1; s1.slen=S1?len:0; \ 
     76                s2.ptr=S2; s2.slen=S2?len:0; \ 
    7377                pj_get_timestamp(&t1); \ 
    7478                if (pj_stricmp(&s1,&s2)!=res) return code; \ 
     
    8185                pj_sub_timestamp(&t2, &t1); \ 
    8286                pj_add_timestamp(&e2, &t2); \ 
     87                if (pj_stricmp2(&s1,S2)!=res) return code*10; \ 
     88                if (pj_strnicmp(&s1,&s2,len)!=res) return code*100; \ 
     89                if (pj_strnicmp2(&s1,S2,len)!=res) return code*1000; \ 
    8390            } while (0) 
    8491 
     
    96103    len=0; 
    97104    STRTEST( 0, "","",-500); 
     105    STRTEST( 0, NULL,"",-502); 
     106    STRTEST( 0, "",NULL,-504); 
     107    STRTEST( 0, NULL,NULL,-506); 
     108    STRTEST( 0, "hello","world",-508); 
    98109 
    99110    /* equal, length=1  
     
    105116    STRTEST( 0, "a",buf+1,-512); 
    106117    STRTEST( -1, "0", "P", -514); 
     118    STRTEST(-1, NULL, "a", -516); 
     119    STRTEST(1, "a", NULL, -518); 
    107120 
    108121    /* equal, length=2  
     
    208221} 
    209222 
     223/* This tests pj_strcmp(), pj_strcmp2(), pj_strncmp(), pj_strncmp2() */ 
     224static int strcmp_test(void) 
     225{ 
     226#define STR_TEST(res,S1,S2,code)    \ 
     227            do { \ 
     228                s1.ptr=S1; s1.slen=S1?len:0; \ 
     229                s2.ptr=S2; s2.slen=S2?len:0; \ 
     230                if (pj_strcmp(&s1,&s2)!=res) return code; \ 
     231                if (pj_strcmp2(&s1,S2)!=res) return code-1; \ 
     232                if (pj_strncmp(&s1,&s2,len)!=res) return code-2; \ 
     233                if (pj_strncmp2(&s1,S2,len)!=res) return code-3; \ 
     234            } while (0) 
     235 
     236    pj_str_t s1, s2; 
     237    int len; 
     238     
     239    /* Test with length == 0 */ 
     240    len=0; 
     241    STR_TEST(0, "", "", -400); 
     242    STR_TEST(0, NULL, "", -405); 
     243    STR_TEST(0, "", NULL, -410); 
     244    STR_TEST(0, NULL, NULL, -415); 
     245    STR_TEST(0, "hello", "", -420); 
     246    STR_TEST(0, "hello", NULL, -425); 
     247 
     248    /* Test with length != 0 */ 
     249    len = 2; 
     250    STR_TEST(0, "12", "12", -430); 
     251    STR_TEST(1, "12", "1", -435); 
     252    STR_TEST(-1, "1", "12", -440); 
     253    STR_TEST(-1, NULL, "12", -445); 
     254    STR_TEST(1, "12", NULL, -450); 
     255 
     256    return 0; 
     257 
     258#undef STR_TEST 
     259} 
     260 
    210261int string_test(void) 
    211262{ 
     
    308359    pj_pool_release(pool); 
    309360 
    310     return stricmp_test(); 
     361    /* Case sensitive comparison test. */ 
     362    i = strcmp_test(); 
     363    if (i != 0) 
     364        return i; 
     365 
     366    /* Caseless comparison test. */ 
     367    i = stricmp_test(); 
     368    if (i != 0) 
     369        return i; 
     370 
     371    return 0; 
    311372} 
    312373 
Note: See TracChangeset for help on using the changeset viewer.