/* *### TODO: Add more invalid rules to test all different scenarios. * */ static void TestInvalidRules(){ #define MAX_ERROR_STATES 2 static const char* rulesArr[MAX_ERROR_STATES] = { "& C < ch, cH, Ch[this should fail]<d", "& C < ch, cH, & Ch[variable top]" }; static const char* preContextArr[MAX_ERROR_STATES] = { " C < ch, cH, Ch", "& C < ch, cH", }; static const char* postContextArr[MAX_ERROR_STATES] = { "[this should fa", ", & Ch[variable" }; int i; for(i = 0;i<MAX_ERROR_STATES;i++){ UChar rules[1000] = { '\0' }; UChar preContextExp[1000] = { '\0' }; UChar postContextExp[1000] = { '\0' }; UParseError parseError; UErrorCode status = U_ZERO_ERROR; UCollator* coll=0; u_charsToUChars(rulesArr[i],rules,uprv_strlen(rulesArr[i])+1); u_charsToUChars(preContextArr[i],preContextExp,uprv_strlen(preContextArr[i])+1); u_charsToUChars(postContextArr[i],postContextExp,uprv_strlen(postContextArr[i])+1); /* clean up stuff in parseError */ u_memset(parseError.preContext,0x0000,U_PARSE_CONTEXT_LEN); u_memset(parseError.postContext,0x0000,U_PARSE_CONTEXT_LEN); /* open the rules and test */ coll = ucol_openRules(rules,u_strlen(rules),UCOL_OFF,UCOL_DEFAULT_STRENGTH,&parseError,&status); (void)coll; /* Suppress set but not used warning. */ if(u_strcmp(parseError.preContext,preContextExp)!=0){ log_err_status(status, "preContext in UParseError for ucol_openRules does not match: \"%s\"\n", aescstrdup(parseError.preContext, -1)); } if(u_strcmp(parseError.postContext,postContextExp)!=0){ log_err_status(status, "postContext in UParseError for ucol_openRules does not match: \"%s\"\n", aescstrdup(parseError.postContext, -1)); } } }
void reportCResult( const UChar source[], const UChar target[], uint8_t *sourceKey, uint8_t *targetKey, UCollationResult compareResult, UCollationResult keyResult, UCollationResult incResult, UCollationResult expectedResult ) { if (expectedResult < -1 || expectedResult > 1) { log_err("***** invalid call to reportCResult ****\n"); return; } if (compareResult != expectedResult) { log_err("Compare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1), getCompareResult(compareResult), getCompareResult(expectedResult) ); } if (incResult != expectedResult) { log_err("incCompare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1), getCompareResult(incResult), getCompareResult(expectedResult) ); } if (keyResult != expectedResult) { log_err("KeyCompare(%s , %s) returned: %s expected: %s\n", aescstrdup(source,-1), aescstrdup(target,-1), getCompareResult(keyResult), getCompareResult(expectedResult) ); } if (keyResult != compareResult) { log_err("difference between sortkey and compare result for (%s , %s) Keys: %s compare %s\n", aescstrdup(source,-1), aescstrdup(target,-1), getCompareResult(keyResult), getCompareResult(compareResult)); } if(keyResult != expectedResult || keyResult != compareResult) { char sk[10000]; log_verbose("SortKey1: %s\n", dumpSk(sourceKey, sk)); log_verbose("SortKey2: %s\n", dumpSk(targetKey, sk)); } }
/** * Test localized currency patterns. */ static void TestCurrency(void) { UNumberFormat *currencyFmt; UChar *str; int32_t lneed, i; UFieldPosition pos; UChar res[100]; UErrorCode status = U_ZERO_ERROR; const char* locale[]={"fr_CA", "de_DE_PREEURO", "fr_FR_PREEURO"}; const char* result[]={"1,50\\u00a0$", "1,50\\u00a0DM", "1,50\\u00a0F"}; log_verbose("\nTesting the number format with different currency patterns\n"); for(i=0; i < 3; i++) { str=NULL; currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,locale[i],NULL, &status); if(U_FAILURE(status)){ log_data_err("Error in the construction of number format with style currency: %s (Are you missing data?)\n", myErrorName(status)); } else { lneed=0; lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); pos.field = 0; unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) ); } else { u_unescape(result[i], res, (int32_t)strlen(result[i])+1); if (u_strcmp(str, res) != 0){ log_err("FAIL: Expected %s Got: %s for locale: %s\n", result[i], aescstrdup(str, -1), locale[i]); } } } unum_close(currencyFmt); free(str); } }
static void TestCaseLower(void) { static const UChar beforeLower[]= { 0x61, 0x42, 0x49, 0x3a3, 0xdf, 0x3a3, 0x2f, 0xd93f, 0xdfff }, lowerRoot[]= { 0x61, 0x62, 0x69, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff }, lowerTurkish[]={ 0x61, 0x62, 0x131, 0x3c3, 0xdf, 0x3c2, 0x2f, 0xd93f, 0xdfff }; UChar buffer[32]; int32_t length; UErrorCode errorCode; /* lowercase with root locale and separate buffers */ buffer[0]=0xabcd; errorCode=U_ZERO_ERROR; length=u_strToLower(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, "", &errorCode); if( U_FAILURE(errorCode) || length!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) || uprv_memcmp(lowerRoot, buffer, length*U_SIZEOF_UCHAR)!=0 || buffer[length]!=0 ) { log_err("error in u_strToLower(root locale)=%ld error=%s string matches: %s\t\nlowerRoot=%s\t\nbuffer=%s\n", length, u_errorName(errorCode), uprv_memcmp(lowerRoot, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no", aescstrdup(lowerRoot,-1), aescstrdup(buffer,-1)); } /* lowercase with turkish locale and in the same buffer */ uprv_memcpy(buffer, beforeLower, sizeof(beforeLower)); buffer[sizeof(beforeLower)/U_SIZEOF_UCHAR]=0; errorCode=U_ZERO_ERROR; length=u_strToLower(buffer, sizeof(buffer)/U_SIZEOF_UCHAR, buffer, -1, /* implicit srcLength */ "tr", &errorCode); if( U_FAILURE(errorCode) || length!=(sizeof(lowerTurkish)/U_SIZEOF_UCHAR) || uprv_memcmp(lowerTurkish, buffer, length*U_SIZEOF_UCHAR)!=0 || buffer[length]!=0 ) { log_err("error in u_strToLower(turkish locale)=%ld error=%s string matches: %s\n", length, u_errorName(errorCode), uprv_memcmp(lowerTurkish, buffer, length*U_SIZEOF_UCHAR)==0 && buffer[length]==0 ? "yes" : "no"); } /* test preflighting */ buffer[0]=buffer[2]=0xabcd; errorCode=U_ZERO_ERROR; length=u_strToLower(buffer, 2, /* set destCapacity=2 */ beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, "", &errorCode); if( errorCode!=U_BUFFER_OVERFLOW_ERROR || length!=(sizeof(lowerRoot)/U_SIZEOF_UCHAR) || uprv_memcmp(lowerRoot, buffer, 2*U_SIZEOF_UCHAR)!=0 || buffer[2]!=0xabcd ) { log_err("error in u_strToLower(root locale preflighting)=%ld error=%s string matches: %s\n", length, u_errorName(errorCode), uprv_memcmp(lowerRoot, buffer, 2*U_SIZEOF_UCHAR)==0 && buffer[2]==0xabcd ? "yes" : "no"); } /* test error handling */ errorCode=U_ZERO_ERROR; length=u_strToLower(NULL, sizeof(buffer)/U_SIZEOF_UCHAR, beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, "", &errorCode); if(errorCode!=U_ILLEGAL_ARGUMENT_ERROR) { log_err("error in u_strToLower(root locale dest=NULL)=%ld error=%s\n", length, u_errorName(errorCode)); } buffer[0]=0xabcd; errorCode=U_ZERO_ERROR; length=u_strToLower(buffer, -1, beforeLower, sizeof(beforeLower)/U_SIZEOF_UCHAR, "", &errorCode); if( errorCode!=U_ILLEGAL_ARGUMENT_ERROR || buffer[0]!=0xabcd ) { log_err("error in u_strToLower(root locale destCapacity=-1)=%ld error=%s buffer[0]==0x%lx\n", length, u_errorName(errorCode), buffer[0]); } }
static void TestUListFmt() { const ListFmtTestEntry * lftep; for (lftep = listFmtTestEntries; lftep->locale != NULL ; lftep++ ) { UErrorCode status = U_ZERO_ERROR; UListFormatter *listfmt = ulistfmt_open(lftep->locale, &status); if ( U_FAILURE(status) ) { log_data_err("ERROR: ulistfmt_open fails for locale %s, status %s\n", lftep->locale, u_errorName(status)); } else { UChar ubufActual[kUBufMax]; int32_t ulenActual = ulistfmt_format(listfmt, strings, stringLengths, lftep->stringCount, ubufActual, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("ERROR: ulistfmt_format fails for locale %s count %d (real lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); } else { UChar ubufExpected[kUBufMax]; int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { log_err("ERROR: ulistfmt_format for locale %s count %d (real lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); } } /* try again with all lengths -1 */ status = U_ZERO_ERROR; ulenActual = ulistfmt_format(listfmt, strings, stringLengthsNeg, lftep->stringCount, ubufActual, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("ERROR: ulistfmt_format fails for locale %s count %d (-1 lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); } else { UChar ubufExpected[kUBufMax]; int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { log_err("ERROR: ulistfmt_format for locale %s count %d (-1 lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); } } /* try again with NULL lengths */ status = U_ZERO_ERROR; ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, ubufActual, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("ERROR: ulistfmt_format fails for locale %s count %d (NULL lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); } else { UChar ubufExpected[kUBufMax]; int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { log_err("ERROR: ulistfmt_format for locale %s count %d (NULL lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); } } /* try calls that should return error */ status = U_ZERO_ERROR; ulenActual = ulistfmt_format(listfmt, NULL, NULL, lftep->stringCount, ubufActual, kUBufMax, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) { log_err("ERROR: ulistfmt_format for locale %s count %d with NULL strings, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale, lftep->stringCount, u_errorName(status), ulenActual); } status = U_ZERO_ERROR; ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, NULL, kUBufMax, &status); if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) { log_err("ERROR: ulistfmt_format for locale %s count %d with NULL result, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale, lftep->stringCount, u_errorName(status), ulenActual); } ulistfmt_close(listfmt); } } }
/** * Test currency "object" (we use this name to match the other C++ * test name and the Jave name). Actually, test ISO currency code * support in the C API. */ static void TestCurrencyObject(void) { UNumberFormat *currencyFmt; UChar *str=NULL, *res=NULL; int32_t lneed, i; UFieldPosition pos; UErrorCode status = U_ZERO_ERROR; const char* locale[]={ "fr_FR", "fr_FR", }; const char* currency[]={ "", "JPY", }; const char* result[]={ "1\\u202F234,56\\u00A0\\u20AC", "1\\u202F235\\u00A0JPY", }; log_verbose("\nTesting the number format with different currency codes\n"); for(i=0; i < 2; i++) { char cStr[20]={0}; UChar isoCode[16]={0}; currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,locale[i],NULL, &status); if(U_FAILURE(status)){ log_data_err("Error in the construction of number format with style currency: %s (Are you missing data?)\n", myErrorName(status)); } else { if (*currency[i]) { u_uastrcpy(isoCode, currency[i]); unum_setTextAttribute(currencyFmt, UNUM_CURRENCY_CODE, isoCode, u_strlen(isoCode), &status); if(U_FAILURE(status)) { log_err("FAIL: can't set currency code %s\n", myErrorName(status) ); } } unum_getTextAttribute(currencyFmt, UNUM_CURRENCY_CODE, isoCode, sizeof(isoCode), &status); if(U_FAILURE(status)) { log_err("FAIL: can't get currency code %s\n", myErrorName(status) ); } u_UCharsToChars(isoCode,cStr,u_strlen(isoCode)); log_verbose("ISO code %s\n", cStr); if (*currency[i] && uprv_strcmp(cStr, currency[i])) { log_err("FAIL: currency should be %s, but is %s\n", currency[i], cStr); } lneed=0; lneed= unum_formatDouble(currencyFmt, 1234.56, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); pos.field = 0; unum_formatDouble(currencyFmt, 1234.56, str, lneed+1, &pos, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) ); } else { res=(UChar*)malloc(sizeof(UChar) * (strlen(result[i])+1) ); u_unescape(result[i],res, (int32_t)(strlen(result[i])+1)); if (u_strcmp(str, res) != 0){ log_err("FAIL: Expected %s Got: %s for locale: %s\n", result[i],aescstrdup(str, -1),locale[i]); } } } unum_close(currencyFmt); free(str); free(res); } }
/** * Test localized currency patterns for PREEURO variants. */ static void TestCurrencyPreEuro(void) { UNumberFormat *currencyFmt; UChar *str=NULL, *res=NULL; int32_t lneed, i; UFieldPosition pos; UErrorCode status = U_ZERO_ERROR; const char* locale[]={ "ca_ES_PREEURO", "de_LU_PREEURO", "en_IE_PREEURO", "fi_FI_PREEURO", "fr_LU_PREEURO", "it_IT_PREEURO", "pt_PT_PREEURO", "de_AT_PREEURO", "el_GR_PREEURO", "es_ES_PREEURO", "fr_BE_PREEURO", "ga_IE_PREEURO", "nl_BE_PREEURO", "de_DE_PREEURO", "en_BE_PREEURO", "eu_ES_PREEURO", "fr_FR_PREEURO", "gl_ES_PREEURO", "nl_NL_PREEURO", }; const char* result[]={ "\\u20A7\\u00A02", "2\\u00A0F", "IEP\\u00A01.50", "1,50\\u00A0mk", "2\\u00A0F", "ITL\\u00A02", "1$50\\u00A0\\u200B", "\\u00F6S\\u00A01,50", "1,50\\u00A0\\u0394\\u03C1\\u03C7", "2\\u00A0\\u20A7", "1,50\\u00A0FB", "IEP\\u00A01.50", "BEF\\u00A01,50", "1,50\\u00A0DM", "1,50\\u00A0BEF", "\\u20A7\\u00A02", "1,50\\u00A0F", "2\\u00A0\\u20A7", "NLG\\u00A01,50" }; log_verbose("\nTesting the number format with different currency patterns\n"); for(i=0; i < 19; i++) { char curID[256] = {0}; uloc_canonicalize(locale[i], curID, 256, &status); if(U_FAILURE(status)){ log_data_err("Could not canonicalize %s. Error: %s (Are you missing data?)\n", locale[i], u_errorName(status)); continue; } currencyFmt = unum_open(UNUM_CURRENCY, NULL,0,curID,NULL, &status); if(U_FAILURE(status)){ log_data_err("Error in the construction of number format with style currency: %s (Are you missing data?)\n", myErrorName(status)); } else { lneed=0; lneed= unum_formatDouble(currencyFmt, 1.50, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); pos.field = 0; unum_formatDouble(currencyFmt, 1.50, str, lneed+1, &pos, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status) ); } else { res=(UChar*)malloc(sizeof(UChar) * (strlen(result[i])+1) ); u_unescape(result[i],res,(int32_t)(strlen(result[i])+1)); if (u_strcmp(str, res) != 0){ log_err("FAIL: Expected %s Got: %s for locale: %s\n", result[i],aescstrdup(str, -1),locale[i]); } } } unum_close(currencyFmt); free(str); free(res); } }
static void expectItems(const USet* set, const char* items) { const char* p = items; UChar ustr[4096], itemStr[4096]; char buf[4096]; char *pat; UErrorCode ec; int32_t expectedSize = 0; int32_t itemCount = uset_getItemCount(set); int32_t itemIndex = 0; UChar32 start = 1, end = 0; int32_t itemLen = 0, length; ec = U_ZERO_ERROR; length = uset_toPattern(set, ustr, sizeof(ustr), TRUE, &ec); if (U_FAILURE(ec)) { log_err("FAIL: uset_toPattern => %s\n", u_errorName(ec)); return; } pat=aescstrdup(ustr, length); if (uset_isEmpty(set) != (strlen(items)==0)) { log_data_err("FAIL: %s should return %s from isEmpty (Are you missing data?)\n", pat, strlen(items)==0 ? "TRUE" : "FALSE"); } /* Don't test patterns starting with "[^" */ if (u_strlen(ustr) > 2 && ustr[1] == 0x5e /*'^'*/) { return; } while (*p) { ++expectedSize; if (start > end || start == -1) { /* Fetch our next item */ if (itemIndex >= itemCount) { log_data_err("FAIL: ran out of items iterating %s (Are you missing data?)\n", pat); return; } itemLen = uset_getItem(set, itemIndex, &start, &end, itemStr, sizeof(itemStr), &ec); if (U_FAILURE(ec) || itemLen < 0) { log_err("FAIL: uset_getItem => %s\n", u_errorName(ec)); return; } if (itemLen == 0) { log_verbose("Ok: %s item %d is %c-%c\n", pat, itemIndex, oneUCharToChar(start), oneUCharToChar(end)); } else { itemStr[itemLen] = 0; u_UCharsToChars(itemStr, buf, itemLen+1); log_verbose("Ok: %s item %d is \"%s\"\n", pat, itemIndex, buf); } ++itemIndex; } if (*p=='{') { const char* stringStart = ++p; int32_t stringLength = 0; char strCopy[64]; while (*p++ != '}') { } stringLength = (int32_t)(p - stringStart - 1); strncpy(strCopy, stringStart, stringLength); strCopy[stringLength] = 0; u_charsToUChars(stringStart, ustr, stringLength); ustr[stringLength] = 0; if (itemLen == 0) { log_err("FAIL: for %s expect \"%s\" next, but got a char\n", pat, strCopy); return; } if (u_strcmp(ustr, itemStr) != 0) { log_err("FAIL: for %s expect \"%s\" next\n", pat, strCopy); return; } } else { UChar32 c; u_charsToUChars(p, ustr, 1); c = ustr[0]; if (itemLen != 0) { log_err("FAIL: for %s expect '%c' next, but got a string\n", pat, *p); return; } if (c != start++) { log_err("FAIL: for %s expect '%c' next\n", pat, *p); return; } ++p; } } if (uset_size(set) == expectedSize) { log_verbose("Ok: %s size is %d\n", pat, expectedSize); } else { log_err("FAIL: %s size is %d, expected %d\n", pat, uset_size(set), expectedSize); } }
static void expectContainment(const USet* set, const char* list, UBool isIn) { const char* p = list; UChar ustr[4096]; char *pat; UErrorCode ec; int32_t rangeStart = -1, rangeEnd = -1, length; ec = U_ZERO_ERROR; length = uset_toPattern(set, ustr, sizeof(ustr), TRUE, &ec); if(U_FAILURE(ec)) { log_err("FAIL: uset_toPattern() fails in expectContainment() - %s\n", u_errorName(ec)); return; } pat=aescstrdup(ustr, length); while (*p) { if (*p=='{') { const char* stringStart = ++p; int32_t stringLength = 0; char strCopy[64]; while (*p++ != '}') { } stringLength = (int32_t)(p - stringStart - 1); strncpy(strCopy, stringStart, stringLength); strCopy[stringLength] = 0; u_charsToUChars(stringStart, ustr, stringLength); if (uset_containsString(set, ustr, stringLength) == isIn) { log_verbose("Ok: %s %s \"%s\"\n", pat, (isIn ? "contains" : "does not contain"), strCopy); } else { log_data_err("FAIL: %s %s \"%s\" (Are you missing data?)\n", pat, (isIn ? "does not contain" : "contains"), strCopy); } } else { UChar32 c; u_charsToUChars(p, ustr, 1); c = ustr[0]; if (uset_contains(set, c) == isIn) { log_verbose("Ok: %s %s '%c'\n", pat, (isIn ? "contains" : "does not contain"), *p); } else { log_data_err("FAIL: %s %s '%c' (Are you missing data?)\n", pat, (isIn ? "does not contain" : "contains"), *p); } /* Test the range API too by looking for ranges */ if (c == rangeEnd+1) { rangeEnd = c; } else { if (rangeStart >= 0) { if (uset_containsRange(set, rangeStart, rangeEnd) == isIn) { log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat, (isIn ? "contains" : "does not contain"), rangeStart, rangeEnd); } else { log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat, (isIn ? "does not contain" : "contains"), rangeStart, rangeEnd); } } rangeStart = rangeEnd = c; } ++p; } } if (rangeStart >= 0) { if (uset_containsRange(set, rangeStart, rangeEnd) == isIn) { log_verbose("Ok: %s %s U+%04X-U+%04X\n", pat, (isIn ? "contains" : "does not contain"), rangeStart, rangeEnd); } else { log_data_err("FAIL: %s %s U+%04X-U+%04X (Are you missing data?)\n", pat, (isIn ? "does not contain" : "contains"), rangeStart, rangeEnd); } } }
static void doTestVariant(UCollator* myCollation, const UChar source[], const UChar target[], UCollationResult result) { int32_t sortklen1, sortklen2, sortklenmax, sortklenmin; int temp=0, gSortklen1=0,gSortklen2=0; UCollationResult compareResult, compareResulta, keyResult, compareResultIter = result; uint8_t *sortKey1, *sortKey2, *sortKey1a, *sortKey2a; uint32_t sLen = u_strlen(source); uint32_t tLen = u_strlen(target); char buffer[256]; uint32_t len; UErrorCode status = U_ZERO_ERROR; UColAttributeValue norm = ucol_getAttribute(myCollation, UCOL_NORMALIZATION_MODE, &status); UCharIterator sIter, tIter; uiter_setString(&sIter, source, sLen); uiter_setString(&tIter, target, tLen); compareResultIter = ucol_strcollIter(myCollation, &sIter, &tIter, &status); if(compareResultIter != result) { log_err("different results in iterative comparison for UTF-16 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1)); } /* convert the strings to UTF-8 and do try comparing with char iterator */ if(QUICK <= 0) { /*!QUICK*/ char utf8Source[256], utf8Target[256]; int32_t utf8SourceLen = 0, utf8TargetLen = 0; u_strToUTF8(utf8Source, 256, &utf8SourceLen, source, sLen, &status); if(U_FAILURE(status)) { /* probably buffer is not big enough */ log_verbose("Src UTF-8 buffer too small! Will not compare!\n"); } else { u_strToUTF8(utf8Target, 256, &utf8TargetLen, target, tLen, &status); if(U_SUCCESS(status)) { /* probably buffer is not big enough */ UCollationResult compareResultUTF8 = result, compareResultUTF8Norm = result; /*UCharIterator sIter, tIter;*/ /*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/ uiter_setUTF8(&sIter, utf8Source, utf8SourceLen); uiter_setUTF8(&tIter, utf8Target, utf8TargetLen); /*uiter_setString(&sIter, source, sLen); uiter_setString(&tIter, target, tLen);*/ compareResultUTF8 = ucol_strcollIter(myCollation, &sIter, &tIter, &status); ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); sIter.move(&sIter, 0, UITER_START); tIter.move(&tIter, 0, UITER_START); compareResultUTF8Norm = ucol_strcollIter(myCollation, &sIter, &tIter, &status); ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status); if(compareResultUTF8 != compareResultIter) { log_err("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1)); } if(compareResultUTF8 != compareResultUTF8Norm) { log_err("different results in iterative when normalization is turned on with UTF-8 strings. %s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1)); } } else { log_verbose("Target UTF-8 buffer too small! Did not compare!\n"); } if(U_FAILURE(status)) { log_verbose("UTF-8 strcoll failed! Ignoring result\n"); } } } /* testing the partial sortkeys */ if(1) { /*!QUICK*/ int32_t i = 0; int32_t partialSizes[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */ int32_t partialSizesSize = 1; if(QUICK <= 0) { partialSizesSize = 7; } /*log_verbose("partial sortkey test piecesize=");*/ for(i = 0; i < partialSizesSize; i++) { UCollationResult partialSKResult = result, partialNormalizedSKResult = result; /*log_verbose("%i ", partialSizes[i]);*/ partialSKResult = compareUsingPartials(myCollation, source, sLen, target, tLen, partialSizes[i], &status); if(partialSKResult != result) { log_err("Partial sortkey comparison returned wrong result (%i exp. %i): %s, %s (size %i)\n", partialSKResult, result, aescstrdup(source,-1), aescstrdup(target,-1), partialSizes[i]); } if(QUICK <= 0 && norm != UCOL_ON) { /*log_verbose("N ");*/ ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); partialNormalizedSKResult = compareUsingPartials(myCollation, source, sLen, target, tLen, partialSizes[i], &status); ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status); if(partialSKResult != partialNormalizedSKResult) { log_err("Partial sortkey comparison gets different result when normalization is on: %s, %s (size %i)\n", aescstrdup(source,-1), aescstrdup(target,-1), partialSizes[i]); } } } /*log_verbose("\n");*/ } compareResult = ucol_strcoll(myCollation, source, sLen, target, tLen); compareResulta = ucol_strcoll(myCollation, source, -1, target, -1); if (compareResult != compareResulta) { log_err("ucol_strcoll result from null terminated and explicit length strings differs.\n"); } sortklen1=ucol_getSortKey(myCollation, source, sLen, NULL, 0); sortklen2=ucol_getSortKey(myCollation, target, tLen, NULL, 0); sortklenmax = (sortklen1>sortklen2?sortklen1:sortklen2); sortklenmin = (sortklen1<sortklen2?sortklen1:sortklen2); sortKey1 =(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1)); sortKey1a=(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1)); ucol_getSortKey(myCollation, source, sLen, sortKey1, sortklen1+1); ucol_getSortKey(myCollation, source, -1, sortKey1a, sortklen1+1); sortKey2 =(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1)); sortKey2a=(uint8_t*)malloc(sizeof(uint8_t) * (sortklenmax+1)); ucol_getSortKey(myCollation, target, tLen, sortKey2, sortklen2+1); ucol_getSortKey(myCollation, target, -1, sortKey2a, sortklen2+1); /* Check that sort key generated with null terminated string is identical */ /* to that generted with a length specified. */ if (uprv_strcmp((const char *)sortKey1, (const char *)sortKey1a) != 0 || uprv_strcmp((const char *)sortKey2, (const char *)sortKey2a) != 0 ) { log_err("Sort Keys from null terminated and explicit length strings differ.\n"); } /*memcmp(sortKey1, sortKey2,sortklenmax);*/ temp= uprv_strcmp((const char *)sortKey1, (const char *)sortKey2); gSortklen1 = uprv_strlen((const char *)sortKey1)+1; gSortklen2 = uprv_strlen((const char *)sortKey2)+1; if(sortklen1 != gSortklen1){ log_err("SortKey length does not match Expected: %i Got: %i\n",sortklen1, gSortklen1); log_verbose("Generated sortkey: %s\n", sortKeyToString(myCollation, sortKey1, buffer, &len)); } if(sortklen2!= gSortklen2){ log_err("SortKey length does not match Expected: %i Got: %i\n", sortklen2, gSortklen2); log_verbose("Generated sortkey: %s\n", sortKeyToString(myCollation, sortKey2, buffer, &len)); } if(temp < 0) { keyResult=UCOL_LESS; } else if(temp > 0) { keyResult= UCOL_GREATER; } else { keyResult = UCOL_EQUAL; } reportCResult( source, target, sortKey1, sortKey2, compareResult, keyResult, compareResultIter, result ); free(sortKey1); free(sortKey2); free(sortKey1a); free(sortKey2a); }
static void _expect(const UTransliterator* trans, const char* cfrom, const char* cto) { /* u_uastrcpy has no capacity param for the buffer -- so just * make all buffers way too big */ enum { CAP = 256 }; UChar from[CAP]; UChar to[CAP]; UChar buf[CAP]; const UChar *ID; int32_t IDLength; const char *id; UErrorCode status = U_ZERO_ERROR; int32_t limit; UTransPosition pos; XReplaceable xrep; XReplaceable *xrepPtr = &xrep; UReplaceableCallbacks xrepVtable; u_uastrcpy(from, cfrom); u_uastrcpy(to, cto); ID = utrans_getUnicodeID(trans, &IDLength); id = aescstrdup(ID, IDLength); /* utrans_transUChars() */ u_strcpy(buf, from); limit = u_strlen(buf); utrans_transUChars(trans, buf, NULL, CAP, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } /* utrans_transIncrementalUChars() */ u_strcpy(buf, from); pos.start = pos.contextStart = 0; pos.limit = pos.contextLimit = u_strlen(buf); utrans_transIncrementalUChars(trans, buf, NULL, CAP, &pos, &status); utrans_transUChars(trans, buf, NULL, CAP, pos.start, &pos.limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transIncrementalUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transIncrementalUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transIncrementalUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } /* utrans_trans() */ InitXReplaceableCallbacks(&xrepVtable); InitXReplaceable(&xrep, cfrom); limit = u_strlen(from); utrans_trans(trans, (UReplaceable*)xrepPtr, &xrepVtable, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_trans() failed, error=%s\n", u_errorName(status)); FreeXReplaceable(&xrep); return; } if (0 == u_strcmp(xrep.text, to)) { log_verbose("Ok: utrans_trans(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, xrep.text); log_err("FAIL: utrans_trans(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } FreeXReplaceable(&xrep); /* utrans_transIncremental() */ InitXReplaceable(&xrep, cfrom); pos.start = pos.contextStart = 0; pos.limit = pos.contextLimit = u_strlen(from); utrans_transIncremental(trans, (UReplaceable*)xrepPtr, &xrepVtable, &pos, &status); utrans_trans(trans, (UReplaceable*)xrepPtr, &xrepVtable, pos.start, &pos.limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transIncremental() failed, error=%s\n", u_errorName(status)); FreeXReplaceable(&xrep); return; } if (0 == u_strcmp(xrep.text, to)) { log_verbose("Ok: utrans_transIncremental(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, xrep.text); log_err("FAIL: utrans_transIncremental(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } FreeXReplaceable(&xrep); }
static void TestFilter() { UErrorCode status = U_ZERO_ERROR; UChar filt[128]; UChar buf[128]; UChar exp[128]; char *cbuf; int32_t limit; const char* DATA[] = { "[^c]", /* Filter out 'c' */ "abcde", "\\u0061\\u0062c\\u0064\\u0065", "", /* No filter */ "abcde", "\\u0061\\u0062\\u0063\\u0064\\u0065" }; int32_t DATA_length = sizeof(DATA) / sizeof(DATA[0]); int32_t i; UTransliterator* hex = utrans_open("Any-Hex", UTRANS_FORWARD, NULL,0,NULL,&status); if (hex == 0 || U_FAILURE(status)) { log_err("FAIL: utrans_open(Unicode-Hex) failed, error=%s\n", u_errorName(status)); goto exit; } for (i=0; i<DATA_length; i+=3) { /*u_uastrcpy(filt, DATA[i]);*/ u_charsToUChars(DATA[i], filt, (int32_t)strlen(DATA[i])+1); utrans_setFilter(hex, filt, -1, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_setFilter() failed, error=%s\n", u_errorName(status)); goto exit; } /*u_uastrcpy(buf, DATA[i+1]);*/ u_charsToUChars(DATA[i+1], buf, (int32_t)strlen(DATA[i+1])+1); limit = 5; utrans_transUChars(hex, buf, NULL, 128, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transUChars() failed, error=%s\n", u_errorName(status)); goto exit; } cbuf=aescstrdup(buf, -1); u_charsToUChars(DATA[i+2], exp, (int32_t)strlen(DATA[i+2])+1); if (0 == u_strcmp(buf, exp)) { log_verbose("Ok: %s | %s -> %s\n", DATA[i+1], DATA[i], cbuf); } else { log_err("FAIL: %s | %s -> %s, expected %s\n", DATA[i+1], DATA[i], cbuf, DATA[i+2]); } } exit: utrans_close(hex); }
static void TestUnicodeIDs() { UEnumeration *uenum; UTransliterator *utrans; const UChar *id, *id2; int32_t idLength, id2Length, count, count2; UErrorCode errorCode; errorCode=U_ZERO_ERROR; uenum=utrans_openIDs(&errorCode); if(U_FAILURE(errorCode)) { log_err("utrans_openIDs() failed - %s\n", u_errorName(errorCode)); return; } count=uenum_count(uenum, &errorCode); if(U_FAILURE(errorCode) || count<1) { log_err("uenum_count(transliterator IDs)=%d - %s\n", count, u_errorName(errorCode)); } count=0; for(;;) { id=uenum_unext(uenum, &idLength, &errorCode); if(U_FAILURE(errorCode)) { log_err("uenum_unext(transliterator ID %d) failed - %s\n", count, u_errorName(errorCode)); break; } if(id==NULL) { break; } if(++count>10) { /* try to actually open only a few transliterators */ continue; } utrans=utrans_openU(id, idLength, UTRANS_FORWARD, NULL, 0, NULL, &errorCode); if(U_FAILURE(errorCode)) { log_err("utrans_openU(%s) failed - %s\n", aescstrdup(id, idLength), u_errorName(errorCode)); continue; } id2=utrans_getUnicodeID(utrans, &id2Length); if(idLength!=id2Length || 0!=u_memcmp(id, id2, idLength)) { log_err("utrans_getUnicodeID(%s) does not match the original ID\n", aescstrdup(id, idLength)); } utrans_close(utrans); } uenum_reset(uenum, &errorCode); if(U_FAILURE(errorCode) || count<1) { log_err("uenum_reset(transliterator IDs) failed - %s\n", u_errorName(errorCode)); } else { count2=uenum_count(uenum, &errorCode); if(U_FAILURE(errorCode) || count<1) { log_err("2nd uenum_count(transliterator IDs)=%d - %s\n", count2, u_errorName(errorCode)); } else if(count!=count2) { log_err("uenum_unext(transliterator IDs) returned %d IDs but uenum_count() after uenum_reset() claims there are %d\n", count, count2); } } uenum_close(uenum); }
void TestCzechMonths459() { int32_t lneed, pos; UChar *pattern=NULL, *tzID=NULL; UChar *juneStr, *julyStr; UDateFormat *fmt; UCalendar *cal; UDate june, july, d; UErrorCode status = U_ZERO_ERROR; UChar *date; ctest_setTimeZone(NULL, &status); fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)){ log_data_err("Error in constructing the date format -> %s (Are you missing data?)\n", u_errorName(status)); ctest_resetTimeZone(); return; } lneed=0; lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); udat_toPattern(fmt, TRUE, pattern, lneed+1, &status); } if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); } tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "GMT"); cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status); if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); } ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status); june=ucal_getMillis(cal, &status); ucal_setDate(cal, 1997, UCAL_JULY, 15, &status); july=ucal_getMillis(cal, &status); juneStr = myDateFormat(fmt, june); julyStr = myDateFormat(fmt, july); pos=0; if(juneStr == NULL) { log_data_err("Can't load juneStr. Quitting.\n"); return; } d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status); date = myDateFormat(fmt, d); if(U_SUCCESS(status)){ UChar* out1 = myDateFormat(fmt, june); UChar* out2 = myDateFormat(fmt, d); if(u_strcmp(out1, out2) !=0) log_err("Error in handling the czech month june\n"); else log_verbose("Pass: Date = %s (czech month June)\n", aescstrdup(date, -1)); }else{ log_err("udat_parse failed. Error. %s\n",u_errorName(status)); } pos=0; d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status); date = myDateFormat(fmt, d); if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0) log_err("Error in handling the czech month july\n"); else log_verbose("Pass: Date = %s (czech month July)\n", aescstrdup(date, -1)); ctest_resetTimeZone(); udat_close(fmt); ucal_close(cal); free(pattern); free(tzID); }