/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2013, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** * * File CDATTST.C * * Modification History: * Name Description * Madhu Katragadda Creation ********************************************************************************* */ /* C API TEST FOR DATE FORMAT */ #include "unicode/utypes.h" #if !UCONFIG_NO_FORMATTING #include "unicode/uloc.h" #include "unicode/udat.h" #include "unicode/udatpg.h" #include "unicode/ucal.h" #include "unicode/unum.h" #include "unicode/ustring.h" #include "cintltst.h" #include "cdattst.h" #include "cformtst.h" #include "cmemory.h" #include static void TestExtremeDates(void); static void TestAllLocales(void); static void TestRelativeCrash(void); static void TestContext(void); static void TestCalendarDateParse(void); #define LEN(a) (sizeof(a)/sizeof(a[0])) void addDateForTest(TestNode** root); #define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x) void addDateForTest(TestNode** root) { TESTCASE(TestDateFormat); TESTCASE(TestRelativeDateFormat); TESTCASE(TestSymbols); TESTCASE(TestDateFormatCalendar); TESTCASE(TestExtremeDates); TESTCASE(TestAllLocales); TESTCASE(TestRelativeCrash); TESTCASE(TestContext); TESTCASE(TestCalendarDateParse); } /* 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= dateToUse)? dateResult - dateToUse: dateToUse - dateResult; if ( U_FAILURE(status) || dateDiff > minutesTolerance ) { log_err("udat_parse timeStyle SHORT dateStyle (%d | UDAT_RELATIVE) fails, error %s, expect approx %.1f, got %.1f, parsePos %d\n", *stylePtr, myErrorName(status), dateToUse, dateResult, parsePos ); status = U_ZERO_ERROR; } udat_format(fmtRelDate, dateToUse, strDate, kDateOrTimeOutMax, NULL, &status); if ( U_FAILURE(status) ) { log_err("udat_format timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s\n", *stylePtr, myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, strDate) == NULL ) { log_err("relative date string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } else { parsePos = 0; dateResult = udat_parse(fmtRelDate, strDate, -1, &parsePos, &status); dateDiff = (dateResult >= dateToUse)? dateResult - dateToUse: dateToUse - dateResult; if ( U_FAILURE(status) || dateDiff > daysTolerance ) { log_err("udat_parse timeStyle NONE dateStyle (%d | UDAT_RELATIVE) fails, error %s, expect approx %.1f, got %.1f, parsePos %d\n", *stylePtr, myErrorName(status), dateToUse, dateResult, parsePos ); status = U_ZERO_ERROR; } } udat_format(fmtTime, dateToUse, strTime, kDateOrTimeOutMax, NULL, &status); if ( U_FAILURE(status) ) { log_err("udat_format timeStyle SHORT dateStyle NONE fails, error %s\n", myErrorName(status) ); status = U_ZERO_ERROR; } else if ( u_strstr(strDateTime, strTime) == NULL ) { log_err("time string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } strPtr = u_strstr(strDateTime, minutesStr); if ( strPtr != NULL ) { int32_t beginIndex = strPtr - strDateTime; if ( fp.beginIndex != beginIndex ) { log_err("UFieldPosition beginIndex %d, expected %d, in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", fp.beginIndex, beginIndex, *stylePtr ); } } else { log_err("minutes string not found in udat_format timeStyle SHORT dateStyle (%d | UDAT_RELATIVE)\n", *stylePtr ); } } } udat_close(fmtRelDateTime); udat_close(fmtRelDate); udat_close(fmtTime); } } /*Testing udat_getSymbols() and udat_setSymbols() and udat_countSymbols()*/ static void TestSymbols() { UDateFormat *def, *fr; UErrorCode status = U_ZERO_ERROR; UChar *value=NULL; UChar *result = NULL; int32_t resultlength; int32_t resultlengthout; UChar *pattern; /*creating a dateformat with french locale */ log_verbose("\ncreating a date format with french locale\n"); fr = udat_open(UDAT_FULL, UDAT_DEFAULT, "fr_FR", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)) { log_data_err("error in creating the dateformat using full time style with french locale -> %s (Are you missing data?)\n", myErrorName(status) ); return; } /*creating a default dateformat */ log_verbose("\ncreating a date format with default locale\n"); /* 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_DEFAULT,UDAT_DEFAULT ,NULL, NULL, 0, &status); */ def = udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US", NULL, 0, NULL, 0, &status); if(U_FAILURE(status)) { log_err("error in creating the dateformat using short date and time style\n %s\n", myErrorName(status) ); return; } /*Testing countSymbols, getSymbols and setSymbols*/ log_verbose("\nTesting countSymbols\n"); /*since the month names has the last string empty and week names are 1 based 1.e first string in the weeknames array is empty */ if(udat_countSymbols(def, UDAT_ERAS)!=2 || udat_countSymbols(def, UDAT_MONTHS)!=12 || udat_countSymbols(def, UDAT_SHORT_MONTHS)!=12 || udat_countSymbols(def, UDAT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_SHORT_WEEKDAYS)!=8 || udat_countSymbols(def, UDAT_AM_PMS)!=2 || udat_countSymbols(def, UDAT_QUARTERS) != 4 || udat_countSymbols(def, UDAT_SHORT_QUARTERS) != 4 || udat_countSymbols(def, UDAT_LOCALIZED_CHARS)!=1 || udat_countSymbols(def, UDAT_SHORTER_WEEKDAYS)!=8) { log_err("FAIL: error in udat_countSymbols\n"); } else log_verbose("PASS: udat_countSymbols() successful\n"); /*testing getSymbols*/ log_verbose("\nTesting getSymbols\n"); pattern=(UChar*)malloc(sizeof(UChar) * 10); u_uastrcpy(pattern, "jeudi"); resultlength=0; resultlengthout=udat_getSymbols(fr, UDAT_WEEKDAYS, 5 , NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthout+1; if(result != NULL) { free(result); result = NULL; } result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(fr, UDAT_WEEKDAYS, 5, result, resultlength, &status); } if(U_FAILURE(status)) { log_err("FAIL: Error in udat_getSymbols().... %s\n", myErrorName(status) ); } else log_verbose("PASS: getSymbols succesful\n"); if(u_strcmp(result, pattern)==0) log_verbose("PASS: getSymbols retrieved the right value\n"); else log_data_err("FAIL: getSymbols retrieved the wrong value\n"); /*run series of tests to test getsymbols regressively*/ log_verbose("\nTesting getSymbols() regressively\n"); VerifygetSymbols(fr, UDAT_WEEKDAYS, 1, "dimanche"); VerifygetSymbols(def, UDAT_WEEKDAYS, 1, "Sunday"); VerifygetSymbols(fr, UDAT_SHORT_WEEKDAYS, 7, "sam."); VerifygetSymbols(fr, UDAT_SHORTER_WEEKDAYS, 7, "sa"); VerifygetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Sat"); VerifygetSymbols(def, UDAT_MONTHS, 11, "December"); VerifygetSymbols(def, UDAT_MONTHS, 0, "January"); VerifygetSymbols(fr, UDAT_ERAS, 0, "av. J.-C."); VerifygetSymbols(def, UDAT_AM_PMS, 0, "AM"); VerifygetSymbols(def, UDAT_AM_PMS, 1, "PM"); VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv."); VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec"); VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre"); VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter"); VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2"); VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3"); VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQqVUOXx"); if(result != NULL) { free(result); result = NULL; } free(pattern); log_verbose("\nTesting setSymbols\n"); /*applying the pattern so that setSymbolss works */ resultlength=0; resultlengthout=udat_toPattern(fr, FALSE, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthout + 1; pattern=(UChar*)malloc(sizeof(UChar) * resultlength); udat_toPattern(fr, FALSE, pattern, resultlength, &status); } if(U_FAILURE(status)) { log_err("FAIL: error in extracting the pattern from UNumberFormat\n %s\n", myErrorName(status) ); } udat_applyPattern(def, FALSE, pattern, u_strlen(pattern)); resultlength=0; resultlengthout=udat_toPattern(def, FALSE, NULL, resultlength,&status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthout + 1; if(result != NULL) { free(result); result = NULL; } result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_toPattern(fr, 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, pattern)==0) log_verbose("Pattern applied properly\n"); else log_err("pattern could not be applied properly\n"); free(pattern); /*testing set symbols */ resultlength=0; resultlengthout=udat_getSymbols(fr, UDAT_MONTHS, 11 , NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; resultlength=resultlengthout+1; if(result != NULL) { free(result); result = NULL; } result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(fr, UDAT_MONTHS, 11, result, resultlength, &status); } if(U_FAILURE(status)) log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) ); resultlength=resultlengthout+1; udat_setSymbols(def, UDAT_MONTHS, 11, result, resultlength, &status); if(U_FAILURE(status)) { log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) ); } else log_verbose("PASS: SetSymbols successful\n"); resultlength=0; resultlengthout=udat_getSymbols(def, UDAT_MONTHS, 11, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; resultlength=resultlengthout+1; value=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(def, UDAT_MONTHS, 11, value, resultlength, &status); } if(U_FAILURE(status)) log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n"); if(u_strcmp(result, value)!=0) log_data_err("FAIL: Error in settting and getting symbols\n"); else log_verbose("PASS: setSymbols successful\n"); /*run series of tests to test setSymbols regressively*/ log_verbose("\nTesting setSymbols regressively\n"); VerifysetSymbols(def, UDAT_ERAS, 0, "BeforeChrist"); VerifysetSymbols(def, UDAT_ERA_NAMES, 1, "AnnoDomini"); VerifysetSymbols(def, UDAT_WEEKDAYS, 1, "Sundayweek"); VerifysetSymbols(def, UDAT_SHORT_WEEKDAYS, 7, "Satweek"); VerifysetSymbols(def, UDAT_NARROW_WEEKDAYS, 4, "M"); VerifysetSymbols(def, UDAT_STANDALONE_WEEKDAYS, 1, "Sonntagweek"); VerifysetSymbols(def, UDAT_STANDALONE_SHORT_WEEKDAYS, 7, "Sams"); VerifysetSymbols(def, UDAT_STANDALONE_NARROW_WEEKDAYS, 4, "V"); VerifysetSymbols(fr, UDAT_MONTHS, 11, "december"); VerifysetSymbols(fr, UDAT_SHORT_MONTHS, 0, "Jan"); VerifysetSymbols(fr, UDAT_NARROW_MONTHS, 1, "R"); VerifysetSymbols(fr, UDAT_STANDALONE_MONTHS, 11, "dezember"); VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_MONTHS, 7, "Aug"); VerifysetSymbols(fr, UDAT_STANDALONE_NARROW_MONTHS, 2, "M"); VerifysetSymbols(fr, UDAT_QUARTERS, 0, "1. Quart"); VerifysetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "QQ2"); VerifysetSymbols(fr, UDAT_STANDALONE_QUARTERS, 2, "3rd Quar."); VerifysetSymbols(fr, UDAT_STANDALONE_SHORT_QUARTERS, 3, "4QQ"); /*run series of tests to test get and setSymbols regressively*/ log_verbose("\nTesting get and set symbols regressively\n"); VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 1); VerifygetsetSymbols(fr, def, UDAT_WEEKDAYS, 7); VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 1); VerifygetsetSymbols(fr, def, UDAT_SHORT_WEEKDAYS, 7); VerifygetsetSymbols(fr, def, UDAT_MONTHS, 0); VerifygetsetSymbols(fr, def, UDAT_SHORT_MONTHS, 0); VerifygetsetSymbols(fr, def, UDAT_ERAS,1); VerifygetsetSymbols(fr, def, UDAT_LOCALIZED_CHARS, 0); VerifygetsetSymbols(fr, def, UDAT_AM_PMS, 1); /*closing*/ udat_close(fr); udat_close(def); if(result != NULL) { free(result); result = NULL; } free(value); } /** * Test DateFormat(Calendar) API */ 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(); } /** * Test parsing two digit year against "YY" vs. "YYYY" patterns */ static void TestCalendarDateParse() { int32_t result; UErrorCode ec = U_ZERO_ERROR; UDateFormat* simpleDateFormat = 0; int parsePos = 0; int twoDigitCenturyStart = 75; int currentTwoDigitYear = 0; int startCentury = 0; UCalendar* tempCal = 0; UCalendar* calendar = 0; U_STRING_DECL(pattern, "yyyy", 4); U_STRING_DECL(pattern2, "yy", 2); U_STRING_DECL(text, "75", 2); U_STRING_INIT(pattern, "yyyy", 4); U_STRING_INIT(pattern2, "yy", 2); U_STRING_INIT(text, "75", 2); simpleDateFormat = udat_open(UDAT_FULL, UDAT_FULL, "en-GB", 0, 0, 0, 0, &ec); if (U_FAILURE(ec)) { log_data_err("udat_open(UDAT_FULL, UDAT_FULL, \"en-GB\", 0, 0, 0, 0, &ec) failed: %s - (Are you missing data?)\n", u_errorName(ec)); return; } udat_applyPattern(simpleDateFormat, 0, pattern, u_strlen(pattern)); udat_setLenient(simpleDateFormat, 0); currentTwoDigitYear = getCurrentYear() % 100; startCentury = getCurrentYear() - currentTwoDigitYear; if (twoDigitCenturyStart > currentTwoDigitYear) { startCentury -= 100; } tempCal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &ec); ucal_setMillis(tempCal, 0, &ec); ucal_setDateTime(tempCal, startCentury + twoDigitCenturyStart, UCAL_JANUARY, 1, 0, 0, 0, &ec); udat_set2DigitYearStart(simpleDateFormat, ucal_getMillis(tempCal, &ec), &ec); calendar = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &ec); ucal_setMillis(calendar, 0, &ec); ucal_setDateTime(calendar, twoDigitCenturyStart, UCAL_JANUARY, 1, 0, 0, 0, &ec); udat_parseCalendar(simpleDateFormat, calendar, text, u_strlen(text), &parsePos, &ec); /* Check result */ result = ucal_get(calendar, UCAL_YEAR, &ec); if (U_FAILURE(ec)) { log_err("FAIL: ucal_get(UCAL_YEAR) failed with %s\n", u_errorName(ec)); goto FAIL; } if (result != 75) { log_err("FAIL: parsed incorrect year: %d\n", result); goto FAIL; } parsePos = 0; udat_applyPattern(simpleDateFormat, 0, pattern2, u_strlen(pattern2)); udat_parseCalendar(simpleDateFormat, calendar, text, u_strlen(text), &parsePos, &ec); /* Check result */ result = ucal_get(calendar, UCAL_YEAR, &ec); if (U_FAILURE(ec)) { log_err("FAIL: ucal_get(UCAL_YEAR) failed with %s\n", u_errorName(ec)); goto FAIL; } if (result != 1975) { log_err("FAIL: parsed incorrect year: %d\n", result); goto FAIL; } FAIL: udat_close(simpleDateFormat); udat_close(tempCal); udat_close(calendar); } /*INTERNAL FUNCTIONS USED*/ static int getCurrentYear() { static int currentYear = 0; if (currentYear == 0) { UErrorCode status = U_ZERO_ERROR; UCalendar *cal = ucal_open(NULL, -1, NULL, UCAL_GREGORIAN, &status); if (!U_FAILURE(status)) { /* Get the current year from the default UCalendar */ currentYear = ucal_get(cal, UCAL_YEAR, &status); ucal_close(cal); } } return currentYear; } /* N.B.: use idx instead of index to avoid 'shadow' warnings in strict mode. */ static void VerifygetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t idx, const char* expected) { UChar *pattern=NULL; UErrorCode status = U_ZERO_ERROR; UChar *result=NULL; int32_t resultlength, resultlengthout; pattern=(UChar*)malloc(sizeof(UChar) * (strlen(expected)+1)); u_uastrcpy(pattern, expected); resultlength=0; resultlengthout=udat_getSymbols(datfor, type, idx , NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthout+1; result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(datfor, type, idx, result, resultlength, &status); } if(U_FAILURE(status)) { log_err("FAIL: Error in udat_getSymbols()... %s\n", myErrorName(status) ); return; } if(u_strcmp(result, pattern)==0) log_verbose("PASS: getSymbols retrieved the right value\n"); else{ log_data_err("FAIL: getSymbols retrieved the wrong value\n Expected %s Got %s\n", austrdup(pattern), austrdup(result) ); } free(result); free(pattern); } static void VerifysetSymbols(UDateFormat* datfor, UDateFormatSymbolType type, int32_t idx, const char* expected) { UChar *result=NULL; UChar *value=NULL; int32_t resultlength, resultlengthout; UErrorCode status = U_ZERO_ERROR; value=(UChar*)malloc(sizeof(UChar) * (strlen(expected) + 1)); u_uastrcpy(value, expected); udat_setSymbols(datfor, type, idx, value, u_strlen(value), &status); if(U_FAILURE(status)) { log_err("FAIL: Error in udat_setSymbols() %s\n", myErrorName(status) ); return; } resultlength=0; resultlengthout=udat_getSymbols(datfor, type, idx, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; resultlength=resultlengthout+1; result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(datfor, type, idx, result, resultlength, &status); } if(U_FAILURE(status)){ log_err("FAIL: error in retrieving the value using getSymbols after setting it previously\n %s\n", myErrorName(status) ); return; } if(u_strcmp(result, value)!=0){ log_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(value), austrdup(result) ); } else log_verbose("PASS: setSymbols successful\n"); free(value); free(result); } static void VerifygetsetSymbols(UDateFormat* from, UDateFormat* to, UDateFormatSymbolType type, int32_t idx) { UChar *result=NULL; UChar *value=NULL; int32_t resultlength, resultlengthout; UErrorCode status = U_ZERO_ERROR; resultlength=0; resultlengthout=udat_getSymbols(from, type, idx , NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; resultlength=resultlengthout+1; result=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(from, type, idx, result, resultlength, &status); } if(U_FAILURE(status)){ log_err("FAIL: error in getSymbols() %s\n", myErrorName(status) ); return; } resultlength=resultlengthout+1; udat_setSymbols(to, type, idx, result, resultlength, &status); if(U_FAILURE(status)) { log_err("FAIL: Error in udat_setSymbols() : %s\n", myErrorName(status) ); return; } resultlength=0; resultlengthout=udat_getSymbols(to, type, idx, NULL, resultlength, &status); if(status==U_BUFFER_OVERFLOW_ERROR){ status=U_ZERO_ERROR; resultlength=resultlengthout+1; value=(UChar*)malloc(sizeof(UChar) * resultlength); udat_getSymbols(to, type, idx, value, resultlength, &status); } if(U_FAILURE(status)){ log_err("FAIL: error in retrieving the value using getSymbols i.e roundtrip\n %s\n", myErrorName(status) ); return; } if(u_strcmp(result, value)!=0){ log_data_err("FAIL:Error in setting and then getting symbols\n Expected %s Got %s\n", austrdup(result), austrdup(value) ); } else log_verbose("PASS: setSymbols successful\n"); free(value); free(result); } static UChar* myNumformat(const UNumberFormat* numfor, double d) { UChar *result2=NULL; int32_t resultlength, resultlengthneeded; UErrorCode status = U_ZERO_ERROR; resultlength=0; resultlengthneeded=unum_formatDouble(numfor, d, NULL, resultlength, NULL, &status); if(status==U_BUFFER_OVERFLOW_ERROR) { status=U_ZERO_ERROR; resultlength=resultlengthneeded+1; /*result2=(UChar*)malloc(sizeof(UChar) * resultlength);*/ /* this leaks */ result2=(UChar*)ctst_malloc(sizeof(UChar) * resultlength); /*this won't*/ unum_formatDouble(numfor, d, result2, resultlength, NULL, &status); } if(U_FAILURE(status)) { log_err("FAIL: Error in formatting using unum_format(.....) %s\n", myErrorName(status) ); return 0; } return result2; } /** * The search depth for TestExtremeDates. The total number of * dates that will be tested is (2^EXTREME_DATES_DEPTH) - 1. */ #define EXTREME_DATES_DEPTH 8 /** * Support for TestExtremeDates (below). * * Test a single date to see whether udat_format handles it properly. */ 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; } /** * Support for TestExtremeDates (below). * * Recursively test between 'small' and 'large', up to the depth * limit specified by EXTREME_DATES_DEPTH. */ static UBool _aux2ExtremeDates(UDateFormat* fmt, UDate small, UDate large, UChar* buf, int32_t buflen, char* cbuf, int32_t count, UErrorCode* ec) { /* Logarithmic midpoint; see below */ UDate mid = (UDate) exp((log(small) + log(large)) / 2); if (count == EXTREME_DATES_DEPTH) { return TRUE; } return _aux1ExtremeDates(fmt, mid, buf, buflen, cbuf, ec) && _aux2ExtremeDates(fmt, small, mid, buf, buflen, cbuf, count+1, ec) && _aux2ExtremeDates(fmt, mid, large, buf, buflen, cbuf, count+1, ec); } /** * http://www.jtcsv.com/cgibin/icu-bugs?findid=3659 * * For certain large dates, udat_format crashes on MacOS. This test * attempts to reproduce this problem by doing a recursive logarithmic* * binary search of a predefined interval (from 'small' to 'large'). * * The limit of the search is given by EXTREME_DATES_DEPTH, above. * * *The search has to be logarithmic, not linear. A linear search of the * range 0..10^30, for example, will find 0.5*10^30, then 0.25*10^30 and * 0.75*10^30, etc. A logarithmic search will find 10^15, then 10^7.5 * and 10^22.5, etc. */ static void TestExtremeDates() { UDateFormat *fmt; UErrorCode ec; UChar buf[256]; char cbuf[256]; const double small = 1000; /* 1 sec */ const double large = 1e+30; /* well beyond usable UDate range */ /* There is no need to test larger values from 1e+30 to 1e+300; the failures occur around 1e+27, and never above 1e+30. */ ec = U_ZERO_ERROR; fmt = udat_open(UDAT_LONG, UDAT_LONG, "en_US", 0, 0, 0, 0, &ec); if (U_FAILURE(ec)) { log_data_err("FAIL: udat_open (%s) (Are you missing data?)\n", u_errorName(ec)); return; } _aux2ExtremeDates(fmt, small, large, buf, LEN(buf), cbuf, 0, &ec); udat_close(fmt); } static void TestAllLocales(void) { int32_t idx, dateIdx, timeIdx, localeCount; static const UDateFormatStyle style[] = { UDAT_FULL, UDAT_LONG, UDAT_MEDIUM, UDAT_SHORT }; localeCount = uloc_countAvailable(); for (idx = 0; idx < localeCount; idx++) { for (dateIdx = 0; dateIdx < (int32_t)(sizeof(style)/sizeof(style[0])); dateIdx++) { for (timeIdx = 0; timeIdx < (int32_t)(sizeof(style)/sizeof(style[0])); timeIdx++) { UErrorCode status = U_ZERO_ERROR; udat_close(udat_open(style[dateIdx], style[timeIdx], uloc_getAvailable(idx), NULL, 0, NULL, 0, &status)); if (U_FAILURE(status)) { log_err("FAIL: udat_open(%s) failed with (%s) dateIdx=%d, timeIdx=%d\n", uloc_getAvailable(idx), u_errorName(status), dateIdx, timeIdx); } } } } } static void TestRelativeCrash(void) { static const UChar tzName[] = { 0x0055, 0x0053, 0x002F, 0x0050, 0x0061, 0x0063, 0x0069, 0x0066, 0x0069, 0x0063, 0 }; static const UDate aDate = -631152000000.0; UErrorCode status = U_ZERO_ERROR; UErrorCode expectStatus = U_ILLEGAL_ARGUMENT_ERROR; UDateFormat icudf; icudf = udat_open(UDAT_NONE, UDAT_SHORT_RELATIVE, "en", tzName, -1, NULL, 0, &status); if ( U_SUCCESS(status) ) { const char *what = "???"; { UErrorCode subStatus = U_ZERO_ERROR; what = "udat_set2DigitYearStart"; log_verbose("Trying %s on a relative date..\n", what); udat_set2DigitYearStart(icudf, aDate, &subStatus); if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { /* clone works polymorphically. try it anyways */ UErrorCode subStatus = U_ZERO_ERROR; UDateFormat *oth; what = "clone"; log_verbose("Trying %s on a relative date..\n", what); oth = udat_clone(icudf, &subStatus); if(subStatus == U_ZERO_ERROR) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); udat_close(oth); /* ? */ } else { log_err("FAIL: didn't crash on %s, but got %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { UErrorCode subStatus = U_ZERO_ERROR; what = "udat_get2DigitYearStart"; log_verbose("Trying %s on a relative date..\n", what); udat_get2DigitYearStart(icudf, &subStatus); if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { /* Now udat_toPattern works for relative date formatters, unless localized is TRUE */ UErrorCode subStatus = U_ZERO_ERROR; what = "udat_toPattern"; log_verbose("Trying %s on a relative date..\n", what); udat_toPattern(icudf, TRUE,NULL,0, &subStatus); if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { UErrorCode subStatus = U_ZERO_ERROR; what = "udat_applyPattern"; log_verbose("Trying %s on a relative date..\n", what); udat_applyPattern(icudf, FALSE,tzName,-1); subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* what it should be, if this took an errorcode. */ if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { UChar erabuf[32]; UErrorCode subStatus = U_ZERO_ERROR; what = "udat_getSymbols"; log_verbose("Trying %s on a relative date..\n", what); udat_getSymbols(icudf, UDAT_ERAS,0,erabuf,sizeof(erabuf)/sizeof(erabuf[0]), &subStatus); if(subStatus == U_ZERO_ERROR) { log_verbose("Success: %s returned %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got %s instead of U_ZERO_ERROR.\n", what, u_errorName(subStatus)); } } { UErrorCode subStatus = U_ZERO_ERROR; UChar symbolValue = 0x0041; what = "udat_setSymbols"; log_verbose("Trying %s on a relative date..\n", what); udat_setSymbols(icudf, UDAT_ERAS,0,&symbolValue,1, &subStatus); /* bogus values */ if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } { UErrorCode subStatus = U_ZERO_ERROR; what = "udat_countSymbols"; log_verbose("Trying %s on a relative date..\n", what); udat_countSymbols(icudf, UDAT_ERAS); subStatus = U_ILLEGAL_ARGUMENT_ERROR; /* should have an errorcode. */ if(subStatus == expectStatus) { log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus)); } else { log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus)); } } udat_close(icudf); } else { log_data_err("FAIL: err calling udat_open() ->%s (Are you missing data?)\n", u_errorName(status)); } } static const UChar skeleton_yMMMM[] = { 0x79,0x4D,0x4D,0x4D,0x4D,0 }; /* "yMMMM"; fr maps to "MMMM y", cs maps to "LLLL y" */ static const UChar july2008_frDefault[] = { 0x6A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "juillet 2008" */ static const UChar july2008_frTitle[] = { 0x4A,0x75,0x69,0x6C,0x6C,0x65,0x74,0x20,0x32,0x30,0x30,0x38,0 }; /* "Juillet 2008" sentence-begin, standalone */ static const UChar july2008_csDefault[] = { 0x10D,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "c(hacek)ervenec 2008" */ static const UChar july2008_csTitle[] = { 0x10C,0x65,0x72,0x76,0x65,0x6E,0x65,0x63,0x20,0x32,0x30,0x30,0x38,0 }; /* "C(hacek)ervenec 2008" sentence-begin, uiListOrMenu */ typedef struct { const char * locale; const UChar * skeleton; UDisplayContext capitalizationContext; const UChar * expectedFormat; } TestContextItem; static const TestContextItem textContextItems[] = { { "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_NONE, july2008_frDefault }, #if !UCONFIG_NO_BREAK_ITERATION { "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, july2008_frDefault }, { "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, july2008_frTitle }, { "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, july2008_frDefault }, { "fr", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, july2008_frTitle }, #endif { "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_NONE, july2008_csDefault }, #if !UCONFIG_NO_BREAK_ITERATION { "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, july2008_csDefault }, { "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, july2008_csTitle }, { "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU, july2008_csTitle }, { "cs", skeleton_yMMMM, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, july2008_csDefault }, #endif { NULL, NULL, (UDisplayContext)0, NULL } }; static const UDate july022008 = 1215000001979.0; enum { kUbufMax = 64, kBbufMax = 3*kUbufMax }; static void TestContext(void) { const TestContextItem* textContextItemPtr = textContextItems; for (; textContextItemPtr->locale != NULL; ++textContextItemPtr) { UErrorCode status = U_ZERO_ERROR; UDateFormat* udfmt = udat_open(UDAT_NONE, UDAT_MEDIUM, textContextItemPtr->locale, NULL, 0, NULL, 0, &status); if ( U_FAILURE(status) ) { log_data_err("FAIL: udat_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) ); } else { UDateTimePatternGenerator* udtpg = udatpg_open(textContextItemPtr->locale, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udatpg_open for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) ); } else { UChar ubuf[kUbufMax]; int32_t len = udatpg_getBestPattern(udtpg, textContextItemPtr->skeleton, -1, ubuf, kUbufMax, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udatpg_getBestPattern for locale %s, status %s\n", textContextItemPtr->locale, u_errorName(status) ); } else { udat_applyPattern(udfmt, FALSE, ubuf, len); udat_setContext(udfmt, textContextItemPtr->capitalizationContext, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udat_setContext for locale %s, capitalizationContext %d, status %s\n", textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) ); } else { UDisplayContext getContext; len = udat_format(udfmt, july022008, ubuf, kUbufMax, NULL, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udat_format for locale %s, capitalizationContext %d, status %s\n", textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) ); status = U_ZERO_ERROR; } else if (u_strncmp(ubuf, textContextItemPtr->expectedFormat, kUbufMax) != 0) { char bbuf1[kBbufMax]; char bbuf2[kBbufMax]; log_err("FAIL: udat_format for locale %s, capitalizationContext %d, expected %s, got %s\n", textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_austrncpy(bbuf1,textContextItemPtr->expectedFormat,kUbufMax), u_austrncpy(bbuf2,ubuf,kUbufMax) ); } getContext = udat_getContext(udfmt, UDISPCTX_TYPE_CAPITALIZATION, &status); if ( U_FAILURE(status) ) { log_err("FAIL: udat_getContext for locale %s, capitalizationContext %d, status %s\n", textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, u_errorName(status) ); } else if (getContext != textContextItemPtr->capitalizationContext) { log_err("FAIL: udat_getContext for locale %s, capitalizationContext %d, got context %d\n", textContextItemPtr->locale, (int)textContextItemPtr->capitalizationContext, (int)getContext ); } } } udatpg_close(udtpg); } udat_close(udfmt); } } } #endif /* #if !UCONFIG_NO_FORMATTING */