X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..249c4c5ea9376c24572daf9c2effa7484a282f14:/icuSources/test/cintltst/cstrtest.c diff --git a/icuSources/test/cintltst/cstrtest.c b/icuSources/test/cintltst/cstrtest.c index 89909443..0cf3c6f7 100644 --- a/icuSources/test/cintltst/cstrtest.c +++ b/icuSources/test/cintltst/cstrtest.c @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** -* Copyright (C) 1998-2003, International Business Machines Corporation +* Copyright (C) 1998-2014, International Business Machines Corporation * and others. All Rights Reserved. ********************************************************************** * @@ -13,18 +15,23 @@ ******************************************************************************* */ - +#include "unicode/ustring.h" +#include "unicode/ucnv.h" #include "cstring.h" +#include "uinvchar.h" #include "cintltst.h" #include "cmemory.h" static void TestAPI(void); void addCStringTest(TestNode** root); +static void TestInvariant(void); +static void TestCompareInvEbcdicAsAscii(void); + void addCStringTest(TestNode** root) { - addTest(root, &TestAPI, "tsutil/cstrtest/TestAPI"); - + addTest(root, &TestInvariant, "tsutil/cstrtest/TestInvariant"); + addTest(root, &TestCompareInvEbcdicAsAscii, "tsutil/cstrtest/TestCompareInvEbcdicAsAscii"); } static void TestAPI(void) @@ -90,28 +97,36 @@ static void TestAPI(void) if(uprv_stricmp(src, "1AF") != 0 || i != 3){ log_err("FAIL: ****integerToString(src, 431, 16); failed. Expected: \"1AF\", Got: %s\n", src); } + i = T_CString_int64ToString(src, U_INT64_MAX, 10); + if(uprv_strcmp(src, "9223372036854775807") != 0 || i != 19){ + log_err("FAIL: ****integerToString(src, 9223372036854775807, 10); failed. Got: %s\n", src); + } + i = T_CString_int64ToString(src, U_INT64_MAX, 16); + if(uprv_stricmp(src, "7FFFFFFFFFFFFFFF") != 0 || i != 16){ + log_err("FAIL: ****integerToString(src, 7FFFFFFFFFFFFFFF, 16); failed. Got: %s\n", src); + } uprv_strcpy(src, "this is lower case"); - if(T_CString_stricmp(src, "THIS is lower CASE") != 0){ - log_err("FAIL: *****T_CString_stricmp() failed."); + if(uprv_stricmp(src, "THIS is lower CASE") != 0){ + log_err("FAIL: *****uprv_stricmp() failed."); } - if((intValue=T_CString_stricmp(NULL, "first string is null") )!= -1){ - log_err("FAIL: T_CString_stricmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_stricmp(NULL, "first string is null") )!= -1){ + log_err("FAIL: uprv_stricmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp("second string is null", NULL)) != 1){ - log_err("FAIL: T_CString_stricmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_stricmp("second string is null", NULL)) != 1){ + log_err("FAIL: uprv_stricmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp(NULL, NULL)) != 0){ - log_err("FAIL: T_CString_stricmp(NULL, NULL) failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_stricmp(NULL, NULL)) != 0){ + log_err("FAIL: uprv_stricmp(NULL, NULL) failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_stricmp("", "")) != 0){ - log_err("FAIL: T_CString_stricmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_stricmp("", "")) != 0){ + log_err("FAIL: uprv_stricmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_stricmp("", "abc")) != -1){ - log_err("FAIL: T_CString_stricmp(\"\", \"abc\") failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_stricmp("", "abc")) != -1){ + log_err("FAIL: uprv_stricmp(\"\", \"abc\") failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_stricmp("abc", "")) != 1){ - log_err("FAIL: T_CString_stricmp(\"abc\", \"\") failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_stricmp("abc", "")) != 1){ + log_err("FAIL: uprv_stricmp(\"abc\", \"\") failed. Expected: 1, returned %d\n", intValue); } temp=uprv_strdup("strdup"); @@ -121,26 +136,206 @@ static void TestAPI(void) uprv_free((char *)temp); uprv_strcpy(src, "this is lower case"); - if(T_CString_strnicmp(src, "THIS", 4 ) != 0){ - log_err("FAIL: *****T_CString_strnicmp() failed."); + if(uprv_strnicmp(src, "THIS", 4 ) != 0){ + log_err("FAIL: *****uprv_strnicmp() failed."); } - if((intValue=T_CString_strnicmp(NULL, "first string is null", 10) )!= -1){ - log_err("FAIL: T_CString_strnicmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_strnicmp(NULL, "first string is null", 10) )!= -1){ + log_err("FAIL: uprv_strnicmp() where the first string is null failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp("second string is null", NULL, 10)) != 1){ - log_err("FAIL: T_CString_strnicmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("second string is null", NULL, 10)) != 1){ + log_err("FAIL: uprv_strnicmp() where the second string is null failed. Expected: 1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp(NULL, NULL, 10)) != 0){ - log_err("FAIL: T_CString_strnicmp(NULL, NULL, 10) failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_strnicmp(NULL, NULL, 10)) != 0){ + log_err("FAIL: uprv_strnicmp(NULL, NULL, 10) failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_strnicmp("", "", 10)) != 0){ - log_err("FAIL: T_CString_strnicmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; + if((intValue=uprv_strnicmp("", "", 10)) != 0){ + log_err("FAIL: uprv_strnicmp(\"\", \"\") failed. Expected: 0, returned %d\n", intValue);; } - if((intValue=T_CString_strnicmp("", "abc", 10)) != -1){ - log_err("FAIL: T_CString_stricmp(\"\", \"abc\", 10) failed. Expected: -1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("", "abc", 10)) != -1){ + log_err("FAIL: uprv_stricmp(\"\", \"abc\", 10) failed. Expected: -1, returned %d\n", intValue); } - if((intValue=T_CString_strnicmp("abc", "", 10)) != 1){ - log_err("FAIL: T_CString_strnicmp(\"abc\", \"\", 10) failed. Expected: 1, returned %d\n", intValue); + if((intValue=uprv_strnicmp("abc", "", 10)) != 1){ + log_err("FAIL: uprv_strnicmp(\"abc\", \"\", 10) failed. Expected: 1, returned %d\n", intValue); } } + +/* test invariant-character handling */ +static void +TestInvariant() { + /* all invariant graphic chars and some control codes (not \n!) */ + const char invariantChars[]= + "\t\r \"%&'()*+,-./" + "0123456789:;<=>?" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ_" + "abcdefghijklmnopqrstuvwxyz"; + + const UChar invariantUChars[]={ + 9, 0xd, 0x20, 0x22, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5f, + 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0 + }; + + const char variantChars[]="\n!#$@[\\]^`{|}~"; + + const UChar variantUChars[]={ + 0x0a, 0x21, 0x23, 0x24, 0x40, 0x5b, 0x5c, 0x5d, 0x5e, 0x60, 0x7b, 0x7c, 0x7d, 0x7e, 0 + }; + + const UChar nonASCIIUChars[]={ 0x80, 0xa0, 0x900, 0xff51 }; + + UChar us[120]; + char cs[120]; + + int32_t i, length; + + /* make sure that all invariant characters convert both ways */ + length=sizeof(invariantChars); + u_charsToUChars(invariantChars, us, length); + if(u_strcmp(us, invariantUChars)!=0) { + log_err("u_charsToUChars(invariantChars) failed\n"); + } + + u_UCharsToChars(invariantUChars, cs, length); + if(strcmp(cs, invariantChars)!=0) { + log_err("u_UCharsToChars(invariantUChars) failed\n"); + } + + + /* + * make sure that variant characters convert from source code literals to Unicode + * but not back to char * + */ + length=sizeof(variantChars); + u_charsToUChars(variantChars, us, length); + if(u_strcmp(us, variantUChars)!=0) { + log_err("u_charsToUChars(variantChars) failed\n"); + } + +#ifdef NDEBUG + /* + * Test u_UCharsToChars(variantUChars) only in release mode because it will + * cause an assertion failure in debug builds. + */ + u_UCharsToChars(variantUChars, cs, length); + for(i=0; i0 || (diff1==0 && 0!=uprv_strcmp(invStrings[i-1][0], invStrings[i][0]))) { + log_err("uprv_compareInvEbcdicAsAscii(%s, %s)=%hd is wrong\n", + invStrings[i-1][1], invStrings[i][1], (short)diff1); + } + /* compare current vs. previous, should be inverse diff */ + diff2=getSign(uprv_compareInvEbcdicAsAscii(invStrings[i][0], invStrings[i-1][0])); + if(diff2!=-diff1) { + log_err("uprv_compareInvEbcdicAsAscii(%s, %s)=%hd is wrong\n", + invStrings[i][1], invStrings[i-1][1], (short)diff2); + } + } +}