U_CAPI UFILE* U_EXPORT2 u_fopen_u(const UChar *filename, const char *perm, const char *locale, const char *codepage) { UFILE *result; char buffer[256]; u_austrcpy(buffer, filename); result = u_fopen(buffer, perm, locale, codepage); #if U_PLATFORM_USES_ONLY_WIN32_API /* Try Windows API _wfopen if the above fails. */ if (!result) { // TODO: test this code path, including wperm. wchar_t wperm[40] = {}; size_t retVal; mbstowcs_s(&retVal, wperm, perm, _TRUNCATE); FILE *systemFile = _wfopen((const wchar_t *)filename, wperm); if (systemFile) { result = finit_owner(systemFile, locale, codepage, TRUE); } if (!result) { /* Something bad happened. Maybe the converter couldn't be opened. */ fclose(systemFile); } } #endif return result; /* not a file leak */ }
int32_t GLUE_SYM ( Collator ) :: appendAvailable(UnicodeString* strs, int32_t i, int32_t /*count*/) { int avail = OICU_ucol_countAvailable(); UErrorCode status = U_ZERO_ERROR; OICU_u_init(&status); #if COLL_FE_DEBUG fprintf(stderr, "VCF " ICUGLUE_VER_STR " avail %d - init %s\n", avail, u_errorName(status)); #endif for(int j=0;j<avail;j++) { strs[i+j].append(OICU_ucol_getAvailable(j)); strs[i+j].append("@sp=icu"); if(IS_OLD_VERSTR(ICUGLUE_VER_STR)) { strs[i+j].append( ICUGLUE_VER_STR[OLD_VERSTR_MAJ] ); // X_y strs[i+j].append( ICUGLUE_VER_STR[OLD_VERSTR_MIN] ); // x_Y } else { strs[i+j].append( ICUGLUE_VER_STR[NEW_VERSTR_MAJ] ); // Xy_ strs[i+j].append( ICUGLUE_VER_STR[NEW_VERSTR_MIN] ); // xY_ } #if COLL_FE_DEBUG { char foo[999]; const UChar *ss = strs[i+j].getTerminatedBuffer(); u_austrcpy(foo, ss); debugfprintf((stderr, "VCF " ICUGLUE_VER_STR " appending [%d+%d=%d] <<%s>>\n", i, j, i+j, foo)); } #endif } return OICU_ucol_countAvailable(); }
U_CAPI UFILE* U_EXPORT2 u_fopen_u(const UChar *filename, const char *perm, const char *locale, const char *codepage) { UFILE *result; char buffer[256]; u_austrcpy(buffer, filename); result = u_fopen(buffer, perm, locale, codepage); #if U_PLATFORM_USES_ONLY_WIN32_API /* Try Windows API _wfopen if the above fails. */ if (!result) { FILE *systemFile = _wfopen(filename, (UChar*)perm); if (systemFile) { result = finit_owner(systemFile, locale, codepage, TRUE); } if (!result) { /* Something bad happened. Maybe the converter couldn't be opened. */ fclose(systemFile); } } #endif return result; /* not a file leak */ }
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(); }
char *austrdup(const UChar* unichars) { int length; char *newString; length = u_strlen ( unichars ); /*newString = (char*)malloc ( sizeof( char ) * 4 * ( length + 1 ) );*/ /* this leaks for now */ newString = (char*)ctst_malloc ( sizeof( char ) * 4 * ( length + 1 ) ); /* this shouldn't */ if ( newString == NULL ) return NULL; u_austrcpy ( newString, unichars ); return newString; }
/*Extracts the UChar* to a char* and calls through createConverter */ UConverter* ucnv_openU (const UChar * name, UErrorCode * err) { char asciiName[MAX_CONVERTER_NAME_LENGTH]; if (U_FAILURE (*err)) return NULL; if (name == NULL) return ucnv_open (NULL, err); if (u_strlen (name) > MAX_CONVERTER_NAME_LENGTH) { *err = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } return ucnv_open (u_austrcpy (asciiName, name), err); }
int32_t GLUE_SYM ( DateFormat ) :: appendAvailable(UnicodeString* strs, int32_t i, int32_t /*count*/) { int avail = OICU_udat_countAvailable(); UErrorCode status = U_ZERO_ERROR; OICU_u_init(&status); #if DATE_FE_DEBUG fprintf(stderr, "VCF " ICUGLUE_VER_STR " avail %d - init %s\n", avail, u_errorName(status)); #endif for(int j=0;j<avail;j++) { strs[i+j].append(OICU_udat_getAvailable(j)); strs[i+j].append("@sp=icu"); strs[i+j].append( ICUGLUE_VER_STR[0] ); // X_y strs[i+j].append( ICUGLUE_VER_STR[2] ); // x_Y #if DATE_FE_DEBUG { char foo[999]; const UChar *ss = strs[i+j].getTerminatedBuffer(); u_austrcpy(foo, ss); // fprintf(stderr, "VCF " ICUGLUE_VER_STR " appending [%d+%d=%d] <<%s>>\n", i, j, i+j, foo); } #endif } return OICU_ucol_countAvailable(); }
static void TestDateFormatCalendar() { UDateFormat *date=0, *time=0, *full=0; UCalendar *cal=0; UChar buf[256]; char cbuf[256]; int32_t pos; UDate when; UErrorCode ec = U_ZERO_ERROR; ctest_setTimeZone(NULL, &ec); /* Create a formatter for date fields. */ date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec); if (U_FAILURE(ec)) { log_data_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s (Are you missing data?)\n", u_errorName(ec)); goto FAIL; } /* Create a formatter for time fields. */ time = udat_open(UDAT_SHORT, UDAT_NONE, "en_US", NULL, 0, NULL, 0, &ec); if (U_FAILURE(ec)) { log_err("FAIL: udat_open(SHORT, NONE, en_US) failed with %s\n", u_errorName(ec)); goto FAIL; } /* Create a full format for output */ full = udat_open(UDAT_FULL, UDAT_FULL, "en_US", NULL, 0, NULL, 0, &ec); if (U_FAILURE(ec)) { log_err("FAIL: udat_open(FULL, FULL, en_US) failed with %s\n", u_errorName(ec)); goto FAIL; } /* Create a calendar */ cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &ec); if (U_FAILURE(ec)) { log_err("FAIL: ucal_open(en_US) failed with %s\n", u_errorName(ec)); goto FAIL; } /* Parse the date */ ucal_clear(cal); u_uastrcpy(buf, "4/5/2001"); pos = 0; udat_parseCalendar(date, cal, buf, -1, &pos, &ec); if (U_FAILURE(ec)) { log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n", pos, u_errorName(ec)); goto FAIL; } /* Parse the time */ u_uastrcpy(buf, "5:45 PM"); pos = 0; udat_parseCalendar(time, cal, buf, -1, &pos, &ec); if (U_FAILURE(ec)) { log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n", pos, u_errorName(ec)); goto FAIL; } /* Check result */ when = ucal_getMillis(cal, &ec); if (U_FAILURE(ec)) { log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec)); goto FAIL; } udat_format(full, when, buf, sizeof(buf), NULL, &ec); if (U_FAILURE(ec)) { log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec)); goto FAIL; } u_austrcpy(cbuf, buf); /* Thursday, April 5, 2001 5:45:00 PM PDT 986517900000 */ if (when == 986517900000.0) { log_verbose("Ok: Parsed result: %s\n", cbuf); } else { log_err("FAIL: Parsed result: %s, exp 4/5/2001 5:45 PM\n", cbuf); } FAIL: udat_close(date); udat_close(time); udat_close(full); ucal_close(cal); ctest_resetTimeZone(); }
int main (void) { const char *foo = "GG硬硬der"; puts("converting between char * and UChar * with u_*strcpy"); UChar *dst = (UChar *)malloc(sizeof(UChar) * 10); u_uastrcpy (dst, foo); u_printf("cpy C->U %S %d\n", dst, u_strlen(dst)); char *bar = (char *)malloc(sizeof(char) * 50); u_austrcpy (bar, dst); printf("cpy U->C %s %lu\n", bar, strlen(bar)); puts("write/read file test"); FILE *f = fopen("gg", "w"); fputs(bar, f); fclose(f); free(bar); bar = NULL; bar = (char *)malloc(sizeof(char) * 50); f = fopen("gg", "r"); fgets(bar, 100, f); puts("char *"); printf("%s %lu\n", bar, strlen(bar)); u_uastrcpy (dst, bar); puts("UChar *"); u_printf("%S %d\n", dst, u_strlen(dst)); fclose(f); system("rm gg"); free(dst); free(bar); puts("structure file write/read test"); U_STRING_DECL(fmt, "工具人 %d 號四我", 30); f = fopen("qq", "w"); for (int i = 0; i < 10; i++) { struct FOO *foo = (struct FOO *)malloc(sizeof(struct FOO)); sprintf(foo->name, "tool %d", i); u_sprintf_u(foo->nick, fmt, i); _write_struct_from_file (foo, sizeof(struct FOO), i, f); free(foo); } fclose(f); f = fopen("qq", "r"); for (int i = 0; i < 10; i++) { struct FOO *foo = (struct FOO *)_read_struct_from_file (sizeof(struct FOO), i, f); u_printf("[%s] : %S\n", foo->name, foo->nick); free(foo); } fclose(f); puts("randomly fseek"); srand(time(0)); f = fopen("qq", "r"); for (int i = 0; i < 1000000; i++) { unsigned r = ((rand() % 10) +i) % 10; printf("# %d foo[%d]: ", i, r); struct FOO *foo = (struct FOO *)_read_struct_from_file (sizeof(struct FOO), r, f); u_printf("[%s] : %S\n", foo->name, foo->nick); fflush(stdout); free(foo); } fclose(f); return 0; }
/* 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(); }
/** * Test the handling of the currency symbol in patterns. */ static void TestCurrencySign(void) { int32_t lneed; UNumberFormat *fmt; UChar *pattern=NULL; UChar *str=NULL; UChar *pat=NULL; UChar *res=NULL; UErrorCode status = U_ZERO_ERROR; char tempBuf[256]; pattern=(UChar*)malloc(sizeof(UChar) * (strlen("*#,##0.00;-*#,##0.00") + 1) ); u_uastrcpy(pattern, "*#,##0.00;-*#,##0.00"); pattern[0]=pattern[11]=0xa4; /* insert latin-1 currency symbol */ fmt = unum_open(UNUM_IGNORE,pattern, u_strlen(pattern), "en_US",NULL, &status); if(U_FAILURE(status)){ log_err_status(status, "Error in number format construction with pattern \"\\xA4#,##0.00;-\\xA4#,##0.00\\\" -> %s\n", u_errorName(status)); } lneed=0; lneed=unum_formatDouble(fmt, 1234.56, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_formatDouble(fmt, 1234.56, str, lneed+1, NULL, &status); } if(U_FAILURE(status)) { log_err_status(status, "Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); } lneed=0; lneed=unum_toPattern(fmt, FALSE, NULL, lneed, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; pat=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_formatDouble(fmt, FALSE, pat, lneed+1, NULL, &status); } log_verbose("Pattern \" %s \" \n", u_austrcpy(tempBuf, pat)); log_verbose("Format 1234.56 -> %s\n", u_austrcpy(tempBuf, str) ); if(U_SUCCESS(status) && str) { res=(UChar*)malloc(sizeof(UChar) * (strlen("$1,234.56")+1) ); u_uastrcpy(res, "$1,234.56"); if (u_strcmp(str, res) !=0) log_data_err("FAIL: Expected $1,234.56\n"); } else { log_err_status(status, "Error formatting -> %s\n", u_errorName(status)); } free(str); free(res); free(pat); lneed=0; lneed=unum_formatDouble(fmt, -1234.56, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_formatDouble(fmt, -1234.56, str, lneed+1, NULL, &status); } if(U_FAILURE(status)) { log_err_status(status, "Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); } if(str) { res=(UChar*)malloc(sizeof(UChar) * (strlen("-$1,234.56")+1) ); u_uastrcpy(res, "-$1,234.56"); if (u_strcmp(str, res) != 0) log_data_err("FAIL: Expected -$1,234.56\n"); free(str); free(res); } unum_close(fmt); free(pattern); }
/* Test whether apps and ICU can each have their own root.res */ static void TestAppData() { UResourceBundle *icu, *app; UResourceBundle *tmp = NULL; UResourceBundle *tmp2 = NULL; const UChar *appString; const UChar *icuString; int32_t len; UErrorCode status = U_ZERO_ERROR; char testMsgBuf[256]; const char* testPath=loadTestData(&status); icu = ures_open(NULL, "root", &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't open root ICU bundle- %s", __FILE__, __LINE__, u_errorName(status)); return; } /* log_info("Open icu root: %s size_%d\n", u_errorName(status), ures_getSize(icu)); */ status = U_ZERO_ERROR; app = ures_open(testPath, "root", &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't open app ICU bundle [%s]- %s", __FILE__, __LINE__, testPath, u_errorName(status)); return; } /* log_info("Open app: %s, size %d\n", u_errorName(status), ures_getSize(app)); */ tmp = ures_getByKey(icu, "Version", tmp, &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't get Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); return; } icuString = ures_getString(tmp, &len, &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't get string from Version string from ICU root bundle- %s", __FILE__, __LINE__, u_errorName(status)); return; } /* log_info("icuString=%p - %s\n", icuString, austrdup(icuString)); */ tmp2 = ures_getByKey(app, "Version", tmp2, &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't get Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); return; } appString = ures_getString(tmp2, &len, &status); if(U_FAILURE(status)) { log_err("%s:%d: Couldn't get string from Version string from App root bundle- %s", __FILE__, __LINE__, u_errorName(status)); return; } /* log_info("appString=%p - %s\n", appString, austrdup(appString)); */ if(!u_strcmp(icuString, appString)) { log_err("%s:%d: Error! Expected ICU and App root version strings to be DIFFERENT but they are both %s and %s\n", __FILE__, __LINE__, austrdup(icuString), austrdup(appString)); } else { log_verbose("%s:%d: appstr=%s, icustr=%s\n", __FILE__, __LINE__, u_austrcpy(testMsgBuf, appString), u_austrcpy(testMsgBuf, icuString)); } ures_close(tmp); ures_close(tmp2); ures_close(icu); ures_close(app); }
/* Test the handling of quotes*/ static void TestQuotes(void) { int32_t lneed; UErrorCode status=U_ZERO_ERROR; UChar pat[15]; UChar res[15]; UChar *str=NULL; UNumberFormat *fmt; char tempBuf[256]; log_verbose("\nTestting the handling of quotes in number format\n"); u_uastrcpy(pat, "a'fo''o'b#"); fmt =unum_open(UNUM_IGNORE,pat, u_strlen(pat), "en_US",NULL, &status); if(U_FAILURE(status)){ log_err_status(status, "Error in number format costruction using pattern \"a'fo''o'b#\" -> %s\n", u_errorName(status)); } lneed=0; lneed=unum_format(fmt, 123, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_format(fmt, 123, str, lneed+1, NULL, &status); } if(U_FAILURE(status) || !str) { log_err_status(status, "Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); return; } log_verbose("Pattern \"%s\" \n", u_austrcpy(tempBuf, pat) ); log_verbose("Format 123 -> %s\n", u_austrcpy(tempBuf, str) ); u_uastrcpy(res, "afo'ob123"); if(u_strcmp(str, res) != 0) log_err("FAIL: Expected afo'ob123"); free(str); unum_close(fmt); u_uastrcpy(pat, ""); u_uastrcpy(pat, "a''b#"); fmt =unum_open(UNUM_IGNORE,pat, u_strlen(pat), "en_US",NULL, &status); if(U_FAILURE(status)){ log_err("Error in number format costruction using pattern \"a''b#\"\n"); } lneed=0; lneed=unum_format(fmt, 123, NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_format(fmt, 123, str, lneed+1, NULL, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); } log_verbose("Pattern \"%s\" \n", u_austrcpy(tempBuf, pat) ); log_verbose("Format 123 -> %s\n", u_austrcpy(tempBuf, str) ); u_uastrcpy(res, ""); u_uastrcpy(res, "a'b123"); if(u_strcmp(str, res) != 0) log_err("FAIL: Expected a'b123\n"); free(str); unum_close(fmt); }
/* Test exponential pattern*/ static void TestExponential(void) { int32_t pat_length, val_length, lval_length; int32_t ival, ilval, p, v, lneed; UNumberFormat *fmt; int32_t ppos; UChar *upat; UChar pattern[20]; UChar *str=NULL; UChar uvalfor[20], ulvalfor[20]; char tempMsgBug[256]; double a; UErrorCode status = U_ZERO_ERROR; #if U_PLATFORM == U_PF_OS390 static const double val[] = { 0.01234, 123456789, 1.23e75, -3.141592653e-78 }; #else static const double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 }; #endif static const char* pat[] = { "0.####E0", "00.000E00", "##0.######E000", "0.###E0;[0.###E0]" }; static const int32_t lval[] = { 0, -1, 1, 123456789 }; static const char* valFormat[] = { "1.234E-2", "1.2346E8", "1.23E300", "-3.1416E-271", "12.340E-03", "12.346E07", "12.300E299", "-31.416E-272", "12.34E-003", "123.4568E006", "1.23E300", "-314.1593E-273", "1.234E-2", "1.235E8", "1.23E300", "[3.142E-271]" }; static const char* lvalFormat[] = { "0E0", "-1E0", "1E0", "1.2346E8", "00.000E00", "-10.000E-01", "10.000E-01", "12.346E07", "0E000", "-1E000", "1E000", "123.4568E006", "0E0", "[1E0]", "1E0", "1.235E8" }; static const double valParse[] = { #if U_PLATFORM == U_PF_OS390 0.01234, 123460000, 1.23E75, -3.1416E-78, 0.01234, 123460000, 1.23E75, -3.1416E-78, 0.01234, 123456800, 1.23E75, -3.141593E-78, 0.01234, 123500000, 1.23E75, -3.142E-78 #else /* We define the whole IEEE 754 number in the 4th column because Visual Age 7 has a bug in rounding numbers. */ 0.01234, 123460000, 1.23E300, -3.1415999999999999E-271, 0.01234, 123460000, 1.23E300, -3.1415999999999999E-271, 0.01234, 123456800, 1.23E300, -3.1415929999999999E-271, 0.01234, 123500000, 1.23E300, -3.1420000000000001E-271 #endif }; static const int32_t lvalParse[] = { 0, -1, 1, 123460000, 0, -1, 1, 123460000, 0, -1, 1, 123456800, 0, -1, 1, 123500000 }; pat_length = UPRV_LENGTHOF(pat); val_length = UPRV_LENGTHOF(val); lval_length = UPRV_LENGTHOF(lval); ival = 0; ilval = 0; for (p=0; p < pat_length; ++p) { upat=(UChar*)malloc(sizeof(UChar) * (strlen(pat[p])+1) ); u_uastrcpy(upat, pat[p]); fmt=unum_open(UNUM_IGNORE,upat, u_strlen(upat), "en_US",NULL, &status); if (U_FAILURE(status)) { log_err_status(status, "FAIL: Bad status returned by Number format construction with pattern %s -> %s\n", pat[p], u_errorName(status)); continue; } lneed= u_strlen(upat) + 1; unum_toPattern(fmt, FALSE, pattern, lneed, &status); log_verbose("Pattern \" %s \" -toPattern-> \" %s \" \n", upat, u_austrcpy(tempMsgBug, pattern) ); for (v=0; v<val_length; ++v) { /*format*/ lneed=0; lneed=unum_formatDouble(fmt, val[v], NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_formatDouble(fmt, val[v], str, lneed+1, NULL, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); } u_uastrcpy(uvalfor, valFormat[v+ival]); if(u_strcmp(str, uvalfor) != 0) log_verbose("FAIL: Expected %s ( %s )\n", valFormat[v+ival], u_austrcpy(tempMsgBug, uvalfor) ); /*parsing*/ ppos=0; a=unum_parseDouble(fmt, str, u_strlen(str), &ppos, &status); if (ppos== u_strlen(str)) { if (a != valParse[v+ival]) log_err("FAIL: Expected: %e, Got: %g\n", valParse[v+ival], a); } else log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a); free(str); } for (v=0; v<lval_length; ++v) { /*format*/ lneed=0; lneed=unum_formatDouble(fmt, lval[v], NULL, lneed, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; str=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); unum_formatDouble(fmt, lval[v], str, lneed+1, NULL, &status); } if(U_FAILURE(status)) { log_err("Error in formatting using unum_format(.....): %s\n", myErrorName(status) ); } /*printf(" Format %e -> %s\n", lval[v], austrdup(str) );*/ u_uastrcpy(ulvalfor, lvalFormat[v+ilval]); if(u_strcmp(str, ulvalfor) != 0) log_err("FAIL: Expected %s ( %s )\n", valFormat[v+ilval], austrdup(ulvalfor) ); /*parsing*/ ppos=0; a=unum_parseDouble(fmt, str, u_strlen(str), &ppos, &status); if (ppos== u_strlen(str)) { /*printf(" Parse -> %e\n", a);*/ if (a != lvalParse[v+ilval]) log_err("FAIL: Expected : %e\n", valParse[v+ival]); } else log_err(" FAIL: Partial parse ( %d chars ) -> %e\n", ppos, a); free(str); } ival += val_length; ilval += lval_length; unum_close(fmt); free(upat); } }
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 }
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 }
void TestMessageCatalog(void) { UErrorCode ec = U_ZERO_ERROR; u_nl_catd catd; const char* DATA[] = { /* set_num, msg_num, expected string result, expected error code */ "1", "4", "Good morning.", "U_ZERO_ERROR", "1", "5", "Good afternoon.", "U_ZERO_ERROR", "1", "6", "FAIL", "U_MISSING_RESOURCE_ERROR", "1", "7", "Good evening.", "U_ZERO_ERROR", "1", "8", "Good night.", "U_ZERO_ERROR", "1", "9", "FAIL", "U_MISSING_RESOURCE_ERROR", "3", "1", "FAIL", "U_MISSING_RESOURCE_ERROR", "4", "14", "Please ", "U_ZERO_ERROR", "4", "15", "FAIL", "U_MISSING_RESOURCE_ERROR", "4", "19", "Thank you.", "U_ZERO_ERROR", "4", "20", "Sincerely,", "U_ZERO_ERROR", NULL }; const UChar FAIL[] = {0x46, 0x41, 0x49, 0x4C, 0x00}; /* "FAIL" */ int32_t i; const char *path = loadTestData(&ec); if (U_FAILURE(ec)) { log_data_err("FAIL: loadTestData => %s\n", u_errorName(ec)); return; } catd = u_catopen(path, "mc", &ec); if (U_FAILURE(ec)) { log_data_err("FAIL: u_catopen => %s\n", u_errorName(ec)); return; } for (i=0; DATA[i]!=NULL; i+=4) { int32_t set_num = T_CString_stringToInteger(DATA[i], 10); int32_t msg_num = T_CString_stringToInteger(DATA[i+1], 10); UChar exp[128]; int32_t len = -1; const char* err; char str[128]; const UChar* ustr; u_uastrcpy(exp, DATA[i+2]); ec = U_ZERO_ERROR; ustr = u_catgets(catd, set_num, msg_num, FAIL, &len, &ec); u_austrcpy(str, ustr); err = u_errorName(ec); log_verbose("u_catgets(%d, %d) => \"%s\", len=%d, %s\n", set_num, msg_num, str, len, err); if (u_strcmp(ustr, exp) != 0) { log_err("FAIL: u_catgets => \"%s\", exp. \"%s\"\n", str, DATA[i+2]); } if (len != (int32_t) uprv_strlen(DATA[i+2])) { log_err("FAIL: u_catgets => len=%d, exp. %d\n", len, uprv_strlen(DATA[i+2])); } if (uprv_strcmp(err, DATA[i+3]) != 0) { log_err("FAIL: u_catgets => %s, exp. %s\n", err, DATA[i+3]); } } u_catclose(catd); }
static void uprintf(const UChar* str) { char buf[256]; u_austrcpy(buf, str); printf("%s", buf); }
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 }
void Test_GEec(void) { UErrorCode status = U_ZERO_ERROR; UDateFormat * dtfmt = udat_open(UDAT_LONG, UDAT_LONG, "en", zonePST, -1, NULL, 0, &status); if ( U_SUCCESS(status) ) { const DatePatternAndText *patTextPtr; for (patTextPtr = datePatternsAndText; patTextPtr->pattern != NULL; ++patTextPtr) { UChar dmyGnText[DATE_TEXT_MAX_CHARS]; char byteText[3*DATE_TEXT_MAX_CHARS]; int32_t dmyGnTextLen; UDate dateResult; udat_applyPattern(dtfmt, FALSE, patTextPtr->pattern, -1); dmyGnTextLen = udat_format(dtfmt, july022008, dmyGnText, DATE_TEXT_MAX_CHARS, NULL, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udat_format with %s: %s\n", patTextPtr->label, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strcmp(dmyGnText, patTextPtr->text) != 0 ) { log_err("FAIL: udat_format with %s: wrong UChar[] result %s\n", patTextPtr->label, u_austrcpy(byteText,dmyGnText) ); } dateResult = udat_parse(dtfmt, patTextPtr->text, -1, NULL, &status); /* no time, dateResult != july022008 by some hours */ if ( U_FAILURE(status) ) { log_err("FAIL: udat_parse with %s: %s\n", patTextPtr->label, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( patTextPtr->label[0] != '*' && july022008 - dateResult > dayMillisec ) { log_err("FAIL: udat_parse with %s: wrong UDate result\n", patTextPtr->label ); } } udat_close(dtfmt); } else { log_data_err("FAIL: udat_open fails: %s (Are you missing data?)\n", myErrorName(status)); } }
static void _expect(const UTransliterator* trans, const char* cfrom, const char* cto) { /* u_uastrcpy has no capacity param for the buffer -- so just * make all buffers way too big */ enum { CAP = 256 }; UChar from[CAP]; UChar to[CAP]; UChar buf[CAP]; const UChar *ID; int32_t IDLength; const char *id; UErrorCode status = U_ZERO_ERROR; int32_t limit; UTransPosition pos; XReplaceable xrep; XReplaceable *xrepPtr = &xrep; UReplaceableCallbacks xrepVtable; u_uastrcpy(from, cfrom); u_uastrcpy(to, cto); ID = utrans_getUnicodeID(trans, &IDLength); id = aescstrdup(ID, IDLength); /* utrans_transUChars() */ u_strcpy(buf, from); limit = u_strlen(buf); utrans_transUChars(trans, buf, NULL, CAP, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } /* utrans_transIncrementalUChars() */ u_strcpy(buf, from); pos.start = pos.contextStart = 0; pos.limit = pos.contextLimit = u_strlen(buf); utrans_transIncrementalUChars(trans, buf, NULL, CAP, &pos, &status); utrans_transUChars(trans, buf, NULL, CAP, pos.start, &pos.limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transIncrementalUChars() failed, error=%s\n", u_errorName(status)); return; } if (0 == u_strcmp(buf, to)) { log_verbose("Ok: utrans_transIncrementalUChars(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, buf); log_err("FAIL: utrans_transIncrementalUChars(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } /* utrans_trans() */ InitXReplaceableCallbacks(&xrepVtable); InitXReplaceable(&xrep, cfrom); limit = u_strlen(from); utrans_trans(trans, (UReplaceable*)xrepPtr, &xrepVtable, 0, &limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_trans() failed, error=%s\n", u_errorName(status)); FreeXReplaceable(&xrep); return; } if (0 == u_strcmp(xrep.text, to)) { log_verbose("Ok: utrans_trans(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, xrep.text); log_err("FAIL: utrans_trans(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } FreeXReplaceable(&xrep); /* utrans_transIncremental() */ InitXReplaceable(&xrep, cfrom); pos.start = pos.contextStart = 0; pos.limit = pos.contextLimit = u_strlen(from); utrans_transIncremental(trans, (UReplaceable*)xrepPtr, &xrepVtable, &pos, &status); utrans_trans(trans, (UReplaceable*)xrepPtr, &xrepVtable, pos.start, &pos.limit, &status); if (U_FAILURE(status)) { log_err("FAIL: utrans_transIncremental() failed, error=%s\n", u_errorName(status)); FreeXReplaceable(&xrep); return; } if (0 == u_strcmp(xrep.text, to)) { log_verbose("Ok: utrans_transIncremental(%s) x %s -> %s\n", id, cfrom, cto); } else { char actual[CAP]; u_austrcpy(actual, xrep.text); log_err("FAIL: utrans_transIncremental(%s) x %s -> %s, expected %s\n", id, cfrom, actual, cto); } FreeXReplaceable(&xrep); }