X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/46f4442e9a5a4f3b98b7c1083586332f6a8a99a4..51004dcb01e06fef634b61be77ed73dd61cb6db9:/icuSources/test/cintltst/ccapitst.c diff --git a/icuSources/test/cintltst/ccapitst.c b/icuSources/test/cintltst/ccapitst.c index 18accc45..1ac3bb3d 100644 --- a/icuSources/test/cintltst/ccapitst.c +++ b/icuSources/test/cintltst/ccapitst.c @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2008, International Business Machines Corporation and + * Copyright (c) 1997-2012, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /***************************************************************************** @@ -26,54 +26,7 @@ #include "cmemory.h" /* for UAlignedMemory */ #include "cintltst.h" #include "ccapitst.h" - -/* for not including "cstring.h" -begin*/ -#ifdef U_WINDOWS -# define ctest_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE _stricmp(str1, str2) -#elif defined(POSIX) -# define ctest_stricmp(str1, str2) U_STANDARD_CPP_NAMESPACE strcasecmp(str1, str2) -#else -# define ctest_stricmp(str1, str2) T_CString_stricmp(str1, str2) -#endif - -static int U_EXPORT2 -T_CString_stricmp(const char *str1, const char *str2) { - if(str1==NULL) { - if(str2==NULL) { - return 0; - } else { - return -1; - } - } else if(str2==NULL) { - return 1; - } else { - /* compare non-NULL strings lexically with lowercase */ - int rc; - unsigned char c1, c2; - for(;;) { - c1=(unsigned char)*str1; - c2=(unsigned char)*str2; - if(c1==0) { - if(c2==0) { - return 0; - } else { - return -1; - } - } else if(c2==0) { - return 1; - } else { - /* compare non-zero characters with lowercase */ - rc=(int)(unsigned char)tolower(c1)-(int)(unsigned char)tolower(c2); - if(rc!=0) { - return rc; - } - } - ++str1; - ++str2; - } - } -} -/* for not including "cstring.h" -end*/ +#include "cstring.h" #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) @@ -82,8 +35,10 @@ T_CString_stricmp(const char *str1, const char *str2) { #define UCS_FILE_NAME_SIZE 512 /*returns an action other than the one provided*/ +#if !UCONFIG_NO_LEGACY_CONVERSION static UConverterFromUCallback otherUnicodeAction(UConverterFromUCallback MIA); static UConverterToUCallback otherCharAction(UConverterToUCallback MIA); +#endif static UConverter * cnv_open(const char *name, UErrorCode *pErrorCode) { @@ -101,7 +56,9 @@ static void TestDuplicateAlias(void); static void TestCCSID(void); static void TestJ932(void); static void TestJ1968(void); +#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION static void TestLMBCSMaxChar(void); +#endif #if !UCONFIG_NO_LEGACY_CONVERSION static void TestConvertSafeCloneCallback(void); @@ -110,14 +67,18 @@ static void TestConvertSafeCloneCallback(void); static void TestEBCDICSwapLFNL(void); static void TestConvertEx(void); static void TestConvertExFromUTF8(void); +static void TestConvertExFromUTF8_C5F0(void); static void TestConvertAlgorithmic(void); void TestDefaultConverterError(void); /* defined in cctest.c */ + void TestDefaultConverterSet(void); /* defined in cctest.c */ static void TestToUCountPending(void); static void TestFromUCountPending(void); static void TestDefaultName(void); static void TestCompareNames(void); static void TestSubstString(void); static void InvalidArguments(void); +static void TestGetName(void); +static void TestUTFBOM(void); void addTestConvert(TestNode** root); @@ -135,18 +96,26 @@ void addTestConvert(TestNode** root) addTest(root, &TestCCSID, "tsconv/ccapitst/TestCCSID"); addTest(root, &TestJ932, "tsconv/ccapitst/TestJ932"); addTest(root, &TestJ1968, "tsconv/ccapitst/TestJ1968"); +#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION addTest(root, &TestLMBCSMaxChar, "tsconv/ccapitst/TestLMBCSMaxChar"); +#endif addTest(root, &TestEBCDICSwapLFNL, "tsconv/ccapitst/TestEBCDICSwapLFNL"); addTest(root, &TestConvertEx, "tsconv/ccapitst/TestConvertEx"); addTest(root, &TestConvertExFromUTF8, "tsconv/ccapitst/TestConvertExFromUTF8"); + addTest(root, &TestConvertExFromUTF8_C5F0, "tsconv/ccapitst/TestConvertExFromUTF8_C5F0"); addTest(root, &TestConvertAlgorithmic, "tsconv/ccapitst/TestConvertAlgorithmic"); addTest(root, &TestDefaultConverterError, "tsconv/ccapitst/TestDefaultConverterError"); + addTest(root, &TestDefaultConverterSet, "tsconv/ccapitst/TestDefaultConverterSet"); +#if !UCONFIG_NO_FILE_IO addTest(root, &TestToUCountPending, "tsconv/ccapitst/TestToUCountPending"); addTest(root, &TestFromUCountPending, "tsconv/ccapitst/TestFromUCountPending"); +#endif addTest(root, &TestDefaultName, "tsconv/ccapitst/TestDefaultName"); addTest(root, &TestCompareNames, "tsconv/ccapitst/TestCompareNames"); addTest(root, &TestSubstString, "tsconv/ccapitst/TestSubstString"); addTest(root, &InvalidArguments, "tsconv/ccapitst/InvalidArguments"); + addTest(root, &TestGetName, "tsconv/ccapitst/TestGetName"); + addTest(root, &TestUTFBOM, "tsconv/ccapitst/TestUTFBOM"); } static void ListNames(void) { @@ -160,7 +129,7 @@ static void ListNames(void) { log_verbose("Testing ucnv_openAllNames()..."); allNamesEnum = ucnv_openAllNames(&err); if(U_FAILURE(err)) { - log_err("FAILURE! ucnv_openAllNames() -> %s\n", myErrorName(err)); + log_data_err("FAILURE! ucnv_openAllNames() -> %s\n", myErrorName(err)); } else { const char *string = NULL; @@ -210,7 +179,7 @@ static void ListNames(void) { /* Test ucnv_countAliases() etc. */ count = ucnv_countAliases("utf-8", &err); if(U_FAILURE(err)) { - log_err("FAILURE! ucnv_countAliases(\"utf-8\") -> %s\n", myErrorName(err)); + log_data_err("FAILURE! ucnv_countAliases(\"utf-8\") -> %s\n", myErrorName(err)); } else if(count <= 0) { log_err("FAILURE! ucnv_countAliases(\"utf-8\") -> %d aliases\n", count); } else { @@ -582,8 +551,8 @@ static void TestConvert() if (!myConverter || U_FAILURE(err)) { log_data_err("Error creating the ibm-949 converter - %s \n", u_errorName(err)); - - return; + fclose(ucs_file_in); + break; } /*testing for ucnv_getName() */ @@ -595,7 +564,7 @@ static void TestConvert() { log_verbose("getName o.k. %s\n", ucnv_getName(myConverter, &err)); } - if (ctest_stricmp(ucnv_getName(myConverter, &err), CodePagesToTest[codepage_index])) + if (uprv_stricmp(ucnv_getName(myConverter, &err), CodePagesToTest[codepage_index])) log_err("getName failed\n"); else log_verbose("getName ok\n"); @@ -873,7 +842,8 @@ static void TestConvert() if (BOM!=0xFEFF && BOM!=0xFFFE) { log_err("File Missing BOM...Bailing!\n"); - return; + fclose(ucs_file_in); + break; } @@ -1076,16 +1046,17 @@ static void TestConvert() #endif } +#if !UCONFIG_NO_LEGACY_CONVERSION static UConverterFromUCallback otherUnicodeAction(UConverterFromUCallback MIA) { return (MIA==(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_STOP)?(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SUBSTITUTE:(UConverterFromUCallback)UCNV_FROM_U_CALLBACK_STOP; } - static UConverterToUCallback otherCharAction(UConverterToUCallback MIA) { return (MIA==(UConverterToUCallback)UCNV_TO_U_CALLBACK_STOP)?(UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE:(UConverterToUCallback)UCNV_TO_U_CALLBACK_STOP; } +#endif static void TestFlushCache(void) { #if !UCONFIG_NO_LEGACY_CONVERSION @@ -1341,7 +1312,7 @@ static void TestDuplicateAlias(void) { status = U_ZERO_ERROR; alias = ucnv_getStandardName("Shift_JIS", "IBM", &status); if (alias == NULL || strcmp(alias, "ibm-943") != 0 || status != U_AMBIGUOUS_ALIAS_WARNING) { - log_err("FAIL: Didn't get ibm-943 for Shift_JIS {IBM}. Got %s\n", alias); + log_data_err("FAIL: Didn't get ibm-943 for Shift_JIS {IBM}. Got %s\n", alias); } status = U_ZERO_ERROR; alias = ucnv_getStandardName("ibm-943", "IANA", &status); @@ -1385,6 +1356,7 @@ static TSCCContext *TSCC_clone(TSCCContext *ctx) return newCtx; } +#if !UCONFIG_NO_LEGACY_CONVERSION static void TSCC_fromU(const void *context, UConverterFromUnicodeArgs *fromUArgs, const UChar* codeUnits, @@ -1432,7 +1404,6 @@ static void TSCC_fromU(const void *context, } } - static void TSCC_toU(const void *context, UConverterToUnicodeArgs *toUArgs, const char* codeUnits, @@ -1500,7 +1471,6 @@ static void TSCC_print_log(TSCCContext *q, const char *name) } } -#if !UCONFIG_NO_LEGACY_CONVERSION static void TestConvertSafeCloneCallback() { UErrorCode err = U_ZERO_ERROR; @@ -1516,7 +1486,7 @@ static void TestConvertSafeCloneCallback() conv1 = ucnv_open("iso-8859-3", &err); if(U_FAILURE(err)) { - log_data_err("Err opening iso-8859-3, %s", u_errorName(err)); + log_data_err("Err opening iso-8859-3, %s\n", u_errorName(err)); return; } @@ -1734,7 +1704,7 @@ static void TestConvertSafeClone() UChar *pUCharTargetLimit = uniCharBuffer + sizeof(uniCharBuffer)/sizeof(*uniCharBuffer); const UChar * pUniBuffer; const UChar *uniBufferLimit = uniBuffer + sizeof(uniBuffer)/sizeof(*uniBuffer); - int32_t index, j; + int32_t idx, j; err = U_ZERO_ERROR; cnv = ucnv_open(names[0], &err); @@ -1821,22 +1791,22 @@ static void TestConvertSafeClone() /* Do these cloned converters work at all - shuffle UChars to chars & back again..*/ for(j = 0; j < LENGTHOF(bufferSizes); ++j) { - for (index = 0; index < LENGTHOF(names); index++) + for (idx = 0; idx < LENGTHOF(names); idx++) { err = U_ZERO_ERROR; - cnv = ucnv_open(names[index], &err); + cnv = ucnv_open(names[idx], &err); if(U_FAILURE(err)) { - log_data_err("ucnv_open(\"%s\") failed - %s\n", names[index], u_errorName(err)); + log_data_err("ucnv_open(\"%s\") failed - %s\n", names[idx], u_errorName(err)); continue; } if(j == 0) { /* preflight to get maxBufferSize */ - actualSizes[index] = 0; - ucnv_safeClone(cnv, NULL, &actualSizes[index], &err); - if(actualSizes[index] > maxBufferSize) { - maxBufferSize = actualSizes[index]; - maxName = names[index]; + actualSizes[idx] = 0; + ucnv_safeClone(cnv, NULL, &actualSizes[idx], &err); + if(actualSizes[idx] > maxBufferSize) { + maxBufferSize = actualSizes[idx]; + maxName = names[idx]; } } @@ -1848,10 +1818,10 @@ static void TestConvertSafeClone() /* close the original immediately to make sure that the clone works by itself */ ucnv_close(cnv); - if( actualSizes[index] <= (bufferSizes[j] - (int32_t)sizeof(UAlignedMemory)) && + if( actualSizes[idx] <= (bufferSizes[j] - (int32_t)sizeof(UAlignedMemory)) && err == U_SAFECLONE_ALLOCATED_WARNING ) { - log_err("ucnv_safeClone(%s) did a heap clone although the buffer was large enough\n", names[index]); + log_err("ucnv_safeClone(%s) did a heap clone although the buffer was large enough\n", names[idx]); } /* check if the clone function overwrote any bytes that it is not supposed to touch */ @@ -1861,13 +1831,13 @@ static void TestConvertSafeClone() containsAnyOtherByte(buffer[1]+bufferSize, (int32_t)(sizeof(buffer)-(sizeof(buffer[0])+bufferSize)), 0xaa) ) { log_err("cloning %s in a stack buffer overwrote bytes outside the bufferSize %d (requested %d)\n", - names[index], bufferSize, bufferSizes[j]); + names[idx], bufferSize, bufferSizes[j]); } } else { /* heap-allocated the clone */ if(containsAnyOtherByte(buffer[0], (int32_t)sizeof(buffer), 0xaa)) { log_err("cloning %s used the heap (bufferSize %d, requested %d) but overwrote stack buffer bytes\n", - names[index], bufferSize, bufferSizes[j]); + names[idx], bufferSize, bufferSizes[j]); } } @@ -2466,11 +2436,13 @@ static const char *const badUTF8[]={ "\xff" }; +#define ARG_CHAR_ARR_SIZE 8 + /* get some character that can be converted and convert it */ static UBool getTestChar(UConverter *cnv, const char *converterName, char charUTF8[4], int32_t *pCharUTF8Length, - char char0[8], int32_t *pChar0Length, - char char1[8], int32_t *pChar1Length) { + char char0[ARG_CHAR_ARR_SIZE], int32_t *pChar0Length, + char char1[ARG_CHAR_ARR_SIZE], int32_t *pChar1Length) { UChar utf16[U16_MAX_LENGTH]; int32_t utf16Length; @@ -2495,7 +2467,7 @@ static UBool getTestChar(UConverter *cnv, const char *converterName, utf16Source=utf16; target=char0; ucnv_fromUnicode(cnv, - &target, char0+sizeof(char0), + &target, char0+ARG_CHAR_ARR_SIZE, &utf16Source, utf16+utf16Length, NULL, FALSE, &errorCode); *pChar0Length=(int32_t)(target-char0); @@ -2503,7 +2475,7 @@ static UBool getTestChar(UConverter *cnv, const char *converterName, utf16Source=utf16; target=char1; ucnv_fromUnicode(cnv, - &target, char1+sizeof(char1), + &target, char1+ARG_CHAR_ARR_SIZE, &utf16Source, utf16+utf16Length, NULL, FALSE, &errorCode); *pChar1Length=(int32_t)(target-char1); @@ -2652,7 +2624,7 @@ static void TestConvertExFromUTF8() { errorCode=U_ZERO_ERROR; utf8Cnv=ucnv_open("UTF-8", &errorCode); if(U_FAILURE(errorCode)) { - log_err("unable to open UTF-8 converter - %s\n", u_errorName(errorCode)); + log_data_err("unable to open UTF-8 converter - %s\n", u_errorName(errorCode)); return; } @@ -2660,7 +2632,7 @@ static void TestConvertExFromUTF8() { errorCode=U_ZERO_ERROR; cnv=ucnv_open(converterNames[i], &errorCode); if(U_FAILURE(errorCode)) { - log_err("unable to open %s converter - %s\n", converterNames[i], u_errorName(errorCode)); + log_data_err("unable to open %s converter - %s\n", converterNames[i], u_errorName(errorCode)); continue; } if(!getTestChar(cnv, converterNames[i], charUTF8, &charUTF8Length, char0, &char0Length, char1, &char1Length)) { @@ -2673,6 +2645,91 @@ static void TestConvertExFromUTF8() { ucnv_close(utf8Cnv); } +static void TestConvertExFromUTF8_C5F0() { + static const char *const converterNames[]={ +#if !UCONFIG_NO_LEGACY_CONVERSION + "windows-1251", + "shift-jis", +#endif + "us-ascii", + "iso-8859-1", + "utf-8" + }; + + UConverter *utf8Cnv, *cnv; + UErrorCode errorCode; + int32_t i; + + static const char bad_utf8[2]={ (char)0xC5, (char)0xF0 }; + /* Expect "��" (2x U+FFFD as decimal NCRs) */ + static const char twoNCRs[16]={ + 0x26, 0x23, 0x36, 0x35, 0x35, 0x33, 0x33, 0x3B, + 0x26, 0x23, 0x36, 0x35, 0x35, 0x33, 0x33, 0x3B + }; + static const char twoFFFD[6]={ + (char)0xef, (char)0xbf, (char)0xbd, + (char)0xef, (char)0xbf, (char)0xbd + }; + const char *expected; + int32_t expectedLength; + char dest[20]; /* longer than longest expectedLength */ + + const char *src; + char *target; + + UChar pivotBuffer[128]; + UChar *pivotSource, *pivotTarget; + + errorCode=U_ZERO_ERROR; + utf8Cnv=ucnv_open("UTF-8", &errorCode); + if(U_FAILURE(errorCode)) { + log_data_err("unable to open UTF-8 converter - %s\n", u_errorName(errorCode)); + return; + } + + for(i=0; i %s/decimal NCRs) failed\n", converterNames[i]); + } + ucnv_close(cnv); + } + ucnv_close(utf8Cnv); +} + static void TestConvertAlgorithmic() { #if !UCONFIG_NO_LEGACY_CONVERSION @@ -2799,6 +2856,7 @@ ucnv_close(cnv); #endif } +#if !UCONFIG_NO_FILE_IO && !UCONFIG_NO_LEGACY_CONVERSION static void TestLMBCSMaxChar(void) { static const struct { int8_t maxSize; @@ -2808,8 +2866,8 @@ static void TestLMBCSMaxChar(void) { { 1, "US-ASCII"}, { 1, "ISO-8859-1"}, - { 4, "UTF-16"}, - { 4, "UTF-16BE"}, + { 2, "UTF-16"}, + { 2, "UTF-16BE"}, { 3, "UTF-8"}, { 3, "CESU-8"}, { 3, "SCSU"}, @@ -2868,7 +2926,7 @@ static void TestLMBCSMaxChar(void) { log_err("error UCNV_GET_MAX_BYTES_FOR_STRING(1, 2)<10\n"); } } - +#endif static void TestJ1968(void) { UErrorCode err = U_ZERO_ERROR; @@ -3246,14 +3304,14 @@ TestToUCountPending(){ } ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_STOP, NULL, oldToUAction, NULL, &status); for(i=0; i