void genericOrderingTestWithResult(UCollator *coll, const char * const s[], uint32_t size, UCollationResult result) { UChar t1[2048] = {0}; UChar t2[2048] = {0}; UCollationElements *iter; UErrorCode status = U_ZERO_ERROR; uint32_t i = 0, j = 0; log_verbose("testing sequence:\n"); for(i = 0; i < size; i++) { log_verbose("%s\n", s[i]); } iter = ucol_openElements(coll, t1, u_strlen(t1), &status); if (U_FAILURE(status)) { log_err("Creation of iterator failed\n"); } for(i = 0; i < size-1; i++) { for(j = i+1; j < size; j++) { u_unescape(s[i], t1, 2048); u_unescape(s[j], t2, 2048); doTest(coll, t1, t2, result); /* synwee : added collation element iterator test */ ucol_setText(iter, t1, u_strlen(t1), &status); backAndForth(iter); ucol_setText(iter, t2, u_strlen(t2), &status); backAndForth(iter); } } ucol_closeElements(iter); }
static void TestFractionDigitOverride(void) { UErrorCode status = U_ZERO_ERROR; UNumberFormat *fmt = unum_open(UNUM_CURRENCY, NULL, 0, "hu_HU", NULL, &status); UChar buffer[256]; UChar expectedBuf[256]; const char expectedFirst[] = "123\\u00A0Ft"; const char expectedSecond[] = "123,46\\u00A0Ft"; const char expectedThird[] = "123,456\\u00A0Ft"; if (U_FAILURE(status)) { log_data_err("Error: unum_open returned %s (Are you missing data?)\n", myErrorName(status)); return; } /* Make sure that you can format normal fraction digits. */ unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); u_unescape(expectedFirst, expectedBuf, strlen(expectedFirst)+1); if (u_strcmp(buffer, expectedBuf) != 0) { log_err("Error: unum_formatDouble didn't return %s\n", expectedFirst); } /* Make sure that you can format 2 fraction digits. */ unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 2); unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); u_unescape(expectedSecond, expectedBuf, strlen(expectedSecond)+1); if (u_strcmp(buffer, expectedBuf) != 0) { log_err("Error: unum_formatDouble didn't return %s\n", expectedSecond); } /* Make sure that you can format more fraction digits. */ unum_setAttribute(fmt, UNUM_FRACTION_DIGITS, 3); unum_formatDouble(fmt, 123.456, buffer, sizeof(buffer)/sizeof(buffer[0]), NULL, &status); u_unescape(expectedThird, expectedBuf, strlen(expectedThird)+1); if (u_strcmp(buffer, expectedBuf) != 0) { log_err("Error: unum_formatDouble didn't return %s\n", expectedThird); } unum_close(fmt); }
static void TestDataVariantsCompounds() { const TransIDSourceTarg* itemsPtr; for (itemsPtr = dataVarCompItems; itemsPtr->transID != NULL; itemsPtr++) { UErrorCode status = U_ZERO_ERROR; UChar utrid[kUBufMax]; int32_t utridlen = u_unescape(itemsPtr->transID, utrid, kUBufMax); UTransliterator* utrans = utrans_openU(utrid, utridlen, UTRANS_FORWARD, NULL, 0, NULL, &status); if (U_FAILURE(status)) { log_data_err("FAIL: utrans_openRules(%s) failed, error=%s (Are you missing data?)\n", itemsPtr->transID, u_errorName(status)); continue; } UChar text[kUBufMax]; int32_t textLen = u_unescape(itemsPtr->sourceText, text, kUBufMax); int32_t textLim = textLen; utrans_transUChars(utrans, text, &textLen, kUBufMax, 0, &textLim, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transUChars(%s) failed, error=%s\n", itemsPtr->transID, u_errorName(status)); } else { UChar expect[kUBufMax]; int32_t expectLen = u_unescape(itemsPtr->targetText, expect, kUBufMax); if (textLen != expectLen || u_strncmp(text, expect, textLen) != 0) { char btext[kBBufMax], bexpect[kBBufMax]; u_austrncpy(btext, text, textLen); u_austrncpy(bexpect, expect, expectLen); log_err("FAIL: utrans_transUChars(%s),\n expect %s\n get %s\n", itemsPtr->transID, bexpect, btext); } } utrans_close(utrans); } }
UChar* CharsToUChars(const char* str) { /* Might be faster to just use uprv_strlen() as the preflight len - liu */ int32_t len = u_unescape(str, 0, 0); /* preflight */ /* Do NOT use malloc() - we are supposed to be acting like user code! */ UChar *buf = (UChar*) malloc(sizeof(UChar) * (len + 1)); u_unescape(str, buf, len + 1); return buf; }
static void TestCombineDateTime() { const CombineDateTimeTestItem *itemPtr; log_verbose("\nTesting ureldatefmt_combineDateAndTime() with various parameters\n"); for (itemPtr = combTestItems; itemPtr->locale != NULL; itemPtr++) { URelativeDateTimeFormatter *reldatefmt = NULL; UErrorCode status = U_ZERO_ERROR; UChar ubufreldate[kUBufMax]; UChar ubuftime[kUBufMax]; UChar ubufget[kUBufMax]; int32_t ulenreldate, ulentime, ulenget; reldatefmt = ureldatefmt_open(itemPtr->locale, NULL, itemPtr->width, itemPtr->capContext, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: ureldatefmt_open() for locale %s, width %d, capContext %d: %s\n", itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); continue; } ulenreldate = u_unescape(itemPtr->relativeDateString, ubufreldate, kUBufMax); ulentime = u_unescape(itemPtr->timeString, ubuftime, kUBufMax); ulenget = ureldatefmt_combineDateAndTime(reldatefmt, ubufreldate, ulenreldate, ubuftime, ulentime, ubufget, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("FAIL: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d: %s\n", itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); } else { UChar ubufexp[kUBufMax]; int32_t ulenexp = u_unescape(itemPtr->expectedResult, ubufexp, kUBufMax); if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { char bbufget[kBBufMax]; u_austrncpy(bbufget, ubufget, kUBufMax); log_err("ERROR: ureldatefmt_combineDateAndTime() for locale %s, width %d, capContext %d;\n expected %s\n get %s\n", itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, itemPtr->expectedResult, bbufget ); } } // preflight test status = U_ZERO_ERROR; ulenget = ureldatefmt_combineDateAndTime(reldatefmt, ubufreldate, ulenreldate, ubuftime, ulentime, NULL, 0, &status); if ( status != U_BUFFER_OVERFLOW_ERROR) { log_err("FAIL: ureldatefmt_combineDateAndTime() preflight for locale %s, width %d, capContext %d: expected U_BUFFER_OVERFLOW_ERROR, got %s\n", itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); } else { UChar ubufexp[kUBufMax]; int32_t ulenexp = u_unescape(itemPtr->expectedResult, ubufexp, kUBufMax); if (ulenget != ulenexp) { log_err("ERROR: ureldatefmt_combineDateAndTime() preflight for locale %s, width %d, capContext %d;\n expected len %d, get len %d\n", itemPtr->locale, (int)itemPtr->width, (int)itemPtr->capContext, ulenexp, ulenget ); } } ureldatefmt_close(reldatefmt); } }
static void TestDateIntervalFormat() { const DateIntervalFormatTestItem * testItemPtr; UErrorCode status = U_ZERO_ERROR; ctest_setTimeZone(NULL, &status); log_verbose("\nTesting udtitvfmt_open() and udtitvfmt_format() with various parameters\n"); for ( testItemPtr = testItems; testItemPtr->locale != NULL; ++testItemPtr ) { UDateIntervalFormat* udtitvfmt; int32_t tzidLen; UChar skelBuf[kSkelBufLen]; UChar tzidBuf[kTZIDBufLen]; const char * tzidForLog = (testItemPtr->tzid)? testItemPtr->tzid: "NULL"; status = U_ZERO_ERROR; u_unescape(testItemPtr->skeleton, skelBuf, kSkelBufLen); if ( testItemPtr->tzid ) { u_unescape(testItemPtr->tzid, tzidBuf, kTZIDBufLen); tzidLen = -1; } else { tzidLen = 0; } udtitvfmt = udtitvfmt_open(testItemPtr->locale, skelBuf, -1, tzidBuf, tzidLen, &status); if ( U_SUCCESS(status) ) { UChar result[kFormatBufLen]; UChar resultExpected[kFormatBufLen]; int32_t fmtLen = udtitvfmt_format(udtitvfmt, testItemPtr->from, testItemPtr->to, result, kFormatBufLen, NULL, &status); if (fmtLen >= kFormatBufLen) { result[kFormatBufLen-1] = 0; } if ( U_SUCCESS(status) ) { u_unescape(testItemPtr->resultExpected, resultExpected, kFormatBufLen); if ( u_strcmp(result, resultExpected) != 0 ) { char bcharBuf[kFormatBufLen]; log_err("ERROR: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: expect %s, get %s\n", testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to, testItemPtr->resultExpected, u_austrcpy(bcharBuf,result) ); } } else { log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, tzid %s, from %.1f, to %.1f: %s\n", testItemPtr->locale, testItemPtr->skeleton, tzidForLog, testItemPtr->from, testItemPtr->to, myErrorName(status) ); } udtitvfmt_close(udtitvfmt); } else { log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s, tzid %s - %s\n", testItemPtr->locale, testItemPtr->skeleton, tzidForLog, myErrorName(status) ); } } ctest_resetTimeZone(); }
/** * ICU string comparison */ int strcmp() { UChar source[100]; UChar target[100]; u_unescape(opt_source, source, 100); u_unescape(opt_target, target, 100); UCollationResult result = ucol_strcoll(collator, source, -1, target, -1); if (result == UCOL_LESS) { return -1; } else if (result == UCOL_GREATER) { return 1; } return 0; }
static void TestIDNToASCII(){ int32_t i; UChar buf[MAX_DEST_SIZE]; UChar expected[MAX_DEST_SIZE]; UErrorCode status = U_ZERO_ERROR; int32_t bufLen = 0; UParseError parseError; const char* testName="udina_IDNToASCII"; TestFunc func=uidna_IDNToASCII; for(i=0;i< (int32_t)(sizeof(domainNames)/sizeof(domainNames[0])); i++){ bufLen = (int32_t)strlen(domainNames[i]); bufLen = u_unescape(domainNames[i],buf, bufLen+1); func(buf,bufLen,expected,MAX_DEST_SIZE, UIDNA_ALLOW_UNASSIGNED, &parseError,&status); if(U_FAILURE(status)){ log_err_status(status, "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status)); break; } testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, TRUE, TRUE, func); /*test toASCII with all labels in the string*/ testAPI(buf,expected,testName, FALSE,U_ZERO_ERROR, FALSE, TRUE, func); if(U_FAILURE(status)){ log_err( "%s failed to convert domainNames[%i].Error: %s \n",testName,i, u_errorName(status)); break; } } }
static void TestJB5273(){ static const char INVALID_DOMAIN_NAME[] = "xn--m\\u00FCller.de"; UChar invalid_idn[25] = {'\0'}; int32_t len = u_unescape(INVALID_DOMAIN_NAME, invalid_idn, strlen(INVALID_DOMAIN_NAME)); UChar output[50] = {'\0'}; UErrorCode status = U_ZERO_ERROR; UParseError prsError; int32_t outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status)); } status = U_ZERO_ERROR; outLen = uidna_toUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status)); } status = U_ZERO_ERROR; outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_DEFAULT, &prsError, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status)); } status = U_ZERO_ERROR; outLen = uidna_IDNToUnicode(invalid_idn, len, output, 50, UIDNA_USE_STD3_RULES, &prsError, &status); if(U_FAILURE(status)){ log_err_status(status, "uidna_toUnicode failed with error: %s\n", u_errorName(status)); } }
static void demoUnicodeStringInit() { // *** Make sure to read about invariant characters in utypes.h! *** // Initialization of Unicode strings from C literals works _only_ for // invariant characters! printf("\n* demoUnicodeStringInit() ---------- ***\n\n"); // the string literal is 32 chars long - this must be counted for the macro UnicodeString invariantOnly=UNICODE_STRING("such characters are safe 123 %-.", 32); /* * In C, we need two macros: one to declare the UChar[] array, and * one to populate it; the second one is a noop on platforms where * wchar_t is compatible with UChar and ASCII-based. * The length of the string literal must be counted for both macros. */ /* declare the invString array for the string */ U_STRING_DECL(invString, "such characters are safe 123 %-.", 32); /* populate it with the characters */ U_STRING_INIT(invString, "such characters are safe 123 %-.", 32); // compare the C and C++ strings printf("C and C++ Unicode strings are equal: %d\n", invariantOnly==UnicodeString(TRUE, invString, 32)); /* * convert between char * and UChar * strings that * contain only invariant characters */ static const char *cs1="such characters are safe 123 %-."; static UChar us1[40]; static char cs2[40]; u_charsToUChars(cs1, us1, 33); /* include the terminating NUL */ u_UCharsToChars(us1, cs2, 33); printf("char * -> UChar * -> char * with only " "invariant characters: \"%s\"\n", cs2); // initialize a UnicodeString from a string literal that contains // escape sequences written with invariant characters // do not forget to duplicate the backslashes for ICU to see them // then, count each double backslash only once! UnicodeString german=UNICODE_STRING( "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", 64). unescape(); printUnicodeString("german UnicodeString from unescaping:\n ", german); /* * C: convert and unescape a char * string with only invariant * characters to fill a UChar * string */ UChar buffer[200]; int32_t length; length=u_unescape( "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", buffer, UPRV_LENGTHOF(buffer)); printf("german C Unicode string from char * unescaping: (length %d)\n ", length); printUnicodeString("", UnicodeString(buffer)); }
U_CDECL_END #define LINES 6 void CollationThaiTest::TestInvalidThai(void) { const char *tests[LINES] = { "\\u0E44\\u0E01\\u0E44\\u0E01", "\\u0E44\\u0E01\\u0E01\\u0E44", "\\u0E01\\u0E44\\u0E01\\u0E44", "\\u0E01\\u0E01\\u0E44\\u0E44", "\\u0E44\\u0E44\\u0E01\\u0E01", "\\u0E01\\u0E44\\u0E44\\u0E01", }; UChar strings[LINES][20]; UChar *toSort[LINES]; int32_t i = 0, j = 0, len = 0; UErrorCode coll_status = U_ZERO_ERROR; UnicodeString iteratorText; thaiColl = ucol_open ("th_TH", &coll_status); if (U_FAILURE(coll_status)) { errln("Error opening Thai collator: %s", u_errorName(coll_status)); return; } CollationElementIterator* c = ((RuleBasedCollator *)coll)->createCollationElementIterator( iteratorText ); for(i = 0; i < (int32_t)(sizeof(tests)/sizeof(tests[0])); i++) { len = u_unescape(tests[i], strings[i], 20); strings[i][len] = 0; toSort[i] = strings[i]; } qsort (toSort, LINES, sizeof (UChar *), StrCmp); for (i=0; i < LINES; i++) { logln("%i", i); for (j=i+1; j < LINES; j++) { if (ucol_strcoll (thaiColl, toSort[i], -1, toSort[j], -1) == UCOL_GREATER) { // inconsistency ordering found! errln("Inconsistent ordering between strings %i and %i", i, j); } } iteratorText.setTo(toSort[i]); c->setText(iteratorText, coll_status); backAndForth(*c); } ucol_close(thaiColl); delete c; }
//int main(int argc, const char * const argv[]) { int main(int, const char * const *) { static const char* test[4] = { "\\u304D\\u3085\\u3046\\u0000", "\\u30AD\\u30E6\\u30A6\\u0000", "\\u304D\\u3086\\u3046\\u0000", "\\u30AD\\u30E5\\u30A6\\u0000" }; #if 0 static const char* test2[4] = { "dbc\\u0000", "cbc\\u0000", "bbc\\u0000", "abc\\u0000" }; #endif static UChar uTest[4][5]; static uint8_t keys[4][32]; uint32_t i = 0; for(i = 0; i<4; i++) { u_unescape(test[i], uTest[i], 5); } printArray("Before current", uTest, 4); test_current(uTest, 4, 5, keys); printArray("After current", uTest, 4); printKeys("Current keys", keys, 4); for(i = 0; i<4; i++) { u_unescape(test[i], uTest[i], 5); } printArray("Before legacy", uTest, 4); test_legacy(uTest, 4, 5, keys); printArray("After legacy", uTest, 4); printKeys("Legacy keys", keys, 4); return 0; }
/* Test case for internal API u_caseInsensitivePrefixMatch */ static void TestUCaseInsensitivePrefixMatch(void) { struct { const char *s1; const char *s2; int32_t r1; int32_t r2; } testCases[] = { {"ABC", "ab", 2, 2}, {"ABCD", "abcx", 3, 3}, {"ABC", "xyz", 0, 0}, /* U+00DF LATIN SMALL LETTER SHARP S */ {"A\\u00dfBC", "Ass", 2, 3}, {"Fust", "Fu\\u00dfball", 2, 2}, {"\\u00dfsA", "s\\u00dfB", 2, 2}, {"\\u00dfs", "s\\u00df", 2, 2}, /* U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE */ {"XYZ\\u0130i\\u0307xxx", "xyzi\\u0307\\u0130yyy", 6, 6}, {0, 0, 0, 0} }; int32_t i; for (i = 0; testCases[i].s1 != 0; i++) { UErrorCode sts = U_ZERO_ERROR; UChar u1[64], u2[64]; int32_t matchLen1, matchLen2; u_unescape(testCases[i].s1, u1, 64); u_unescape(testCases[i].s2, u2, 64); u_caseInsensitivePrefixMatch(u1, -1, u2, -1, 0, &matchLen1, &matchLen2, &sts); if (U_FAILURE(sts)) { log_err("error: %s, s1=%s, s2=%s", u_errorName(sts), testCases[i].s1, testCases[i].s2); } else if (matchLen1 != testCases[i].r1 || matchLen2 != testCases[i].r2) { log_err("s1=%s, s2=%2 / match len1=%d, len2=%d / expected len1=%d, len2=%d", testCases[i].s1, testCases[i].s2, matchLen1, matchLen2, testCases[i].r1, testCases[i].r2); } } }
/** * Make sure that when non-invariant chars are passed to uset_openPattern * they do not cause an ugly failure mode (e.g. assertion failure). * JB#3795. */ static void TestNonInvariantPattern() { UErrorCode ec = U_ZERO_ERROR; /* The critical part of this test is that the following pattern must contain a non-invariant character. */ static const char *pattern = "[:ccc!=0:]"; UChar buf[256]; int32_t len = u_unescape(pattern, buf, 256); /* This test 'fails' by having an assertion failure within the following call. It passes by running to completion with no assertion failure. */ USet *set = uset_openPattern(buf, len, &ec); uset_close(set); }
/** * Creates a string search */ UBool processStringSearch() { u_unescape(opt_source, source, 100); u_unescape(opt_pattern, pattern, 100); UErrorCode status = U_ZERO_ERROR; search = usearch_openFromCollator(pattern, -1, source, -1, collator, NULL, &status); if (U_FAILURE(status)) { return FALSE; } if (opt_overlap == TRUE) { usearch_setAttribute(search, USEARCH_OVERLAP, USEARCH_ON, &status); } if (opt_canonical == TRUE) { usearch_setAttribute(search, USEARCH_CANONICAL_MATCH, USEARCH_ON, &status); } if (U_FAILURE(status)) { fprintf(stderr, "Error setting search attributes\n"); return FALSE; } return TRUE; }
void genericRulesStarterWithResult(const char *rules, const char * const s[], uint32_t size, UCollationResult result) { UErrorCode status = U_ZERO_ERROR; UChar rlz[RULE_BUFFER_LEN] = { 0 }; uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN); UCollator *coll = NULL; coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status); log_verbose("Rules starter for %s\n", rules); if(U_SUCCESS(status)) { genericOrderingTestWithResult(coll, s, size, result); ucol_close(coll); } else if(status == U_FILE_ACCESS_ERROR) { log_data_err("Is your data around?\n"); } else { log_err("Unable to open collator with rules %s\n", rules); } }
/** * 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 TestFPos_SkelWithSeconds() { const LocaleAndSkeletonItem * locSkelItemPtr; for (locSkelItemPtr = locSkelItems; locSkelItemPtr->locale != NULL; locSkelItemPtr++) { UDateIntervalFormat* udifmt; UChar ubuf[kSizeUBuf]; int32_t ulen, uelen; UErrorCode status = U_ZERO_ERROR; u_strFromUTF8(ubuf, kSizeUBuf, &ulen, locSkelItemPtr->skeleton, -1, &status); udifmt = udtitvfmt_open(locSkelItemPtr->locale, ubuf, ulen, zoneGMT, -1, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: udtitvfmt_open for locale %s, skeleton %s: %s\n", locSkelItemPtr->locale, locSkelItemPtr->skeleton, u_errorName(status)); } else { const double * deltasPtr = deltas; const ExpectPosAndFormat * expectedPtr = locSkelItemPtr->expected; for (; *deltasPtr >= 0.0; deltasPtr++, expectedPtr++) { UFieldPosition fpos = { locSkelItemPtr->fieldToCheck, 0, 0 }; UChar uebuf[kSizeUBuf]; char bbuf[kSizeBBuf]; char bebuf[kSizeBBuf]; status = U_ZERO_ERROR; uelen = u_unescape(expectedPtr->format, uebuf, kSizeUBuf); ulen = udtitvfmt_format(udifmt, startTime, startTime + *deltasPtr, ubuf, kSizeUBuf, &fpos, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %.1f: %s\n", locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr, u_errorName(status)); } else if ( ulen != uelen || u_strncmp(ubuf,uebuf,uelen) != 0 || fpos.beginIndex != expectedPtr->posBegin || fpos.endIndex != expectedPtr->posEnd ) { u_strToUTF8(bbuf, kSizeBBuf, NULL, ubuf, ulen, &status); u_strToUTF8(bebuf, kSizeBBuf, NULL, uebuf, uelen, &status); // convert back to get unescaped string log_err("FAIL: udtitvfmt_format for locale %s, skeleton %s, delta %12.1f, expect %d-%d \"%s\", get %d-%d \"%s\"\n", locSkelItemPtr->locale, locSkelItemPtr->skeleton, *deltasPtr, expectedPtr->posBegin, expectedPtr->posEnd, bebuf, fpos.beginIndex, fpos.endIndex, bbuf); } } udtitvfmt_close(udifmt); } } }
/* currently not used with options */ void genericRulesStarterWithOptionsAndResult(const char *rules, const char * const s[], uint32_t size, const UColAttribute *attrs, const UColAttributeValue *values, uint32_t attsize, UCollationResult result) { UErrorCode status = U_ZERO_ERROR; UChar rlz[RULE_BUFFER_LEN] = { 0 }; uint32_t rlen = u_unescape(rules, rlz, RULE_BUFFER_LEN); uint32_t i; UCollator *coll = ucol_openRules(rlz, rlen, UCOL_DEFAULT, UCOL_DEFAULT,NULL, &status); log_verbose("Rules starter for %s\n", rules); if(U_SUCCESS(status)) { log_verbose("Setting attributes\n"); for(i = 0; i < attsize; i++) { ucol_setAttribute(coll, attrs[i], values[i], &status); } genericOrderingTestWithResult(coll, s, size, result); } else { log_err("Unable to open collator with rules %s\n", rules); } ucol_close(coll); }
/* Testing the DateFormat API */ static void TestDateFormat() { UDateFormat *def, *fr, *it, *de, *def1, *fr_pat; UDateFormat *any; UDateFormat *copy; UErrorCode status = U_ZERO_ERROR; UChar* result = NULL; const UCalendar *cal; const UNumberFormat *numformat1, *numformat2; UChar temp[50]; int32_t numlocales; UDate d1; int i; int32_t resultlength; int32_t resultlengthneeded; int32_t parsepos; UDate d = 837039928046.0; double num = -10456.37; /*const char* str="yyyy.MM.dd G 'at' hh:mm:ss z"; const char t[]="2/3/76 2:50 AM";*/ /*Testing udat_open() to open a dateformat */ ctest_setTimeZone(NULL, &status); log_verbose("\nTesting udat_open() with various parameters\n"); fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL,0, NULL, 0,&status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using full time style with french locale -> %s (Are you missing data?)\n", myErrorName(status) ); return; } /* this is supposed to open default date format, but later on it treats it like it is "en_US" - very bad if you try to run the tests on machine where default locale is NOT "en_US" */ /* def = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0, &status); */ def = udat_open(UDAT_SHORT, UDAT_SHORT, "en_US", NULL, 0,NULL, 0, &status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", myErrorName(status) ); return; } it = udat_open(UDAT_DEFAULT, UDAT_MEDIUM, "it_IT", NULL, 0, NULL, 0,&status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using medium date style with italian locale\n %s\n", myErrorName(status) ); return; } de = udat_open(UDAT_LONG, UDAT_LONG, "de_DE", NULL, 0, NULL, 0,&status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using long time and date styles with german locale\n %s\n", myErrorName(status)); return; } /*creating a default dateformat */ def1 = udat_open(UDAT_SHORT, UDAT_SHORT, NULL, NULL, 0,NULL, 0, &status); if(U_FAILURE(status)) { log_err("FAIL: error in creating the dateformat using short date and time style\n %s\n", myErrorName(status) ); return; } /*Testing udat_getAvailable() and udat_countAvailable()*/ log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); numlocales=udat_countAvailable(); /* use something sensible w/o hardcoding the count */ if(numlocales < 0) log_data_err("FAIL: error in countAvailable\n"); log_verbose("The number of locales for which date/time formatting patterns are available is %d\n", numlocales); for(i=0;i<numlocales;i++) { UErrorCode subStatus = U_ZERO_ERROR; log_verbose("Testing open of %s\n", udat_getAvailable(i)); any = udat_open(UDAT_SHORT, UDAT_SHORT, udat_getAvailable(i), NULL ,0, NULL, 0, &subStatus); if(U_FAILURE(subStatus)) { log_data_err("FAIL: date format %s (getAvailable(%d)) is not instantiable: %s\n", udat_getAvailable(i), i, u_errorName(subStatus)); } udat_close(any); } /*Testing udat_clone()*/ log_verbose("\nTesting the udat_clone() function of date format\n"); copy=udat_clone(def, &status); if(U_FAILURE(status)){ log_err("Error in creating the clone using udat_clone: %s\n", myErrorName(status) ); } /*if(def != copy) log_err("Error in udat_clone");*/ /*how should i check for equality???? */ /*Testing udat_format()*/ log_verbose("\nTesting the udat_format() function of date format\n"); u_uastrcpy(temp, "7/10/96 4:05 PM"); /*format using def */ resultlength=0; resultlengthneeded=udat_format(def, d, NULL, resultlength, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded+1; if(result != NULL) { free(result); result = NULL; } result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_format(def, d, result, resultlength, NULL, &status); } if(U_FAILURE(status) || !result) { log_err("FAIL: Error in formatting using udat_format(.....) %s\n", myErrorName(status) ); return; } else log_verbose("PASS: formatting successful\n"); if(u_strcmp(result, temp)==0) log_verbose("PASS: Date Format for US locale successful using udat_format()\n"); else { char xbuf[2048]; char gbuf[2048]; u_austrcpy(xbuf, temp); u_austrcpy(gbuf, result); log_err("FAIL: Date Format for US locale failed using udat_format() - expected %s got %s\n", xbuf, gbuf); } /*format using fr */ u_unescape("10 juil. 1996 16:05:28 heure avanc\\u00E9e du Pacifique", temp, 50); if(result != NULL) { free(result); result = NULL; } result=myDateFormat(fr, d); if(u_strcmp(result, temp)==0) log_verbose("PASS: Date Format for french locale successful using udat_format()\n"); else log_data_err("FAIL: Date Format for french locale failed using udat_format().\n" ); /*format using it */ u_uastrcpy(temp, "10/lug/1996 16:05:28"); { UChar *fmtted; char g[100]; char x[100]; fmtted = myDateFormat(it,d); u_austrcpy(g, fmtted); u_austrcpy(x, temp); if(u_strcmp(fmtted, temp)==0) { log_verbose("PASS: Date Format for italian locale successful uisng udat_format() - wanted %s, got %s\n", x, g); } else { log_data_err("FAIL: Date Format for italian locale failed using udat_format() - wanted %s, got %s\n", x, g); } } /*Testing parsing using udat_parse()*/ log_verbose("\nTesting parsing using udat_parse()\n"); u_uastrcpy(temp,"2/3/76 2:50 AM"); parsepos=0; status=U_ZERO_ERROR; d1=udat_parse(def, temp, u_strlen(temp), &parsepos, &status); if(U_FAILURE(status)) { log_err("FAIL: Error in parsing using udat_parse(.....) %s\n", myErrorName(status) ); } else log_verbose("PASS: parsing succesful\n"); /*format it back and check for equality */ if(u_strcmp(myDateFormat(def, d1),temp)!=0) log_err("FAIL: error in parsing\n"); /*Testing parsing using udat_parse()*/ log_verbose("\nTesting parsing using udat_parse()\n"); u_uastrcpy(temp,"2/Don't parse this part"); status=U_ZERO_ERROR; d1=udat_parse(def, temp, u_strlen(temp), NULL, &status); if(status != U_PARSE_ERROR) { log_err("FAIL: udat_parse(\"bad string\") passed when it should have failed\n"); } else log_verbose("PASS: parsing succesful\n"); /*Testing udat_openPattern() */ status=U_ZERO_ERROR; log_verbose("\nTesting the udat_openPattern with a specified pattern\n"); /*for french locale */ fr_pat=udat_open(UDAT_IGNORE, UDAT_IGNORE,"fr_FR",NULL,0,temp, u_strlen(temp), &status); if(U_FAILURE(status)) { log_err("FAIL: Error in creating a date format using udat_openPattern \n %s\n", myErrorName(status) ); } else log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n"); /*Testing applyPattern and toPattern */ log_verbose("\nTesting applyPattern and toPattern()\n"); udat_applyPattern(def1, FALSE, temp, u_strlen(temp)); log_verbose("Extracting the pattern\n"); resultlength=0; resultlengthneeded=udat_toPattern(def1, FALSE, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded + 1; result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_toPattern(def1, FALSE, result, resultlength, &status); } if(U_FAILURE(status)) { log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", myErrorName(status) ); } if(u_strcmp(result, temp)!=0) log_err("FAIL: Error in extracting the pattern\n"); else log_verbose("PASS: applyPattern and toPattern work fine\n"); if(result != NULL) { free(result); result = NULL; } /*Testing getter and setter functions*/ /*isLenient and setLenient()*/ log_verbose("\nTesting the isLenient and setLenient properties\n"); udat_setLenient(fr, udat_isLenient(it)); if(udat_isLenient(fr) != udat_isLenient(it)) log_err("ERROR: setLenient() failed\n"); else log_verbose("PASS: setLenient() successful\n"); /*Test get2DigitYearStart set2DigitYearStart */ log_verbose("\nTesting the get and set 2DigitYearStart properties\n"); d1= udat_get2DigitYearStart(fr_pat,&status); if(U_FAILURE(status)) { log_err("ERROR: udat_get2DigitYearStart failed %s\n", myErrorName(status) ); } status = U_ZERO_ERROR; udat_set2DigitYearStart(def1 ,d1, &status); if(U_FAILURE(status)) { log_err("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); } if(udat_get2DigitYearStart(fr_pat, &status) != udat_get2DigitYearStart(def1, &status)) log_err("FAIL: error in set2DigitYearStart\n"); else log_verbose("PASS: set2DigitYearStart successful\n"); /*try setting it to another value */ udat_set2DigitYearStart(de, 2000.0, &status); if(U_FAILURE(status)){ log_verbose("ERROR: udat_set2DigitYearStart failed %s\n", myErrorName(status) ); } if(udat_get2DigitYearStart(de, &status) != 2000) log_err("FAIL: error in set2DigitYearStart\n"); else log_verbose("PASS: set2DigitYearStart successful\n"); /*Test getNumberFormat() and setNumberFormat() */ log_verbose("\nTesting the get and set NumberFormat properties of date format\n"); numformat1=udat_getNumberFormat(fr_pat); udat_setNumberFormat(def1, numformat1); numformat2=udat_getNumberFormat(def1); if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); else log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n"); /*try setting the number format to another format */ numformat1=udat_getNumberFormat(def); udat_setNumberFormat(def1, numformat1); numformat2=udat_getNumberFormat(def1); if(u_strcmp(myNumformat(numformat1, num), myNumformat(numformat2, num)) !=0) log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); else log_verbose("PASS: setNumberFormat and getNumberFormat succesful\n"); /*Test getCalendar and setCalendar*/ log_verbose("\nTesting the udat_getCalendar() and udat_setCalendar() properties\n"); cal=udat_getCalendar(fr_pat); udat_setCalendar(def1, cal); if(!ucal_equivalentTo(udat_getCalendar(fr_pat), udat_getCalendar(def1))) log_err("FAIL: Error in setting and getting the calendar\n"); else log_verbose("PASS: getting and setting calendar successful\n"); if(result!=NULL) { free(result); } /*Closing the UDateForamt */ udat_close(def); udat_close(fr); udat_close(it); udat_close(de); udat_close(def1); udat_close(fr_pat); udat_close(copy); ctest_resetTimeZone(); }
/** * Testing the discontigous contractions */ static void TestDiscontiguos() { const char *rulestr = "&z < AB < X\\u0300 < ABC < X\\u0300\\u0315"; UChar rule[50]; int rulelen = u_unescape(rulestr, rule, 50); const char *src[] = { "ADB", "ADBC", "A\\u0315B", "A\\u0315BC", /* base character blocked */ "XD\\u0300", "XD\\u0300\\u0315", /* non blocking combining character */ "X\\u0319\\u0300", "X\\u0319\\u0300\\u0315", /* blocking combining character */ "X\\u0314\\u0300", "X\\u0314\\u0300\\u0315", /* contraction prefix */ "ABDC", "AB\\u0315C","X\\u0300D\\u0315", "X\\u0300\\u0319\\u0315", "X\\u0300\\u031A\\u0315", /* ends not with a contraction character */ "X\\u0319\\u0300D", "X\\u0319\\u0300\\u0315D", "X\\u0300D\\u0315D", "X\\u0300\\u0319\\u0315D", "X\\u0300\\u031A\\u0315D" }; const char *tgt[] = { /* non blocking combining character */ "A D B", "A D BC", "A \\u0315 B", "A \\u0315 BC", /* base character blocked */ "X D \\u0300", "X D \\u0300\\u0315", /* non blocking combining character */ "X\\u0300 \\u0319", "X\\u0300\\u0315 \\u0319", /* blocking combining character */ "X \\u0314 \\u0300", "X \\u0314 \\u0300\\u0315", /* contraction prefix */ "AB DC", "AB \\u0315 C","X\\u0300 D \\u0315", "X\\u0300\\u0315 \\u0319", "X\\u0300 \\u031A \\u0315", /* ends not with a contraction character */ "X\\u0300 \\u0319D", "X\\u0300\\u0315 \\u0319D", "X\\u0300 D\\u0315D", "X\\u0300\\u0315 \\u0319D", "X\\u0300 \\u031A\\u0315D" }; int size = 20; UCollator *coll; UErrorCode status = U_ZERO_ERROR; int count = 0; UCollationElements *iter; UCollationElements *resultiter; coll = ucol_openRules(rule, rulelen, UCOL_OFF, UCOL_DEFAULT_STRENGTH,NULL, &status); iter = ucol_openElements(coll, rule, 1, &status); resultiter = ucol_openElements(coll, rule, 1, &status); if (U_FAILURE(status)) { log_err_status(status, "Error opening collation rules -> %s\n", u_errorName(status)); return; } while (count < size) { UChar str[20]; UChar tstr[20]; int strLen = u_unescape(src[count], str, 20); UChar *s; ucol_setText(iter, str, strLen, &status); if (U_FAILURE(status)) { log_err("Error opening collation iterator\n"); return; } u_unescape(tgt[count], tstr, 20); s = tstr; log_verbose("count %d\n", count); for (;;) { uint32_t ce; UChar *e = u_strchr(s, 0x20); if (e == 0) { e = u_strchr(s, 0); } ucol_setText(resultiter, s, (int32_t)(e - s), &status); ce = ucol_next(resultiter, &status); if (U_FAILURE(status)) { log_err("Error manipulating collation iterator\n"); return; } while (ce != UCOL_NULLORDER) { if (ce != (uint32_t)ucol_next(iter, &status) || U_FAILURE(status)) { log_err("Discontiguos contraction test mismatch\n"); return; } ce = ucol_next(resultiter, &status); if (U_FAILURE(status)) { log_err("Error getting next collation element\n"); return; } } s = e + 1; if (*e == 0) { break; } } ucol_reset(iter); backAndForth(iter); count ++; } ucol_closeElements(resultiter); ucol_closeElements(iter); ucol_close(coll); }
/** * Test the incremental normalization */ static void TestNormalization() { UErrorCode status = U_ZERO_ERROR; const char *str = "&a < \\u0300\\u0315 < A\\u0300\\u0315 < \\u0316\\u0315B < \\u0316\\u0300\\u0315"; UCollator *coll; UChar rule[50]; int rulelen = u_unescape(str, rule, 50); int count = 0; const char *testdata[] = {"\\u1ED9", "o\\u0323\\u0302", "\\u0300\\u0315", "\\u0315\\u0300", "A\\u0300\\u0315B", "A\\u0315\\u0300B", "A\\u0316\\u0315B", "A\\u0315\\u0316B", "\\u0316\\u0300\\u0315", "\\u0315\\u0300\\u0316", "A\\u0316\\u0300\\u0315B", "A\\u0315\\u0300\\u0316B", "\\u0316\\u0315\\u0300", "A\\u0316\\u0315\\u0300B"}; int32_t srclen; UChar source[10]; UCollationElements *iter; coll = ucol_openRules(rule, rulelen, UCOL_ON, UCOL_TERTIARY, NULL, &status); ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); if (U_FAILURE(status)){ log_err_status(status, "ERROR: in creation of collator using ucol_openRules()\n %s\n", myErrorName(status)); return; } srclen = u_unescape(testdata[0], source, 10); iter = ucol_openElements(coll, source, srclen, &status); backAndForth(iter); ucol_closeElements(iter); srclen = u_unescape(testdata[1], source, 10); iter = ucol_openElements(coll, source, srclen, &status); backAndForth(iter); ucol_closeElements(iter); while (count < 12) { srclen = u_unescape(testdata[count], source, 10); iter = ucol_openElements(coll, source, srclen, &status); if (U_FAILURE(status)){ log_err("ERROR: in creation of collator element iterator\n %s\n", myErrorName(status)); return; } backAndForth(iter); ucol_closeElements(iter); iter = ucol_openElements(coll, source, -1, &status); if (U_FAILURE(status)){ log_err("ERROR: in creation of collator element iterator\n %s\n", myErrorName(status)); return; } backAndForth(iter); ucol_closeElements(iter); count ++; } ucol_close(coll); }
/** * Creates a collator */ UBool processCollator() { // Set up an ICU collator UErrorCode status = U_ZERO_ERROR; UChar rules[100]; if (opt_rules != 0) { u_unescape(opt_rules, rules, 100); collator = ucol_openRules(rules, -1, UCOL_OFF, UCOL_TERTIARY, NULL, &status); } else { collator = ucol_open(opt_locale, &status); } if (U_FAILURE(status)) { fprintf(stderr, "Collator creation failed.: %d\n", status); return FALSE; } if (status == U_USING_DEFAULT_WARNING) { fprintf(stderr, "Warning, U_USING_DEFAULT_WARNING for %s\n", opt_locale); } if (status == U_USING_FALLBACK_WARNING) { fprintf(stderr, "Warning, U_USING_FALLBACK_ERROR for %s\n", opt_locale); } if (opt_norm) { ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); } if (opt_french) { ucol_setAttribute(collator, UCOL_FRENCH_COLLATION, UCOL_ON, &status); } if (opt_lower) { ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_LOWER_FIRST, &status); } if (opt_upper) { ucol_setAttribute(collator, UCOL_CASE_FIRST, UCOL_UPPER_FIRST, &status); } if (opt_case) { ucol_setAttribute(collator, UCOL_CASE_LEVEL, UCOL_ON, &status); } if (opt_shifted) { ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status); } if (opt_level != 0) { switch (opt_level) { case 1: ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); break; case 2: ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_SECONDARY, &status); break; case 3: ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_TERTIARY, &status); break; case 4: ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_QUATERNARY, &status); break; case 5: ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_IDENTICAL, &status); break; default: fprintf(stderr, "-level param must be between 1 and 5\n"); return FALSE; } } if (U_FAILURE(status)) { fprintf(stderr, "Collator attribute setting failed.: %d\n", status); return FALSE; } return TRUE; }
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); } }
// // TestRuleStatus // Test word break rule status constants. // void RBBIAPITest::TestRuleStatus() { UChar str[30]; //no longer test Han or hiragana breaking here: ruleStatusVec would return nothing // changed UBRK_WORD_KANA to UBRK_WORD_IDEO u_unescape("plain word 123.45 \\u30a1\\u30a2 ", // 012345678901234567 8 9 0 // Katakana str, 30); UnicodeString testString1(str); int32_t bounds1[] = {0, 5, 6, 10, 11, 17, 18, 20, 21}; int32_t tag_lo[] = {UBRK_WORD_NONE, UBRK_WORD_LETTER, UBRK_WORD_NONE, UBRK_WORD_LETTER, UBRK_WORD_NONE, UBRK_WORD_NUMBER, UBRK_WORD_NONE, UBRK_WORD_IDEO, UBRK_WORD_NONE}; int32_t tag_hi[] = {UBRK_WORD_NONE_LIMIT, UBRK_WORD_LETTER_LIMIT, UBRK_WORD_NONE_LIMIT, UBRK_WORD_LETTER_LIMIT, UBRK_WORD_NONE_LIMIT, UBRK_WORD_NUMBER_LIMIT, UBRK_WORD_NONE_LIMIT, UBRK_WORD_IDEO_LIMIT, UBRK_WORD_NONE_LIMIT}; UErrorCode status=U_ZERO_ERROR; BreakIterator *bi = BreakIterator::createWordInstance(Locale::getEnglish(), status); if(U_FAILURE(status)) { errcheckln(status, "Fail : in construction - %s", u_errorName(status)); } else { bi->setText(testString1); // First test that the breaks are in the right spots. doBoundaryTest(*bi, testString1, bounds1); // Then go back and check tag values int32_t i = 0; int32_t pos, tag; for (pos = bi->first(); pos != BreakIterator::DONE; pos = bi->next(), i++) { if (pos != bounds1[i]) { errln("FAIL: unexpected word break at postion %d", pos); break; } tag = bi->getRuleStatus(); if (tag < tag_lo[i] || tag >= tag_hi[i]) { errln("FAIL: incorrect tag value %d at position %d", tag, pos); break; } // Check that we get the same tag values from getRuleStatusVec() int32_t vec[10]; int t = bi->getRuleStatusVec(vec, 10, status); TEST_ASSERT_SUCCESS(status); TEST_ASSERT(t==1); TEST_ASSERT(vec[0] == tag); } } delete bi; // Now test line break status. This test mostly is to confirm that the status constants // are correctly declared in the header. testString1 = "test line. \n"; // break type s s h bi = BreakIterator::createLineInstance(Locale::getEnglish(), status); if(U_FAILURE(status)) { errcheckln(status, "failed to create word break iterator. - %s", u_errorName(status)); } else { int32_t i = 0; int32_t pos, tag; UBool success; bi->setText(testString1); pos = bi->current(); tag = bi->getRuleStatus(); for (i=0; i<3; i++) { switch (i) { case 0: success = pos==0 && tag==UBRK_LINE_SOFT; break; case 1: success = pos==5 && tag==UBRK_LINE_SOFT; break; case 2: success = pos==12 && tag==UBRK_LINE_HARD; break; default: success = FALSE; break; } if (success == FALSE) { errln("Fail: incorrect word break status or position. i=%d, pos=%d, tag=%d", i, pos, tag); break; } pos = bi->next(); tag = bi->getRuleStatus(); } if (UBRK_LINE_SOFT >= UBRK_LINE_SOFT_LIMIT || UBRK_LINE_HARD >= UBRK_LINE_HARD_LIMIT || (UBRK_LINE_HARD > UBRK_LINE_SOFT && UBRK_LINE_HARD < UBRK_LINE_SOFT_LIMIT)) { errln("UBRK_LINE_* constants from header are inconsistent."); } } delete bi; }
/** * Tests surrogate support. * NOTE: This test used \\uD801\\uDC01 pair, which is now assigned to Desseret * Therefore, another (unassigned) code point was used for this test. */ static void TestSurrogates(void) { const char *str = "&z<'\\uD800\\uDC00'<'\\uD800\\uDC0A\\u0308'<A"; int len = strlen(str); int rlen = 0; UChar *rules; UCollator *myCollation; UCollator *enCollation; UErrorCode status = U_ZERO_ERROR; UChar source[][4] = {{'z', 0, 0}, {0xD800, 0xDC00, 0}, {0xD800, 0xDC0A, 0x0308, 0}, {0xD800, 0xDC02}}; UChar target[][4] = {{0xD800, 0xDC00, 0}, {0xD800, 0xDC0A, 0x0308, 0}, {'A', 0, 0}, {0xD800, 0xDC03}}; int count = 0; uint8_t enresult[20], myresult[20]; int enlen, mylen; /* tests for open rules with surrogate rules */ rules = (UChar*)malloc(sizeof(UChar*) * (len + 1)); rlen = u_unescape(str, rules, len); enCollation = ucol_open("en_US", &status); myCollation = ucol_openRules(rules, rlen, UCOL_OFF, UCOL_TERTIARY,NULL, &status); if (U_FAILURE(status)) { log_err("ERROR: in creation of rule based collator :%s\n", myErrorName(status)); return; } /* this test is to verify the supplementary sort key order in the english collator */ log_verbose("start of english collation supplementary characters test\n"); while (count < 2) { doTest(enCollation, source[count], target[count], UCOL_LESS); count ++; } doTest(enCollation, source[count], target[count], UCOL_GREATER); log_verbose("start of tailored collation supplementary characters test\n"); count = 0; /* tests getting collation elements for surrogates for tailored rules */ while (count < 4) { doTest(myCollation, source[count], target[count], UCOL_LESS); count ++; } /* tests that \uD800\uDC02 still has the same value, not changed */ enlen = ucol_getSortKey(enCollation, source[3], 2, enresult, 20); mylen = ucol_getSortKey(myCollation, source[3], 2, myresult, 20); if (enlen != mylen || uprv_memcmp(enresult, myresult, enlen) != 0) { log_verbose("Failed : non-tailored supplementary characters should have the same value\n"); } free(rules); ucol_close(enCollation); ucol_close(myCollation); enCollation = NULL; myCollation = NULL; }
static void TestBreakIteratorSuppressions(void) { const TestBISuppressionsItem * itemPtr; for (itemPtr = testBISuppressionsItems; itemPtr->locale != NULL; itemPtr++) { UChar textU[kTextULenMax]; int32_t textULen = u_unescape(itemPtr->text, textU, kTextULenMax); UErrorCode status = U_ZERO_ERROR; UBreakIterator *bi = ubrk_open(UBRK_SENTENCE, itemPtr->locale, textU, textULen, &status); log_verbose("#%d: %s\n", (itemPtr-testBISuppressionsItems), itemPtr->locale); if (U_SUCCESS(status)) { int32_t offset, start; const int32_t * expOffsetPtr; const int32_t * expOffsetStart; expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; ubrk_first(bi); for (; (offset = ubrk_next(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { if (offset != *expOffsetPtr) { log_err("FAIL: ubrk_next loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset); } } if (offset != UBRK_DONE || *expOffsetPtr >= 0) { log_err("FAIL: ubrk_next loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", itemPtr->locale, offset, *expOffsetPtr); } expOffsetStart = expOffsetPtr = itemPtr->expFwdOffsets; start = ubrk_first(bi) + 1; for (; (offset = ubrk_following(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { if (offset != *expOffsetPtr) { log_err("FAIL: ubrk_following(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset); } start = *expOffsetPtr + 1; } if (offset != UBRK_DONE || *expOffsetPtr >= 0) { log_err("FAIL: ubrk_following(%d) loc \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr); } expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; offset = ubrk_last(bi); log_verbose("___ @%d ubrk_last\n", offset); if(offset == 0) { log_err("FAIL: ubrk_last loc \"%s\" unexpected %d\n", itemPtr->locale, offset); } for (; (offset = ubrk_previous(bi)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { if (offset != *expOffsetPtr) { log_err("FAIL: ubrk_previous loc \"%s\", expected %d, got %d\n", itemPtr->locale, *expOffsetPtr, offset); } else { log_verbose("[%d] @%d ubrk_previous()\n", (expOffsetPtr - expOffsetStart), offset); } } if (offset != UBRK_DONE || *expOffsetPtr >= 0) { log_err("FAIL: ubrk_previous loc \"%s\", expected UBRK_DONE & expOffset[%d] -1, got %d and %d\n", itemPtr->locale, expOffsetPtr - expOffsetStart, offset, *expOffsetPtr); } expOffsetStart = expOffsetPtr = itemPtr->expRevOffsets; start = ubrk_last(bi) - 1; for (; (offset = ubrk_preceding(bi, start)) != UBRK_DONE && *expOffsetPtr >= 0; expOffsetPtr++) { if (offset != *expOffsetPtr) { log_err("FAIL: ubrk_preceding(%d) loc \"%s\", expected %d, got %d\n", start, itemPtr->locale, *expOffsetPtr, offset); } start = *expOffsetPtr - 1; } if (start >=0 && (offset != UBRK_DONE || *expOffsetPtr >= 0)) { log_err("FAIL: ubrk_preceding loc(%d) \"%s\", expected UBRK_DONE & expOffset -1, got %d and %d\n", start, itemPtr->locale, offset, *expOffsetPtr); } ubrk_close(bi); } else { log_data_err("FAIL: ubrk_open(UBRK_SENTENCE, \"%s\", ...) status %s (Are you missing data?)\n", itemPtr->locale, u_errorName(status)); } } }
static void TestRelDateFmt() { const RelDateTimeFormatTestItem *itemPtr; log_verbose("\nTesting ureldatefmt_open(), ureldatefmt_format(), ureldatefmt_formatNumeric() with various parameters\n"); for (itemPtr = fmtTestItems; itemPtr->locale != NULL; itemPtr++) { URelativeDateTimeFormatter *reldatefmt = NULL; UNumberFormat* nfToAdopt = NULL; UErrorCode status = U_ZERO_ERROR; int32_t iOffset; if (itemPtr->decPlaces >= 0) { nfToAdopt = unum_open(UNUM_DECIMAL, NULL, 0, itemPtr->locale, NULL, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: unum_open(UNUM_DECIMAL, ...) for locale %s: %s\n", itemPtr->locale, myErrorName(status)); continue; } unum_setAttribute(nfToAdopt, UNUM_MIN_FRACTION_DIGITS, itemPtr->decPlaces); unum_setAttribute(nfToAdopt, UNUM_MAX_FRACTION_DIGITS, itemPtr->decPlaces); unum_setAttribute(nfToAdopt, UNUM_ROUNDING_MODE, UNUM_ROUND_DOWN); } reldatefmt = ureldatefmt_open(itemPtr->locale, nfToAdopt, itemPtr->width, itemPtr->capContext, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: ureldatefmt_open() for locale %s, decPlaces %d, width %d, capContext %d: %s\n", itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, myErrorName(status) ); continue; } for (iOffset = 0; iOffset < kNumOffsets; iOffset++) { UChar ubufget[kUBufMax]; int32_t ulenget; if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY && offsets[iOffset] != -1.0 && offsets[iOffset] != 0.0 && offsets[iOffset] != 1.0) { continue; /* we do not currently have data for this */ } status = U_ZERO_ERROR; ulenget = ureldatefmt_format(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("FAIL: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n", itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, offsets[iOffset], (int)itemPtr->unit, myErrorName(status) ); } else { UChar ubufexp[kUBufMax]; int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2], ubufexp, kUBufMax); if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { char bbufget[kBBufMax]; u_austrncpy(bbufget, ubufget, kUBufMax); log_err("ERROR: ureldatefmt_format() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n", itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2], bbufget ); } } if (itemPtr->unit >= UDAT_REL_UNIT_SUNDAY) { continue; /* we do not currently have numeric-style data for this */ } status = U_ZERO_ERROR; ulenget = ureldatefmt_formatNumeric(reldatefmt, offsets[iOffset], itemPtr->unit, ubufget, kUBufMax, &status); if ( U_FAILURE(status) ) { log_err("FAIL: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d: %s\n", itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, offsets[iOffset], (int)itemPtr->unit, myErrorName(status) ); } else { UChar ubufexp[kUBufMax]; int32_t ulenexp = u_unescape(itemPtr->expectedResults[iOffset*2 + 1], ubufexp, kUBufMax); if (ulenget != ulenexp || u_strncmp(ubufget, ubufexp, ulenexp) != 0) { char bbufget[kBBufMax]; u_austrncpy(bbufget, ubufget, kUBufMax); log_err("ERROR: ureldatefmt_formatNumeric() for locale %s, decPlaces %d, width %d, capContext %d, offset %.2f, unit %d;\n expected %s\n get %s\n", itemPtr->locale, itemPtr->decPlaces, (int)itemPtr->width, (int)itemPtr->capContext, offsets[iOffset], (int)itemPtr->unit, itemPtr->expectedResults[iOffset*2 + 1], bbufget ); } } } ureldatefmt_close(reldatefmt); } }