void Test714(void) { UDate d=978103543000.0; UChar temp[20]; UErrorCode status = U_ZERO_ERROR; UDateFormat *fmt; UChar *result; const char* expect = "7:25:43 AM"; ctest_setTimeZone(NULL, &status); fmt= udat_open(UDAT_MEDIUM,UDAT_NONE ,"en_US_CA", NULL, -1, NULL, 0, &status); if(U_FAILURE(status)) { log_data_err("FAIL: error in creating the dateformat using medium time style and NO date style: %s (Are you missing data?)\n", myErrorName(status)); return; } result =myFormatit(fmt, d); if(!result) { log_data_err("Fail: could not format - exitting test\n"); return; } u_uastrcpy(temp, expect); if (u_strcmp(result, temp)!=0) { log_err("Fail: %s != %s\n", austrdup(result), expect); } else { log_verbose("Ok: %s == %s\n", austrdup(result), expect ); } udat_close(fmt); ctest_resetTimeZone(); }
void TestTwoDigitYearDSTParse() { UDateFormat *fullFmt, *fmt; UErrorCode status = U_ZERO_ERROR; UChar *pattern; UDate d; UChar *s; int32_t pos; ctest_setTimeZone(NULL, &status); pattern=(UChar*)malloc(sizeof(UChar) * (strlen("EEE MMM dd HH:mm:ss.SSS zzz yyyy G")+1 )); u_uastrcpy(pattern, "EEE MMM dd HH:mm:ss.SSS zzz yyyy G"); fullFmt= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US",NULL,0,pattern, u_strlen(pattern),&status); if(U_FAILURE(status)) { log_err_status(status, "FAIL: Error in creating a date format using udat_openPattern %s\n", myErrorName(status) ); } else { log_verbose("PASS: creating dateformat using udat_openPattern() succesful\n"); u_uastrcpy(pattern, "dd-MMM-yy h:mm:ss 'o''clock' a z"); fmt= udat_open(UDAT_IGNORE,UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status); s=(UChar*)malloc(sizeof(UChar) * (strlen("03-Apr-04 2:20:47 o'clock AM PST")+1) ); u_uastrcpy(s, "03-Apr-04 2:20:47 o'clock AM PST"); pos=0; d = udat_parse(fmt, s, u_strlen(s), &pos, &status); if (U_FAILURE(status)) { log_err("FAIL: Could not parse \"%s\"\n", austrdup(s)); } else { UCalendar *cal = ucal_open(NULL, 0, uloc_getDefault(), UCAL_TRADITIONAL, &status); if (U_FAILURE(status)) { log_err_status(status, "FAIL: Could not open calendar: %s\n", u_errorName(status)); } else { int32_t h; ucal_setMillis(cal, d, &status); h = ucal_get(cal, UCAL_HOUR_OF_DAY, &status); if (U_FAILURE(status)) { log_err("FAIL: Some calendar operations failed"); } else if (h != 2) { log_err("FAIL: Parse of \"%s\" returned HOUR_OF_DAY %d\n", austrdup(s), h); } ucal_close(cal); } } udat_close(fullFmt); udat_close(fmt); free(s); } free(pattern); ctest_resetTimeZone(); }
void TestQuotePattern161() { UDateFormat *format; UCalendar *cal; UDate currentTime_1; UChar *pattern, *tzID, *exp; UChar *dateString; UErrorCode status = U_ZERO_ERROR; const char* expStr = "04/13/1999 at 10:42:28 AM "; ctest_setTimeZone(NULL, &status); pattern=(UChar*)malloc(sizeof(UChar) * (strlen("MM/dd/yyyy 'at' hh:mm:ss a zzz")+1) ); u_uastrcpy(pattern, "MM/dd/yyyy 'at' hh:mm:ss a zzz"); /* 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" */ /* format= udat_openPattern(pattern, u_strlen(pattern), NULL, &status); */ format= udat_open(UDAT_IGNORE, UDAT_IGNORE,"en_US", NULL, 0,pattern, u_strlen(pattern), &status); if(U_FAILURE(status)){ log_err_status(status, "error in udat_open: %s\n", myErrorName(status)); return; } tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "PST"); /* 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" */ /* cal=ucal_open(tzID, u_strlen(tzID), NULL, UCAL_TRADITIONAL, &status); */ cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_err("error in ucal_open cal : %s\n", myErrorName(status)); } ucal_setDateTime(cal, 1999, UCAL_APRIL, 13, 10, 42, 28, &status); currentTime_1 = ucal_getMillis(cal, &status); dateString = myDateFormat(format, currentTime_1); exp=(UChar*)malloc(sizeof(UChar) * (strlen(expStr) + 1) ); u_uastrcpy(exp, expStr); log_verbose("%s\n", austrdup(dateString) ); if(u_strncmp(dateString, exp, (int32_t)strlen(expStr)) !=0) log_err("Error in formatting a pattern with single quotes\n"); udat_close(format); ucal_close(cal); free(exp); free(tzID); free(pattern); ctest_resetTimeZone(); }
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(); }
/* Test u_vformatMessage() with various test patterns. */ static void TestMessageFormatWithValist( void ) { UChar *str; UChar* result; int32_t resultLengthOut,resultlength,i, patternlength; UErrorCode status = U_ZERO_ERROR; UDate d1=1000000000.0; ctest_setTimeZone(NULL, &status); str=(UChar*)malloc(sizeof(UChar) * 7); u_uastrcpy(str, "MyDisk"); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("Testing u_formatMessage90\n"); InitStrings(); for (i = 0; i < cnt_testCases; i++) { status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultLengthOut=CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result,sizeof(UChar) * resultlength); CallFormatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); } else if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } } free(result); free(str); FreeStrings(); ctest_resetTimeZone(); }
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(); }
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(); }
/* 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 u_formatMessage() with various test patterns() */ static void MessageFormatTest( void ) { UChar *str; UChar* result; int32_t resultLengthOut,resultlength,i, patternlength; UErrorCode status = U_ZERO_ERROR; UDate d1=1000000000.0; ctest_setTimeZone(NULL, &status); str=(UChar*)malloc(sizeof(UChar) * 7); u_uastrncpy(str, "MyDisk", 7); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("Testing u_formatMessage()\n"); InitStrings(); for (i = 0; i < cnt_testCases; i++) { status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultLengthOut=u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result,sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); continue; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } } free(result); result = NULL; free(str); { for (i = 0; i < cnt_testCases; i++) { UParseError parseError; status=U_ZERO_ERROR; patternlength=u_strlen(testCasePatterns[i]); resultlength=0; resultLengthOut=u_formatMessageWithError( "en_US",testCasePatterns[i], patternlength, result, resultlength, &parseError,&status, 1, 3456.00, d1); if(status== U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)malloc(sizeof(UChar) * resultlength); u_formatMessage( "en_US",testCasePatterns[i], patternlength, result, resultlength, &status, 1, 3456.00, d1); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format on testcase %d: %s (Are you missing data?)\n", i, myErrorName(status) ); continue; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } free(result); result=NULL; } } { UErrorCode ec = U_ZERO_ERROR; int32_t patternLength = u_strlen(testCasePatterns[0]); UMessageFormat formatter = umsg_open(testCasePatterns[0],patternLength,"en_US",NULL,&ec); if(U_FAILURE(ec)){ log_data_err("umsg_open() failed for testCasePattens[%d]. -> %s (Are you missing data?)\n",i, u_errorName(ec)); return; } for(i = 0;i<cnt_testCases; i++){ UParseError parseError; int32_t resultLength =0,count=0; int32_t one=0; int32_t two=0; UDate d2=0; result=NULL; patternLength = u_strlen(testCasePatterns[i]); umsg_applyPattern(formatter,testCasePatterns[i],patternLength,&parseError,&ec); if(U_FAILURE(ec)){ log_err("umsg_applyPattern() failed for testCasePattens[%d].\n",i); return; } /* pre-flight */ resultLength = umsg_format(formatter,result,resultLength,&ec,1,3456.00,d1); if(ec==U_BUFFER_OVERFLOW_ERROR){ ec=U_ZERO_ERROR; result = (UChar*) malloc(U_SIZEOF_UCHAR*resultLength+2); resultLength = umsg_format(formatter,result,resultLength+2,&ec,1,3456.00,d1); if(U_FAILURE(ec)){ log_err("ERROR: failure in message format on testcase %d: %s\n", i, u_errorName(status) ); free(result); return; } if(u_strcmp(result, testResultStrings[i])==0){ log_verbose("PASS: MessagFormat successful on testcase : %d\n", i); } else{ log_err("FAIL: Error in MessageFormat on testcase : %d\n GOT %s EXPECTED %s\n", i, austrdup(result), austrdup(testResultStrings[i]) ); } if (returnsNullForType(1, (double)2.0)) { /* HP/UX and possibly other platforms don't properly check for this case. We pass in a UDate, but the function expects a UDate *. When va_arg is used, most compilers will return NULL, but HP-UX won't do that and will return 2 in this case. This is a platform dependent test. This relies upon "undefined" behavior, as indicated by C99 7.15.1.1 paragraph 2 */ umsg_parse(formatter,result,resultLength,&count,&ec,one,two,d2); if(ec!=U_ILLEGAL_ARGUMENT_ERROR){ log_err("FAIL: Did not get expected error for umsg_parse(). Expected: U_ILLEGAL_ARGUMENT_ERROR Got: %s \n",u_errorName(ec)); }else{ ec = U_ZERO_ERROR; } } else { log_verbose("Warning: Returning NULL for a mismatched va_arg type isn't supported on this platform.\n", i); } umsg_parse(formatter,result,resultLength,&count,&ec,&one,&two,&d2); if(U_FAILURE(ec)){ log_err("umsg_parse could not parse the pattern. Error: %s.\n",u_errorName(ec)); } free(result); }else{ log_err("FAIL: Expected U_BUFFER_OVERFLOW error while preflighting got: %s for testCasePatterns[%d]",u_errorName(ec),i); } } umsg_close(formatter); } FreeStrings(); ctest_resetTimeZone(); }
/* Test u_formatMessage() and u_parseMessage() , format and parse sequence and round trip */ static void TestSampleFormatAndParse(void) { UChar *result, *tzID, *str; UChar pattern[100]; UChar expected[100]; int32_t resultLengthOut, resultlength; UCalendar *cal; UDate d1,d; UDateFormat *def1; UErrorCode status = U_ZERO_ERROR; int32_t value = 0; UChar ret[30]; ctest_setTimeZone(NULL, &status); log_verbose("Testing format and parse\n"); str=(UChar*)malloc(sizeof(UChar) * 25); u_uastrcpy(str, "disturbance in force"); tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "PST"); cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_data_err("error in ucal_open caldef : %s - (Are you missing data?)\n", myErrorName(status) ); } ucal_setDateTime(cal, 1999, UCAL_MARCH, 18, 0, 0, 0, &status); d1=ucal_getMillis(cal, &status); if(U_FAILURE(status)){ log_data_err("Error: failure in get millis: %s - (Are you missing data?)\n", myErrorName(status) ); } log_verbose("\nTesting with pattern test#4"); u_uastrcpy(pattern, "On {0, date, long}, there was a {1} on planet {2,number,integer}"); u_uastrcpy(expected, "On March 18, 1999, there was a disturbance in force on planet 7"); resultlength=1; result=(UChar*)malloc(sizeof(UChar) * resultlength); resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * resultlength); u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, d1, str, 7); } if(U_FAILURE(status)){ log_data_err("ERROR: failure in message format test#4: %s (Are you missing data?)\n", myErrorName(status)); } else if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#4\n"); else{ log_err("FAIL: Error in MessageFormat on test#4\n GOT: %s EXPECTED: %s\n", austrdup(result), austrdup(expected) ); } /*try to parse this and check*/ log_verbose("\nTesting the parse Message test#5\n"); u_parseMessage("en_US", pattern, u_strlen(pattern), result, u_strlen(result), &status, &d, ret, &value); if(U_FAILURE(status)){ log_data_err("ERROR: error in parsing: test#5: %s (Are you missing data?)\n", myErrorName(status)); } else if(value!=7 && u_strcmp(str,ret)!=0) log_err("FAIL: Error in parseMessage on test#5 \n"); else log_verbose("PASS: parseMessage successful on test#5\n"); def1 = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, NULL,0,&status); if(U_FAILURE(status)) { log_data_err("error in creating the dateformat using short date and time style: %s (Are you missing data?)\n", myErrorName(status)); }else{ if(u_strcmp(myDateFormat(def1, d), myDateFormat(def1, d1))==0) log_verbose("PASS: parseMessage successful test#5\n"); else{ log_err("FAIL: parseMessage didn't parse the date successfully\n GOT: %s EXPECTED %s\n", austrdup(myDateFormat(def1,d)), austrdup(myDateFormat(def1,d1)) ); } } udat_close(def1); ucal_close(cal); free(result); free(str); free(tzID); ctest_resetTimeZone(); }
/*test u_formatMessage() with sample patterns */ static void TestSampleMessageFormat(void) { UChar *str; UChar *result; UChar pattern[100], expected[100]; int32_t resultLengthOut, resultlength; UDate d = 837039928046.0; UErrorCode status = U_ZERO_ERROR; ctest_setTimeZone(NULL, &status); str=(UChar*)malloc(sizeof(UChar) * 15); u_uastrcpy(str, "abc"); u_uastrcpy(pattern, "There are {0} files on {1,date}"); u_uastrcpy(expected, "There are abc files on Jul 10, 1996"); result=(UChar*)malloc(sizeof(UChar) * 1); log_verbose("\nTesting a sample for Message format test#1\n"); resultlength=1; resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * resultlength); u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, d); } if(U_FAILURE(status)){ log_data_err("Error: failure in message format on test#1: %s (Are you missing data?)\n", myErrorName(status)); } else if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#1\n"); else{ log_err("FAIL: Error in MessageFormat on test#1 \n GOT: %s EXPECTED: %s\n", austrdup(result), austrdup(expected) ); } log_verbose("\nTesting message format with another pattern test#2\n"); u_uastrcpy(pattern, "The disk \"{0}\" contains {1,number,integer} file(s)"); u_uastrcpy(expected, "The disk \"MyDisk\" contains 23 file(s)"); u_uastrcpy(str, "MyDisk"); resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 235); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * (resultlength+1)); u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 23); } if(U_FAILURE(status)){ log_data_err("Error: failure in message format on test#2 : %s (Are you missing data?)\n", myErrorName(status)); } else if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#2\n"); else{ log_err("FAIL: Error in MessageFormat on test#2\n GOT: %s EXPECTED: %s\n", austrdup(result), austrdup(expected) ); } log_verbose("\nTesting message format with another pattern test#3\n"); u_uastrcpy(pattern, "You made a {0} of {1,number,currency}"); u_uastrcpy(expected, "You made a deposit of $500.00"); u_uastrcpy(str, "deposit"); resultlength=0; resultLengthOut=u_formatMessage( "en_US", pattern, u_strlen(pattern), NULL, resultlength, &status, str, 500.00); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultLengthOut+1; result=(UChar*)realloc(result, sizeof(UChar) * resultlength); u_formatMessage( "en_US", pattern, u_strlen(pattern), result, resultlength, &status, str, 500.00); } if(U_FAILURE(status)){ log_data_err("Error: failure in message format on test#3 : %s (Are you missing data?)\n", myErrorName(status)); } else if(u_strcmp(result, expected)==0) log_verbose("PASS: MessagFormat successful on test#3\n"); else{ log_err("FAIL: Error in MessageFormat on test#3\n GOT: %s EXPECTED %s\n", austrdup(result), austrdup(expected) ); } free(result); free(str); ctest_resetTimeZone(); }
void TestCzechMonths459() { int32_t lneed, pos; UChar *pattern=NULL, *tzID=NULL; UChar *juneStr, *julyStr; UDateFormat *fmt; UCalendar *cal; UDate june, july, d; UErrorCode status = U_ZERO_ERROR; UChar *date; ctest_setTimeZone(NULL, &status); fmt = udat_open(UDAT_FULL, UDAT_FULL, "cs", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)){ log_data_err("Error in constructing the date format -> %s (Are you missing data?)\n", u_errorName(status)); ctest_resetTimeZone(); return; } lneed=0; lneed=udat_toPattern(fmt, TRUE, NULL, lneed, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; pattern=(UChar*)malloc(sizeof(UChar) * (lneed+1) ); udat_toPattern(fmt, TRUE, pattern, lneed+1, &status); } if(U_FAILURE(status)){ log_err("Error in extracting the pattern\n"); } tzID=(UChar*)malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "GMT"); cal=ucal_open(tzID, u_strlen(tzID), "cs", UCAL_GREGORIAN, &status); if(U_FAILURE(status)){ log_err("error in ucal_open caldef : %s\n", myErrorName(status)); } ucal_setDate(cal, 1997, UCAL_JUNE, 15, &status); june=ucal_getMillis(cal, &status); ucal_setDate(cal, 1997, UCAL_JULY, 15, &status); july=ucal_getMillis(cal, &status); juneStr = myDateFormat(fmt, june); julyStr = myDateFormat(fmt, july); pos=0; if(juneStr == NULL) { log_data_err("Can't load juneStr. Quitting.\n"); return; } d = udat_parse(fmt, juneStr, u_strlen(juneStr), &pos, &status); date = myDateFormat(fmt, d); if(U_SUCCESS(status)){ UChar* out1 = myDateFormat(fmt, june); UChar* out2 = myDateFormat(fmt, d); if(u_strcmp(out1, out2) !=0) log_err("Error in handling the czech month june\n"); else log_verbose("Pass: Date = %s (czech month June)\n", aescstrdup(date, -1)); }else{ log_err("udat_parse failed. Error. %s\n",u_errorName(status)); } pos=0; d = udat_parse(fmt, julyStr, u_strlen(julyStr), &pos, &status); date = myDateFormat(fmt, d); if(u_strcmp(myDateFormat(fmt, july), myDateFormat(fmt, d) ) !=0) log_err("Error in handling the czech month july\n"); else log_verbose("Pass: Date = %s (czech month July)\n", aescstrdup(date, -1)); ctest_resetTimeZone(); udat_close(fmt); ucal_close(cal); free(pattern); free(tzID); }