string.c

来自「Netscape NSPR库源码」· C语言 代码 · 共 1,982 行 · 第 1/5 页

C
1,982
字号
        }        if( array[i].comp )        {            const char *a = array[i].result;            const char *b = array[i].dest;            while( 1 )            {                if( *a != *b )                {                    printf("FAIL %d: %s != %.32s\n", i,                            array[i].result, array[i].dest);                    return PR_FALSE;                }                if( (char)0 == *a ) break;                a++;                b++;            }        }    }                    printf("PASS\n");    return PR_TRUE;}/* PL_strdup */PRBool test_006(void){    static const char *array[] =    {        (const char *)0,        "",        "a",        "abcdefg"    };    int i;    printf("Test 006 (PL_strdup)      ..."); fflush(stdout);    for( i = 0; i < sizeof(array)/sizeof(array[0]); i++ )    {        char *rv = PL_strdup(array[i]);        if( (char *)0 == rv )        {            printf("FAIL %d: 0x%x -> 0\n", i, array[i]);            return PR_FALSE;        }        if( (const char *)0 == array[i] )        {            if( (char)0 != *rv )            {                printf("FAIL %d: (const char *)0 -> %.32s\n", i, rv);                return PR_FALSE;            }        }        else        {            const char *a = array[i];            const char *b = (const char *)rv;            while( 1 )            {                if( *a != *b )                {                    printf("FAIL %d: %s != %.32s\n", i, array[i], rv);                    return PR_FALSE;                }                if( (char)0 == *a ) break;                a++;                b++;            }        }        PL_strfree(rv);    }    printf("PASS\n");    return PR_TRUE;}/* PL_strndup */PRBool test_007(void){    static struct    {        const char *str;        PRUint32    len;        const char *result;    } array[] =      {          { (const char *)0, 0, "" },          { (const char *)0, 1, "" },          { (const char *)0, 7, "" },          { "", 0, "" },          { "", 1, "" },          { "", 7, "" },          { "a", 0, "" },          { "a", 1, "a" },          { "a", 7, "a" },          { "ab", 0, "" },          { "ab", 1, "a" },          { "ab", 7, "ab" },          { "abcdefg", 0, "" },          { "abcdefg", 1, "a" },          { "abcdefg", 7, "abcdefg" },          { "abcdefghijk", 0, "" },          { "abcdefghijk", 1, "a" },          { "abcdefghijk", 7, "abcdefg" },          { "abcdef\0ghijk", 0, "" },          { "abcdef\0ghijk", 1, "a" },          { "abcdef\0ghijk", 7, "abcdef" }      };    int i;    printf("Test 007 (PL_strndup)     ..."); fflush(stdout);    for( i = 0; i < sizeof(array)/sizeof(array[0]); i++ )    {        char *rv = PL_strndup(array[i].str, array[i].len);        const char *a;        const char *b;        if( (char *)0 == rv )        {            printf("FAIL %d: %s,%lu -> 0\n", i,                    array[i].str ? array[i].str : "(null)", array[i].len);            return PR_FALSE;        }        a = array[i].result;        b = (const char *)rv;        while( 1 )        {            if( *a != *b )            {                printf("FAIL %d: %s != %.32s\n", i, array[i].result, rv);                return PR_FALSE;            }            if( (char)0 == *a ) break;            a++;            b++;        }        free(rv);    }    printf("PASS\n");    return PR_TRUE;}/* PL_strcat */PRBool test_008(void){    static struct    {        const char *first;        const char *second;        const char *result;    } array[] =      {          { (const char *)0, (const char *)0, (const char *)0 },          { (const char *)0, "xyz", (const char *)0 },          { "", (const char *)0, "" },          { "", "", "" },          { "ab", "", "ab" },          { "cd", "ef", "cdef" },          { "gh\0X", "", "gh" },          { "ij\0X", "kl", "ijkl" },          { "mn\0X", "op\0X", "mnop" },          { "qr", "st\0X", "qrst" },          { "uv\0X", "wx\0X", "uvwx" }      };    int i;    printf("Test 008 (PL_strcat)      ..."); fflush(stdout);    for( i = 0; i < sizeof(array)/sizeof(array[0]); i++ )    {        char buffer[ 1024 ];        int j;        char *rv;        for( j = 0; j < sizeof(buffer); j++ )            buffer[j] = '-';        if( (const char *)0 != array[i].first )            (void)PL_strcpy(buffer, array[i].first);        rv = PL_strcat(((const char *)0 == array[i].first) ? (char *)0 : buffer,                       array[i].second);        if( (const char *)0 == array[i].result )        {            if( (char *)0 != rv )            {                printf("FAIL %d: %s+%s -> %.32s, not zero\n", i,                       array[i].first ? array[i].first : "(null)",                       array[i].second ? array[i].second : "(null)",                       rv);                return PR_FALSE;            }        }        else        {            if( (char *)0 == rv )            {                printf("FAIL %d: %s+%s -> null, not %s\n", i,                       array[i].first ? array[i].first : "(null)",                       array[i].second ? array[i].second : "(null)",                       array[i].result);                return PR_FALSE;            }            else            {                const char *a = array[i].result;                const char *b = (const char *)rv;                while( 1 )                {                    if( *a != *b )                    {                        printf("FAIL %d: %s+%s -> %.32s, not %s\n", i,                               array[i].first ? array[i].first : "(null)",                               array[i].second ? array[i].second : "(null)",                               rv, array[i].result);                        return PR_FALSE;                    }                    if( (char)0 == *a ) break;                    a++;                    b++;                }            }        }    }    printf("PASS\n");    return PR_TRUE;}/* PL_strncat */PRBool test_009(void){    static struct    {        const char *first;        const char *second;        PRUint32    length;        PRBool      nulled;        const char *result;    } array[] =       {          { (const char *)0, (const char *)0, 0, PR_FALSE, (const char *)0 },          { (const char *)0, (const char *)0, 1, PR_FALSE, (const char *)0 },          { (const char *)0, (const char *)0, 7, PR_FALSE, (const char *)0 },          { (const char *)0, "", 0, PR_FALSE, (const char *)0 },          { (const char *)0, "", 1, PR_FALSE, (const char *)0 },          { (const char *)0, "", 7, PR_FALSE, (const char *)0 },          { (const char *)0, "stuff", 0, PR_FALSE, (const char *)0 },          { (const char *)0, "stuff", 1, PR_FALSE, (const char *)0 },          { (const char *)0, "stuff", 7, PR_FALSE, (const char *)0 },          { "", (const char *)0, 0, PR_TRUE, "" },          { "", (const char *)0, 1, PR_TRUE, "" },          { "", (const char *)0, 7, PR_TRUE, "" },          { "", "", 0, PR_TRUE, "" },          { "", "", 1, PR_TRUE, "" },          { "", "", 7, PR_TRUE, "" },          { "", "abcdefgh", 0, PR_TRUE, "" },          { "", "abcdefgh", 1, PR_FALSE, "a" },          { "", "abcdefgh", 7, PR_FALSE, "abcdefg" },          { "xyz", (const char *)0, 0, PR_TRUE, "xyz" },          { "xyz", (const char *)0, 1, PR_TRUE, "xyz" },          { "xyz", (const char *)0, 7, PR_TRUE, "xyz" },          { "xyz", "", 0, PR_TRUE, "xyz" },          { "xyz", "", 1, PR_TRUE, "xyz" },          { "xyz", "", 7, PR_TRUE, "xyz" },          { "xyz", "abcdefgh", 0, PR_TRUE, "xyz" },          { "xyz", "abcdefgh", 1, PR_FALSE, "xyza" },          { "xyz", "abcdefgh", 7, PR_FALSE, "xyzabcdefg" }      };    int i;    printf("Test 009 (PL_strncat)     ..."); fflush(stdout);    for( i = 0; i < sizeof(array)/sizeof(array[0]); i++ )    {        char buffer[ 1024 ];        int j;        char *rv;        for( j = 0; j < sizeof(buffer); j++ )            buffer[j] = '-';        if( (const char *)0 != array[i].first )            (void)PL_strcpy(buffer, array[i].first);        rv = PL_strncat(((const char *)0 == array[i].first) ? (char *)0 : buffer,                         array[i].second, array[i].length);        if( (const char *)0 == array[i].result )        {            if( (char *)0 != rv )            {                printf("FAIL %d: %s+%s/%lu -> %.32s, not zero\n", i,                       array[i].first ? array[i].first : "(null)",                       array[i].second ? array[i].second : "(null)",                       array[i].length, rv);                return PR_FALSE;            }        }        else        {            if( (char *)0 == rv )            {                printf("FAIL %d: %s+%s/%lu -> null, not %s\n", i,                       array[i].first ? array[i].first : "(null)",                       array[i].second ? array[i].second : "(null)",                       array[i].length, array[i].result);                return PR_FALSE;            }            else            {                const char *a = array[i].result;                const char *b = (const char *)rv;                while( *a )                {                    if( *a != *b )                    {                        printf("FAIL %d: %s+%s/%lu -> %.32s, not %s\n", i,                               array[i].first ? array[i].first : "(null)",                               array[i].second ? array[i].second : "(null)",                               array[i].length, rv, array[i].result);                        return PR_FALSE;                    }                    a++;                    b++;                }                if( array[i].nulled )                {                    if( (char)0 != *b )                    {                        printf("FAIL %d: %s+%s/%lu -> not nulled\n", i,                               array[i].first ? array[i].first : "(null)",                               array[i].second ? array[i].second : "(null)",                               array[i].length);                        return PR_FALSE;                    }                }                else                {                    if( (char)0 == *b )                    {                        printf("FAIL %d: %s+%s/%lu -> overrun\n", i,                               array[i].first ? array[i].first : "(null)",                               array[i].second ? array[i].second : "(null)",                               array[i].length);                        return PR_FALSE;                    }                }            }        }    }    printf("PASS\n");    return PR_TRUE;}/* PL_strcatn */PRBool test_010(void){    static struct    {        const char *first;        const char *second;        PRUint32    length;        const char *result;    } array[] =       {          { (const char *)0, (const char *)0, 0, (const char *)0 },

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?