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); } }
static void test_assert_string(const char *expected, const UChar *actual, UBool nulTerm, const char *file, int line) { char buf_inside_macro[120]; int32_t len = (int32_t)strlen(expected); UBool success; if (nulTerm) { u_austrncpy(buf_inside_macro, (actual), len+1); buf_inside_macro[len+2] = 0; success = (strcmp((expected), buf_inside_macro) == 0); } else { u_austrncpy(buf_inside_macro, (actual), len); buf_inside_macro[len+1] = 0; success = (strncmp((expected), buf_inside_macro, len) == 0); } if (success == FALSE) { log_err("Failure at file %s, line %d, expected \"%s\", got \"%s\"\n", file, line, (expected), buf_inside_macro); } }
static void TestOptions() { const DTPtnGenOptionsData testData[] = { /*loc skel options expectedPattern */ { "en", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm }, { "en", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHcmm }, { "en", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hcmm_a }, { "en", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm }, { "en", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHcmm }, { "en", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhcmm_a }, { "be", skel_Hmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm }, { "be", skel_HHmm, UDATPG_MATCH_NO_OPTIONS, patn_HHpmm }, { "be", skel_hhmm, UDATPG_MATCH_NO_OPTIONS, patn_hpmm_a }, { "be", skel_Hmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_Hpmm }, { "be", skel_HHmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_HHpmm }, { "be", skel_hhmm, UDATPG_MATCH_HOUR_FIELD_LENGTH, patn_hhpmm_a }, }; int count = sizeof(testData) / sizeof(testData[0]); const DTPtnGenOptionsData * testDataPtr = testData; for (; count-- > 0; ++testDataPtr) { UErrorCode status = U_ZERO_ERROR; UDateTimePatternGenerator * dtpgen = udatpg_open(testDataPtr->locale, &status); if ( U_SUCCESS(status) ) { UChar pattern[kTestOptionsPatLenMax]; int32_t patLen = udatpg_getBestPatternWithOptions(dtpgen, testDataPtr->skel, -1, testDataPtr->options, pattern, kTestOptionsPatLenMax, &status); if ( U_FAILURE(status) || u_strncmp(pattern, testDataPtr->expectedPattern, patLen+1) != 0 ) { char skelBytes[kTestOptionsPatLenMax]; char expectedPatternBytes[kTestOptionsPatLenMax]; char patternBytes[kTestOptionsPatLenMax]; log_err("ERROR udatpg_getBestPatternWithOptions, locale %s, skeleton %s, options 0x%04X, expected pattern %s, got %s, status %d\n", testDataPtr->locale, u_austrncpy(skelBytes,testDataPtr->skel,kTestOptionsPatLenMax), testDataPtr->options, u_austrncpy(expectedPatternBytes,testDataPtr->expectedPattern,kTestOptionsPatLenMax), u_austrncpy(patternBytes,pattern,kTestOptionsPatLenMax), status ); } udatpg_close(dtpgen); } else { log_data_err("ERROR udatpg_open failed for locale %s : %s - (Are you missing data?)\n", testDataPtr->locale, myErrorName(status)); } } }
void printTextRange(UChar* str, int32_t start, int32_t end) { char charBuf[1000]; UChar savedEndChar; savedEndChar = str[end]; str[end] = 0; u_austrncpy(charBuf, str+start, sizeof(charBuf)-1); charBuf[sizeof(charBuf)-1]=0; printf("string[%2d..%2d] \"%s\"\n", start, end-1, charBuf); str[end] = savedEndChar; }
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 UBool _aux1ExtremeDates(UDateFormat* fmt, UDate date, UChar* buf, int32_t buflen, char* cbuf, UErrorCode* ec) { int32_t len = udat_format(fmt, date, buf, buflen, 0, ec); if (!assertSuccess("udat_format", ec)) return FALSE; u_austrncpy(cbuf, buf, buflen); if (len < 4) { log_err("FAIL: udat_format(%g) => \"%s\"\n", date, cbuf); } else { log_verbose("udat_format(%g) => \"%s\"\n", date, cbuf); } return TRUE; }
int tokenizer_next( tokenizer_t *t, char *word, size_t size ) { UChar savedEndChar; int k; // start iterator if( t->end == 0 ) { t->start = ubrk_first(t->boundary); } // Find next word again: t->end = ubrk_next(t->boundary); if( t->end == UBRK_DONE ) { return -1; } // Null terminate savedEndChar = t->str[t->end]; t->str[t->end] = 0; // Skip unct if( t->end - t->start == 1 && u_ispunct( t->str[t->start] ) ) { t->str[t->end] = savedEndChar; t->start = t->end; goto again; } // Skip whitespace for( k=t->start; k<t->end; k++ ) { if( u_isspace( t->str[k] ) == 1 ) { t->str[t->end] = savedEndChar; t->start = t->end; goto again; } } // Copy to C bffer u_austrncpy(word, t->str+t->start, size-1); word[size-1] = 0; printf("string[%2d..%2d] \"%s\" %d\n", t->start, t->end-1, word, u_isspace( t->str[t->start])); t->str[t->end] = savedEndChar; t->start = t->end; return 0; }
int icu_format_number(lua_State *L) { double a = luaL_checknumber(L, 1); /* See http://www.unicode.org/repos/cldr/tags/latest/common/bcp47/number.xml for valid system names */ const char* system = luaL_checkstring(L, 2); char locale[18]; // "@numbers=12345678"; UChar buf[256]; char utf8[256]; int32_t needed; UErrorCode status = U_ZERO_ERROR; snprintf(locale, 18, "@numbers=%s", system); UNumberFormat* fmt = unum_open(UNUM_DECIMAL, 0, 0, locale, 0, &status); if(U_FAILURE(status)) { luaL_error(L, "Locale %s unavailable: %s", locale, u_errorName(status)); } needed = unum_formatDouble(fmt, a, buf, 256, NULL, &status); assert(!U_FAILURE(status)); u_austrncpy(utf8, buf, 256); lua_pushstring(L, utf8); return 1; }
static void TestJ904(void) { UChar pattern[256]; UChar result[256]; UChar string[16]; char cresult[256]; int32_t length; UErrorCode status = U_ZERO_ERROR; const char* PAT = "Number {1,number,#0.000}, String {0}, Date {2,date,12:mm:ss.SSS}"; const char* EXP = "Number 0,143, String foo, Date 12:34:56.789"; ctest_setTimeZone(NULL, &status); u_uastrcpy(string, "foo"); /* Slight hack here -- instead of date pattern HH:mm:ss.SSS, use * 12:mm:ss.SSS. Why? So this test generates the same output -- * "12:34:56.789" -- regardless of time zone (as long as we aren't * in one of the 30 minute offset zones!). */ u_uastrcpy(pattern, PAT); length = u_formatMessage("nl", pattern, u_strlen(pattern), result, 256, &status, string, 1/7.0, 789.0+1000*(56+60*(34+60*12))); u_austrncpy(cresult, result, sizeof(cresult)); /* This test passes if it DOESN'T CRASH. However, we test the * output anyway. If the string doesn't match in the date part, * check to see that the machine doesn't have an unusual time zone * offset, that is, one with a non-zero minutes/seconds offset * from GMT -- see above. */ if (strcmp(cresult, EXP) == 0) { log_verbose("Ok: \"%s\"\n", cresult); } else { log_data_err("FAIL: got \"%s\", expected \"%s\" -> %s (Are you missing data?)\n", cresult, EXP, u_errorName(status)); } ctest_resetTimeZone(); }
U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { const char* winid; int32_t len; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); if (U_SUCCESS(status)) { icuid = (char*)uprv_malloc(sizeof(char) * (len + 1)); uprv_memset(icuid, 0, len + 1); u_austrncpy(icuid, icuTZ, len); } } } ures_close(winTZ); if (icuid != NULL) { break; } } ures_close(bundle); return icuid; }
/** * Main Windows time zone detection function. Returns the Windows * time zone, translated to an ICU time zone, or NULL upon failure. */ U_CFUNC const char* U_EXPORT2 uprv_detectWindowsTimeZone() { UErrorCode status = U_ZERO_ERROR; UResourceBundle* bundle = NULL; char* icuid = NULL; UChar apiStd[MAX_LENGTH_ID]; char apiStdName[MAX_LENGTH_ID]; char regStdName[MAX_LENGTH_ID]; char tmpid[MAX_LENGTH_ID]; int32_t len; int id; int errorCode; char ISOcode[3]; /* 2 letter iso code */ LONG result; TZI tziKey; TZI tziReg; TIME_ZONE_INFORMATION apiTZI; /* Obtain TIME_ZONE_INFORMATION from the API, and then convert it to TZI. We could also interrogate the registry directly; we do this below if needed. */ uprv_memset(&apiTZI, 0, sizeof(apiTZI)); uprv_memset(&tziKey, 0, sizeof(tziKey)); uprv_memset(&tziReg, 0, sizeof(tziReg)); GetTimeZoneInformation(&apiTZI); tziKey.bias = apiTZI.Bias; uprv_memcpy((char *)&tziKey.standardDate, (char*)&apiTZI.StandardDate, sizeof(apiTZI.StandardDate)); uprv_memcpy((char *)&tziKey.daylightDate, (char*)&apiTZI.DaylightDate, sizeof(apiTZI.DaylightDate)); /* Convert the wchar_t* standard name to char* */ uprv_memset(apiStdName, 0, sizeof(apiStdName)); u_strFromWCS(apiStd, MAX_LENGTH_ID, NULL, apiTZI.StandardName, -1, &status); u_austrncpy(apiStdName, apiStd, sizeof(apiStdName) - 1); tmpid[0] = 0; id = GetUserGeoID(GEOCLASS_NATION); errorCode = GetGeoInfoA(id,GEO_ISO2,ISOcode,3,0); bundle = ures_openDirect(NULL, "windowsZones", &status); ures_getByKey(bundle, "mapTimezones", bundle, &status); /* Note: We get the winid not from static tables but from resource bundle. */ while (U_SUCCESS(status) && ures_hasNext(bundle)) { UBool idFound = FALSE; const char* winid; UResourceBundle* winTZ = ures_getNextResource(bundle, NULL, &status); if (U_FAILURE(status)) { break; } winid = ures_getKey(winTZ); result = getTZI(winid, &tziReg); if (result == ERROR_SUCCESS) { /* Windows alters the DaylightBias in some situations. Using the bias and the rules suffices, so overwrite these unreliable fields. */ tziKey.standardBias = tziReg.standardBias; tziKey.daylightBias = tziReg.daylightBias; if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) { const UChar* icuTZ = NULL; if (errorCode != 0) { icuTZ = ures_getStringByKey(winTZ, ISOcode, &len, &status); } if (errorCode==0 || icuTZ==NULL) { /* fallback to default "001" and reset status */ status = U_ZERO_ERROR; icuTZ = ures_getStringByKey(winTZ, "001", &len, &status); } if (U_SUCCESS(status)) { /* Get the standard name from the registry key to compare with the one from Windows API call. */ uprv_memset(regStdName, 0, sizeof(regStdName)); result = getSTDName(winid, regStdName, sizeof(regStdName)); if (result == ERROR_SUCCESS) { if (uprv_strcmp(apiStdName, regStdName) == 0) { idFound = TRUE; } } /* tmpid buffer holds the ICU timezone ID corresponding to the timezone ID from Windows. * If none is found, tmpid buffer will contain a fallback ID (i.e. the time zone ID matching * the current time zone information) */ if (idFound || tmpid[0] == 0) { /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */ int index=0; while (! (*icuTZ == '\0' || *icuTZ ==' ')) { tmpid[index++]=(char)(*icuTZ++); /* safe to assume 'char' is ASCII compatible on windows */ } tmpid[index]='\0'; } } } } ures_close(winTZ); if (idFound) { break; } } /* * Copy the timezone ID to icuid to be returned. */ if (tmpid[0] != 0) { len = uprv_strlen(tmpid); icuid = (char*)uprv_calloc(len + 1, sizeof(char)); if (icuid != NULL) { uprv_strcpy(icuid, tmpid); } } ures_close(bundle); return icuid; }
U_CDECL_END U_CDECL_BEGIN static void U_CALLCONV DataDrivenPrintfPrecision(void) { #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO UErrorCode errorCode; TestDataModule *dataModule; TestData *testData; const DataMap *testCase; DataDrivenLogger logger; UChar uBuffer[512]; char cBuffer[512]; char cFormat[sizeof(cBuffer)]; char cExpected[sizeof(cBuffer)]; UnicodeString tempStr; UChar format[512]; UChar expectedResult[512]; UChar argument[512]; int32_t precision; int32_t i; int8_t i8; int16_t i16; int32_t i32; int64_t i64; double dbl; int32_t uBufferLenReturned; errorCode=U_ZERO_ERROR; dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode); if(U_SUCCESS(errorCode)) { testData=dataModule->createTestData("printfPrecision", errorCode); if(U_SUCCESS(errorCode)) { for(i=0; testData->nextCase(testCase, errorCode); ++i) { if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } u_memset(uBuffer, 0x2A, UPRV_LENGTHOF(uBuffer)); uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; tempStr=testCase->getString("format", errorCode); tempStr.extract(format, UPRV_LENGTHOF(format), errorCode); tempStr=testCase->getString("result", errorCode); tempStr.extract(expectedResult, UPRV_LENGTHOF(expectedResult), errorCode); tempStr=testCase->getString("argument", errorCode); tempStr.extract(argument, UPRV_LENGTHOF(argument), errorCode); precision=testCase->getInt28("precision", errorCode); u_austrncpy(cBuffer, format, sizeof(cBuffer)); if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } log_verbose("Test %d: format=\"%s\"\n", i, cBuffer); switch (testCase->getString("argumentType", errorCode)[0]) { case 0x64: // 'd' double dbl = atof(u_austrcpy(cBuffer, argument)); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, dbl); break; case 0x31: // '1' int8_t i8 = (int8_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i8); break; case 0x32: // '2' int16_t i16 = (int16_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i16); break; case 0x34: // '4' int32_t i32 = (int32_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i32); break; case 0x38: // '8' int64_t i64 = uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, i64); break; case 0x73: // 's' char * u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, cBuffer); break; case 0x53: // 'S' UChar * uBufferLenReturned = u_sprintf_u(uBuffer, format, precision, argument); break; default: uBufferLenReturned = 0; log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); } if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); u_austrncpy(cFormat, format, sizeof(cFormat)); u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", i, cFormat, cBuffer, cExpected); } if (uBufferLenReturned <= 0) { log_err("FAILURE test case %d - \"%s\" is an empty string.\n", i, cBuffer); } else if (uBuffer[uBufferLenReturned-1] == 0 || uBuffer[uBufferLenReturned] != 0 || uBuffer[uBufferLenReturned+1] != 0x2A || uBuffer[uBufferLenReturned+2] != 0x2A) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n", i, cBuffer, uBufferLenReturned); } if(U_FAILURE(errorCode)) { log_err("error running icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } } delete testData; } delete dataModule; } else { log_data_err("Failed: could not load test icuio data\n"); } #endif }
U_CDECL_END U_CDECL_BEGIN static void U_CALLCONV DataDrivenScanf(void) { #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO UErrorCode errorCode; TestDataModule *dataModule; TestData *testData; const DataMap *testCase; DataDrivenLogger logger; UChar uBuffer[512]; char cBuffer[512]; char cExpected[sizeof(cBuffer)]; UnicodeString tempStr; UChar format[512]; UChar expectedResult[512]; UChar argument[512]; int32_t i; int8_t i8, expected8; int16_t i16, expected16; int32_t i32, expected32; int64_t i64, expected64; double dbl, expectedDbl; volatile float flt, expectedFlt; // Use volatile in order to get around an Intel compiler issue. int32_t uBufferLenReturned; //const char *fileLocale = "en_US_POSIX"; //int32_t uFileBufferLenReturned; //UFILE *testFile; errorCode=U_ZERO_ERROR; dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode); if(U_SUCCESS(errorCode)) { testData=dataModule->createTestData("scanf", errorCode); if(U_SUCCESS(errorCode)) { for(i=0; testData->nextCase(testCase, errorCode); ++i) { if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } /* testFile = u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8"); if (!testFile) { log_err("Can't open test file - %s\n", STANDARD_TEST_FILE); }*/ u_memset(uBuffer, 0x2A, UPRV_LENGTHOF(uBuffer)); uBuffer[UPRV_LENGTHOF(uBuffer)-1] = 0; tempStr=testCase->getString("format", errorCode); tempStr.extract(format, UPRV_LENGTHOF(format), errorCode); tempStr=testCase->getString("result", errorCode); tempStr.extract(expectedResult, UPRV_LENGTHOF(expectedResult), errorCode); tempStr=testCase->getString("argument", errorCode); tempStr.extract(argument, UPRV_LENGTHOF(argument), errorCode); u_austrncpy(cBuffer, format, sizeof(cBuffer)); if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } log_verbose("Test %d: format=\"%s\"\n", i, cBuffer); switch (testCase->getString("argumentType", errorCode)[0]) { case 0x64: // 'd' double expectedDbl = atof(u_austrcpy(cBuffer, expectedResult)); uBufferLenReturned = u_sscanf_u(argument, format, &dbl); //uFileBufferLenReturned = u_fscanf_u(testFile, format, dbl); if (dbl != expectedDbl) { log_err("error in scanf test case[%d] Got: %f Exp: %f\n", i, dbl, expectedDbl); } break; case 0x66: // 'f' float expectedFlt = (float)atof(u_austrcpy(cBuffer, expectedResult)); uBufferLenReturned = u_sscanf_u(argument, format, &flt); //uFileBufferLenReturned = u_fscanf_u(testFile, format, flt); if (flt != expectedFlt) { log_err("error in scanf test case[%d] Got: %f Exp: %f\n", i, flt, expectedFlt); } break; case 0x31: // '1' int8_t expected8 = (int8_t)uto64(expectedResult); uBufferLenReturned = u_sscanf_u(argument, format, &i8); //uFileBufferLenReturned = u_fscanf_u(testFile, format, i8); if (i8 != expected8) { log_err("error in scanf test case[%d] Got: %02X Exp: %02X\n", i, i8, expected8); } break; case 0x32: // '2' int16_t expected16 = (int16_t)uto64(expectedResult); uBufferLenReturned = u_sscanf_u(argument, format, &i16); //uFileBufferLenReturned = u_fscanf_u(testFile, format, i16); if (i16 != expected16) { log_err("error in scanf test case[%d] Got: %04X Exp: %04X\n", i, i16, expected16); } break; case 0x34: // '4' int32_t expected32 = (int32_t)uto64(expectedResult); uBufferLenReturned = u_sscanf_u(argument, format, &i32); //uFileBufferLenReturned = u_fscanf_u(testFile, format, i32); if (i32 != expected32) { log_err("error in scanf test case[%d] Got: %08X Exp: %08X\n", i, i32, expected32); } break; case 0x38: // '8' int64_t expected64 = uto64(expectedResult); uBufferLenReturned = u_sscanf_u(argument, format, &i64); //uFileBufferLenReturned = u_fscanf_u(testFile, format, i64); if (i64 != expected64) { log_err("error in scanf 64-bit. Test case = %d\n", i); } break; case 0x73: // 's' char * u_austrcpy(cExpected, expectedResult); uBufferLenReturned = u_sscanf_u(argument, format, cBuffer); //uFileBufferLenReturned = u_fscanf_u(testFile, format, cBuffer); if (strcmp(cBuffer, cExpected) != 0) { log_err("error in scanf char * string. Got \"%s\" Expected \"%s\". Test case = %d\n", cBuffer, cExpected, i); } break; case 0x53: // 'S' UChar * uBufferLenReturned = u_sscanf_u(argument, format, uBuffer); //uFileBufferLenReturned = u_fscanf_u(testFile, format, argument); if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrcpy(cExpected, format); u_austrcpy(cBuffer, uBuffer); log_err("error in scanf UChar * string %s Got: \"%s\". Test case = %d\n", cExpected, cBuffer, i); } break; default: uBufferLenReturned = 0; //uFileBufferLenReturned = 0; log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); } if (uBufferLenReturned != 1) { log_err("error scanf converted %d arguments. Test case = %d\n", uBufferLenReturned, i); } /* if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); u_austrncpy(cFormat, format, sizeof(cFormat)); u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", i, cFormat, cBuffer, cExpected); } if (uBuffer[uBufferLenReturned-1] == 0 || uBuffer[uBufferLenReturned] != 0 || uBuffer[uBufferLenReturned+1] != 0x2A || uBuffer[uBufferLenReturned+2] != 0x2A) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n", i, cBuffer, uBufferLenReturned); }*/ /* u_fclose(testFile); testFile = u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8"); if (!testFile) { log_err("Can't open test file - %s\n", STANDARD_TEST_FILE); } uBuffer[0]; u_fgets(uBuffer, UPRV_LENGTHOF(uBuffer), testFile); if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); u_austrncpy(cFormat, format, sizeof(cFormat)); u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", i, cFormat, cBuffer, cExpected); } if (uFileBufferLenReturned != uBufferLenReturned) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n", uFileBufferLenReturned, uBufferLenReturned); } */ if(U_FAILURE(errorCode)) { log_err("error running icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } // u_fclose(testFile); } delete testData; } delete dataModule; } else { log_data_err("Failed: could not load test icuio data\n"); } #endif }
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); } }
U_CDECL_BEGIN static void U_CALLCONV DataDrivenPrintf(void) { #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_FILE_IO UErrorCode errorCode; TestDataModule *dataModule; TestData *testData; const DataMap *testCase; DataDrivenLogger logger; UChar uBuffer[512]; char cBuffer[512]; char cFormat[sizeof(cBuffer)]; char cExpected[sizeof(cBuffer)]; UnicodeString tempStr; UChar format[512]; UChar expectedResult[512]; UChar argument[512]; int32_t i; int8_t i8; int16_t i16; int32_t i32; int64_t i64; double dbl; int32_t uBufferLenReturned; const char *fileLocale = "en_US_POSIX"; int32_t uFileBufferLenReturned; LocalUFILEPointer testFile; errorCode=U_ZERO_ERROR; dataModule=TestDataModule::getTestDataModule("icuio", logger, errorCode); if(U_SUCCESS(errorCode)) { testData=dataModule->createTestData("printf", errorCode); if(U_SUCCESS(errorCode)) { for(i=0; testData->nextCase(testCase, errorCode); ++i) { if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "w", fileLocale, "UTF-8")); if (testFile.isNull()) { log_err("Can't open test file - %s\n", STANDARD_TEST_FILE); continue; } u_memset(uBuffer, 0x2A, sizeof(uBuffer)/sizeof(uBuffer[0])); uBuffer[sizeof(uBuffer)/sizeof(uBuffer[0])-1] = 0; tempStr=testCase->getString("format", errorCode); tempStr.extract(format, sizeof(format)/sizeof(format[0]), errorCode); tempStr=testCase->getString("result", errorCode); tempStr.extract(expectedResult, sizeof(expectedResult)/sizeof(expectedResult[0]), errorCode); tempStr=testCase->getString("argument", errorCode); tempStr.extract(argument, sizeof(argument)/sizeof(argument[0]), errorCode); u_austrncpy(cBuffer, format, sizeof(cBuffer)); if(U_FAILURE(errorCode)) { log_err("error retrieving icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } log_verbose("Test %d: format=\"%s\"\n", i, cBuffer); switch (testCase->getString("argumentType", errorCode)[0]) { case 0x64: // 'd' double dbl = atof(u_austrcpy(cBuffer, argument)); uBufferLenReturned = u_sprintf_u(uBuffer, format, dbl); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, dbl); break; case 0x31: // '1' int8_t i8 = (int8_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, i8); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i8); break; case 0x32: // '2' int16_t i16 = (int16_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, i16); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i16); break; case 0x34: // '4' int32_t i32 = (int32_t)uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, i32); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i32); break; case 0x38: // '8' int64_t i64 = uto64(argument); uBufferLenReturned = u_sprintf_u(uBuffer, format, i64); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, i64); break; case 0x73: // 's' char * u_austrncpy(cBuffer, argument, sizeof(cBuffer)); uBufferLenReturned = u_sprintf_u(uBuffer, format, cBuffer); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, cBuffer); break; case 0x53: // 'S' UChar * uBufferLenReturned = u_sprintf_u(uBuffer, format, argument); uFileBufferLenReturned = u_fprintf_u(testFile.getAlias(), format, argument); break; default: uBufferLenReturned = 0; uFileBufferLenReturned = 0; log_err("Unknown type %c for test %d\n", testCase->getString("argumentType", errorCode)[0], i); } if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); u_austrncpy(cFormat, format, sizeof(cFormat)); u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE string test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", i, cFormat, cBuffer, cExpected); } if (uBufferLenReturned <= 0) { log_err("FAILURE test case %d - \"%s\" is an empty string.\n", i, cBuffer); } else if (uBuffer[uBufferLenReturned-1] == 0 || uBuffer[uBufferLenReturned] != 0 || uBuffer[uBufferLenReturned+1] != 0x2A || uBuffer[uBufferLenReturned+2] != 0x2A) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE test case %d - \"%s\" wrong amount of characters was written. Got %d.\n", i, cBuffer, uBufferLenReturned); } testFile.adoptInstead(u_fopen(STANDARD_TEST_FILE, "r", fileLocale, "UTF-8")); if (testFile.isNull()) { log_err("Can't open test file - %s\n", STANDARD_TEST_FILE); } uBuffer[0]=0; u_fgets(uBuffer, sizeof(uBuffer)/sizeof(uBuffer[0]), testFile.getAlias()); if (u_strcmp(uBuffer, expectedResult) != 0) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); u_austrncpy(cFormat, format, sizeof(cFormat)); u_austrncpy(cExpected, expectedResult, sizeof(cExpected)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE file test case %d \"%s\" - Got: \"%s\" Expected: \"%s\"\n", i, cFormat, cBuffer, cExpected); } if (uFileBufferLenReturned != uBufferLenReturned) { u_austrncpy(cBuffer, uBuffer, sizeof(cBuffer)); cBuffer[sizeof(cBuffer)-1] = 0; log_err("FAILURE uFileBufferLenReturned(%d) != uBufferLenReturned(%d)\n", uFileBufferLenReturned, uBufferLenReturned); } if(U_FAILURE(errorCode)) { log_err("error running icuio/printf test case %d - %s\n", i, u_errorName(errorCode)); errorCode=U_ZERO_ERROR; continue; } } delete testData; } delete dataModule; } else { log_data_err("Failed: could not load test icuio data\n"); } #endif }