/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2007, 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/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); #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(TestSymbols); TESTCASE(TestDateFormatCalendar); TESTCASE(TestExtremeDates); TESTCASE(TestAllLocales); } /* 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[30]; 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_err("FAIL: error in creating the dateformat using full time style with french locale\n %s\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 \"%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 (!assertSuccess("udat_open", &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); } } } } } #endif /* #if !UCONFIG_NO_FORMATTING */