| OLD | NEW |
| 1 /******************************************************************** | 1 /******************************************************************** |
| 2 * COPYRIGHT: | 2 * COPYRIGHT: |
| 3 * Copyright (c) 1997-2014, International Business Machines Corporation and | 3 * Copyright (c) 1997-2015, International Business Machines Corporation and |
| 4 * others. All Rights Reserved. | 4 * others. All Rights Reserved. |
| 5 ********************************************************************/ | 5 ********************************************************************/ |
| 6 /*******************************************************************************
* | 6 /*******************************************************************************
* |
| 7 * | 7 * |
| 8 * File CDATTST.C | 8 * File CDATTST.C |
| 9 * | 9 * |
| 10 * Modification History: | 10 * Modification History: |
| 11 * Name Description | 11 * Name Description |
| 12 * Madhu Katragadda Creation | 12 * Madhu Katragadda Creation |
| 13 ********************************************************************************
* | 13 ********************************************************************************
* |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 /* C API TEST FOR DATE FORMAT */ | 16 /* C API TEST FOR DATE FORMAT */ |
| 17 | 17 |
| 18 #include "unicode/utypes.h" | 18 #include "unicode/utypes.h" |
| 19 | 19 |
| 20 #if !UCONFIG_NO_FORMATTING | 20 #if !UCONFIG_NO_FORMATTING |
| 21 | 21 |
| 22 #include "unicode/uloc.h" | 22 #include "unicode/uloc.h" |
| 23 #include "unicode/udat.h" | 23 #include "unicode/udat.h" |
| 24 #include "unicode/udatpg.h" | 24 #include "unicode/udatpg.h" |
| 25 #include "unicode/ucal.h" | 25 #include "unicode/ucal.h" |
| 26 #include "unicode/unum.h" | 26 #include "unicode/unum.h" |
| 27 #include "unicode/ustring.h" | 27 #include "unicode/ustring.h" |
| 28 #include "unicode/ufieldpositer.h" |
| 28 #include "cintltst.h" | 29 #include "cintltst.h" |
| 29 #include "cdattst.h" | 30 #include "cdattst.h" |
| 30 #include "cformtst.h" | 31 #include "cformtst.h" |
| 31 #include "cmemory.h" | 32 #include "cmemory.h" |
| 32 | 33 |
| 33 #include <math.h> | 34 #include <math.h> |
| 34 | 35 |
| 35 static void TestExtremeDates(void); | 36 static void TestExtremeDates(void); |
| 36 static void TestAllLocales(void); | 37 static void TestAllLocales(void); |
| 37 static void TestRelativeCrash(void); | 38 static void TestRelativeCrash(void); |
| 38 static void TestContext(void); | 39 static void TestContext(void); |
| 39 static void TestCalendarDateParse(void); | 40 static void TestCalendarDateParse(void); |
| 41 static void TestParseErrorReturnValue(void); |
| 42 static void TestFormatForFields(void); |
| 40 | 43 |
| 41 #define LEN(a) (sizeof(a)/sizeof(a[0])) | 44 #define LEN(a) (sizeof(a)/sizeof(a[0])) |
| 42 | 45 |
| 43 void addDateForTest(TestNode** root); | 46 void addDateForTest(TestNode** root); |
| 44 | 47 |
| 45 #define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x) | 48 #define TESTCASE(x) addTest(root, &x, "tsformat/cdattst/" #x) |
| 46 | 49 |
| 47 void addDateForTest(TestNode** root) | 50 void addDateForTest(TestNode** root) |
| 48 { | 51 { |
| 49 TESTCASE(TestDateFormat); | 52 TESTCASE(TestDateFormat); |
| 50 TESTCASE(TestRelativeDateFormat); | 53 TESTCASE(TestRelativeDateFormat); |
| 51 TESTCASE(TestSymbols); | 54 TESTCASE(TestSymbols); |
| 52 TESTCASE(TestDateFormatCalendar); | 55 TESTCASE(TestDateFormatCalendar); |
| 53 TESTCASE(TestExtremeDates); | 56 TESTCASE(TestExtremeDates); |
| 54 TESTCASE(TestAllLocales); | 57 TESTCASE(TestAllLocales); |
| 55 TESTCASE(TestRelativeCrash); | 58 TESTCASE(TestRelativeCrash); |
| 56 TESTCASE(TestContext); | 59 TESTCASE(TestContext); |
| 57 TESTCASE(TestCalendarDateParse); | 60 TESTCASE(TestCalendarDateParse); |
| 58 TESTCASE(TestOverrideNumberFormat); | 61 TESTCASE(TestOverrideNumberFormat); |
| 62 TESTCASE(TestParseErrorReturnValue); |
| 63 TESTCASE(TestFormatForFields); |
| 59 } | 64 } |
| 60 /* Testing the DateFormat API */ | 65 /* Testing the DateFormat API */ |
| 61 static void TestDateFormat() | 66 static void TestDateFormat() |
| 62 { | 67 { |
| 63 UDateFormat *def, *fr, *it, *de, *def1, *fr_pat; | 68 UDateFormat *def, *fr, *it, *de, *def1, *fr_pat; |
| 64 UDateFormat *any; | 69 UDateFormat *any; |
| 65 UDateFormat *copy; | 70 UDateFormat *copy; |
| 66 UErrorCode status = U_ZERO_ERROR; | 71 UErrorCode status = U_ZERO_ERROR; |
| 67 UChar* result = NULL; | 72 UChar* result = NULL; |
| 68 const UCalendar *cal; | 73 const UCalendar *cal; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 log_verbose("PASS: Date Format for US locale successful using udat_forma
t()\n"); | 185 log_verbose("PASS: Date Format for US locale successful using udat_forma
t()\n"); |
| 181 else { | 186 else { |
| 182 char xbuf[2048]; | 187 char xbuf[2048]; |
| 183 char gbuf[2048]; | 188 char gbuf[2048]; |
| 184 u_austrcpy(xbuf, temp); | 189 u_austrcpy(xbuf, temp); |
| 185 u_austrcpy(gbuf, result); | 190 u_austrcpy(gbuf, result); |
| 186 log_err("FAIL: Date Format for US locale failed using udat_format() - ex
pected %s got %s\n", xbuf, gbuf); | 191 log_err("FAIL: Date Format for US locale failed using udat_format() - ex
pected %s got %s\n", xbuf, gbuf); |
| 187 } | 192 } |
| 188 /*format using fr */ | 193 /*format using fr */ |
| 189 | 194 |
| 190 u_unescape("10 juil. 1996 16:05:28 heure d\\u2019\\u00E9t\\u00E9 du Pacifiqu
e", temp, 50); | 195 u_unescape("10 juil. 1996 \\u00E0 16:05:28 heure d\\u2019\\u00E9t\\u00E9 du
Pacifique", temp, 50); |
| 191 if(result != NULL) { | 196 if(result != NULL) { |
| 192 free(result); | 197 free(result); |
| 193 result = NULL; | 198 result = NULL; |
| 194 } | 199 } |
| 195 result=myDateFormat(fr, d); | 200 result=myDateFormat(fr, d); |
| 196 if(u_strcmp(result, temp)==0) | 201 if(u_strcmp(result, temp)==0) |
| 197 log_verbose("PASS: Date Format for french locale successful using udat_f
ormat()\n"); | 202 log_verbose("PASS: Date Format for french locale successful using udat_f
ormat()\n"); |
| 198 else | 203 else |
| 199 log_data_err("FAIL: Date Format for french locale failed using udat_form
at().\n" ); | 204 log_data_err("FAIL: Date Format for french locale failed using udat_form
at().\n" ); |
| 200 | 205 |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv."); | 671 VerifygetSymbols(fr, UDAT_SHORT_MONTHS, 0, "janv."); |
| 667 VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec"); | 672 VerifygetSymbols(def, UDAT_SHORT_MONTHS, 11, "Dec"); |
| 668 VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre"); | 673 VerifygetSymbols(fr, UDAT_QUARTERS, 0, "1er trimestre"); |
| 669 VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter"); | 674 VerifygetSymbols(def, UDAT_QUARTERS, 3, "4th quarter"); |
| 670 VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2"); | 675 VerifygetSymbols(fr, UDAT_SHORT_QUARTERS, 1, "T2"); |
| 671 VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3"); | 676 VerifygetSymbols(def, UDAT_SHORT_QUARTERS, 2, "Q3"); |
| 672 VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_ABBREVIATED, 0, "\\u7532\\u5B50
"); | 677 VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_ABBREVIATED, 0, "\\u7532\\u5B50
"); |
| 673 VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_NARROW, 59, "\\u7678\\u4EA5"); | 678 VerifygetSymbols(zhChiCal, UDAT_CYCLIC_YEARS_NARROW, 59, "\\u7678\\u4EA5"); |
| 674 VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_ABBREVIATED, 0, "\\u9F20"); | 679 VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_ABBREVIATED, 0, "\\u9F20"); |
| 675 VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_WIDE, 11, "\\u732A"); | 680 VerifygetSymbols(zhChiCal, UDAT_ZODIAC_NAMES_WIDE, 11, "\\u732A"); |
| 681 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR |
| 682 VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQq
VUOXxr:"); |
| 683 #else |
| 676 VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQq
VUOXxr"); | 684 VerifygetSymbols(def,UDAT_LOCALIZED_CHARS, 0, "GyMdkHmsSEDFwWahKzYeugAZvcLQq
VUOXxr"); |
| 685 #endif |
| 677 | 686 |
| 678 | 687 |
| 679 if(result != NULL) { | 688 if(result != NULL) { |
| 680 free(result); | 689 free(result); |
| 681 result = NULL; | 690 result = NULL; |
| 682 } | 691 } |
| 683 free(pattern); | 692 free(pattern); |
| 684 | 693 |
| 685 log_verbose("\nTesting setSymbols\n"); | 694 log_verbose("\nTesting setSymbols\n"); |
| 686 /*applying the pattern so that setSymbolss works */ | 695 /*applying the pattern so that setSymbolss works */ |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 * Test DateFormat(Calendar) API | 830 * Test DateFormat(Calendar) API |
| 822 */ | 831 */ |
| 823 static void TestDateFormatCalendar() { | 832 static void TestDateFormatCalendar() { |
| 824 UDateFormat *date=0, *time=0, *full=0; | 833 UDateFormat *date=0, *time=0, *full=0; |
| 825 UCalendar *cal=0; | 834 UCalendar *cal=0; |
| 826 UChar buf[256]; | 835 UChar buf[256]; |
| 827 char cbuf[256]; | 836 char cbuf[256]; |
| 828 int32_t pos; | 837 int32_t pos; |
| 829 UDate when; | 838 UDate when; |
| 830 UErrorCode ec = U_ZERO_ERROR; | 839 UErrorCode ec = U_ZERO_ERROR; |
| 840 UChar buf1[256]; |
| 841 int32_t len1; |
| 842 const char *expected; |
| 843 UChar uExpected[32]; |
| 831 | 844 |
| 832 ctest_setTimeZone(NULL, &ec); | 845 ctest_setTimeZone(NULL, &ec); |
| 833 | 846 |
| 834 /* Create a formatter for date fields. */ | 847 /* Create a formatter for date fields. */ |
| 835 date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec); | 848 date = udat_open(UDAT_NONE, UDAT_SHORT, "en_US", NULL, 0, NULL, 0, &ec); |
| 836 if (U_FAILURE(ec)) { | 849 if (U_FAILURE(ec)) { |
| 837 log_data_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s (Are yo
u missing data?)\n", | 850 log_data_err("FAIL: udat_open(NONE, SHORT, en_US) failed with %s (Are yo
u missing data?)\n", |
| 838 u_errorName(ec)); | 851 u_errorName(ec)); |
| 839 goto FAIL; | 852 goto FAIL; |
| 840 } | 853 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 867 ucal_clear(cal); | 880 ucal_clear(cal); |
| 868 u_uastrcpy(buf, "4/5/2001"); | 881 u_uastrcpy(buf, "4/5/2001"); |
| 869 pos = 0; | 882 pos = 0; |
| 870 udat_parseCalendar(date, cal, buf, -1, &pos, &ec); | 883 udat_parseCalendar(date, cal, buf, -1, &pos, &ec); |
| 871 if (U_FAILURE(ec)) { | 884 if (U_FAILURE(ec)) { |
| 872 log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n", | 885 log_err("FAIL: udat_parseCalendar(4/5/2001) failed at %d with %s\n", |
| 873 pos, u_errorName(ec)); | 886 pos, u_errorName(ec)); |
| 874 goto FAIL; | 887 goto FAIL; |
| 875 } | 888 } |
| 876 | 889 |
| 890 /* Check if formatCalendar matches the original date */ |
| 891 len1 = udat_formatCalendar(date, cal, buf1, UPRV_LENGTHOF(buf1), NULL, &ec); |
| 892 if (U_FAILURE(ec)) { |
| 893 log_err("FAIL: udat_formatCalendar(4/5/2001) failed with %s\n", |
| 894 u_errorName(ec)); |
| 895 goto FAIL; |
| 896 } |
| 897 expected = "4/5/01"; |
| 898 u_uastrcpy(uExpected, expected); |
| 899 if (u_strlen(uExpected) != len1 || u_strncmp(uExpected, buf1, len1) != 0) { |
| 900 log_err("FAIL: udat_formatCalendar(4/5/2001), expected: %s", expected); |
| 901 } |
| 902 |
| 877 /* Parse the time */ | 903 /* Parse the time */ |
| 878 u_uastrcpy(buf, "5:45 PM"); | 904 u_uastrcpy(buf, "5:45 PM"); |
| 879 pos = 0; | 905 pos = 0; |
| 880 udat_parseCalendar(time, cal, buf, -1, &pos, &ec); | 906 udat_parseCalendar(time, cal, buf, -1, &pos, &ec); |
| 881 if (U_FAILURE(ec)) { | 907 if (U_FAILURE(ec)) { |
| 882 log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n", | 908 log_err("FAIL: udat_parseCalendar(17:45) failed at %d with %s\n", |
| 883 pos, u_errorName(ec)); | 909 pos, u_errorName(ec)); |
| 884 goto FAIL; | 910 goto FAIL; |
| 885 } | 911 } |
| 886 | 912 |
| 913 /* Check if formatCalendar matches the original time */ |
| 914 len1 = udat_formatCalendar(time, cal, buf1, UPRV_LENGTHOF(buf1), NULL, &ec); |
| 915 if (U_FAILURE(ec)) { |
| 916 log_err("FAIL: udat_formatCalendar(17:45) failed with %s\n", |
| 917 u_errorName(ec)); |
| 918 goto FAIL; |
| 919 } |
| 920 expected = "5:45 PM"; |
| 921 u_uastrcpy(uExpected, expected); |
| 922 if (u_strlen(uExpected) != len1 || u_strncmp(uExpected, buf1, len1) != 0) { |
| 923 log_err("FAIL: udat_formatCalendar(17:45), expected: %s", expected); |
| 924 } |
| 925 |
| 887 /* Check result */ | 926 /* Check result */ |
| 888 when = ucal_getMillis(cal, &ec); | 927 when = ucal_getMillis(cal, &ec); |
| 889 if (U_FAILURE(ec)) { | 928 if (U_FAILURE(ec)) { |
| 890 log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec)); | 929 log_err("FAIL: ucal_getMillis() failed with %s\n", u_errorName(ec)); |
| 891 goto FAIL; | 930 goto FAIL; |
| 892 } | 931 } |
| 893 udat_format(full, when, buf, sizeof(buf), NULL, &ec); | 932 udat_format(full, when, buf, sizeof(buf), NULL, &ec); |
| 894 if (U_FAILURE(ec)) { | 933 if (U_FAILURE(ec)) { |
| 895 log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec)); | 934 log_err("FAIL: udat_format() failed with %s\n", u_errorName(ec)); |
| 896 goto FAIL; | 935 goto FAIL; |
| (...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1569 {"d", "07 \\u521D\\u4E8C"}, | 1608 {"d", "07 \\u521D\\u4E8C"}, |
| 1570 {"do", "07 \\u521D\\u4E8C"}, | 1609 {"do", "07 \\u521D\\u4E8C"}, |
| 1571 {"Md", "\\u521D\\u4E03 \\u521D\\u4E8C"}, | 1610 {"Md", "\\u521D\\u4E03 \\u521D\\u4E8C"}, |
| 1572 {"MdMMd", "\\u521D\\u4E03 \\u521D\\u4E8C"}, | 1611 {"MdMMd", "\\u521D\\u4E03 \\u521D\\u4E8C"}, |
| 1573 {"mixed", "\\u521D\\u4E03 \\u521D\\u4E8C"} | 1612 {"mixed", "\\u521D\\u4E03 \\u521D\\u4E8C"} |
| 1574 }; | 1613 }; |
| 1575 | 1614 |
| 1576 static void TestOverrideNumberFormat(void) { | 1615 static void TestOverrideNumberFormat(void) { |
| 1577 UErrorCode status = U_ZERO_ERROR; | 1616 UErrorCode status = U_ZERO_ERROR; |
| 1578 UChar pattern[50]; | 1617 UChar pattern[50]; |
| 1579 UChar* expected; | 1618 UChar expected[50]; |
| 1580 UChar* fields; | 1619 UChar fields[50]; |
| 1581 char bbuf1[kBbufMax]; | 1620 char bbuf1[kBbufMax]; |
| 1582 char bbuf2[kBbufMax]; | 1621 char bbuf2[kBbufMax]; |
| 1583 const char* localeString = "zh@numbers=hanidays"; | 1622 const char* localeString = "zh@numbers=hanidays"; |
| 1584 UDateFormat* fmt; | 1623 UDateFormat* fmt; |
| 1585 const UNumberFormat* getter_result; | 1624 const UNumberFormat* getter_result; |
| 1586 int32_t i; | 1625 int32_t i; |
| 1587 unsigned j; | |
| 1588 | 1626 |
| 1589 expected=(UChar*)malloc(sizeof(UChar) * 10); | |
| 1590 fields=(UChar*)malloc(sizeof(UChar) * 10); | |
| 1591 u_uastrcpy(fields, "d"); | 1627 u_uastrcpy(fields, "d"); |
| 1592 u_uastrcpy(pattern,"MM d"); | 1628 u_uastrcpy(pattern,"MM d"); |
| 1593 | 1629 |
| 1594 fmt=udat_open(UDAT_PATTERN, UDAT_PATTERN,"en_US",NULL,0,pattern, u_strlen(pa
ttern), &status); | 1630 fmt=udat_open(UDAT_PATTERN, UDAT_PATTERN, "en_US", zoneGMT, -1, pattern, u_s
trlen(pattern), &status); |
| 1595 if (!assertSuccess("udat_open()", &status)) { | 1631 if (!assertSuccess("udat_open()", &status)) { |
| 1596 return; | 1632 return; |
| 1597 } | 1633 } |
| 1598 | 1634 |
| 1599 | 1635 // loop 5 times to check getter/setter |
| 1600 // loop 50 times to check getter/setter | |
| 1601 for (i = 0; i < 5; i++){ | 1636 for (i = 0; i < 5; i++){ |
| 1602 UNumberFormat* overrideFmt; | 1637 UNumberFormat* overrideFmt; |
| 1603 overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &stat
us); | 1638 overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &stat
us); |
| 1604 assertSuccess("unum_open()", &status); | 1639 assertSuccess("unum_open()", &status); |
| 1605 | |
| 1606 udat_adoptNumberFormatForFields(fmt, fields, overrideFmt, &status); | 1640 udat_adoptNumberFormatForFields(fmt, fields, overrideFmt, &status); |
| 1607 overrideFmt = NULL; // no longer valid | 1641 overrideFmt = NULL; // no longer valid |
| 1608 assertSuccess("udat_setNumberFormatForField()", &status); | 1642 assertSuccess("udat_setNumberFormatForField()", &status); |
| 1609 | 1643 |
| 1610 getter_result = udat_getNumberFormatForField(fmt, 'd'); | 1644 getter_result = udat_getNumberFormatForField(fmt, 'd'); |
| 1611 if(getter_result == NULL) { | 1645 if(getter_result == NULL) { |
| 1612 log_err("FAIL: udat_getNumberFormatForField did not return a valid p
ointer\n"); | 1646 log_err("FAIL: udat_getNumberFormatForField did not return a valid p
ointer\n"); |
| 1613 } | 1647 } |
| 1614 } | 1648 } |
| 1615 | |
| 1616 { | 1649 { |
| 1617 UNumberFormat* overrideFmt; | 1650 UNumberFormat* overrideFmt; |
| 1618 overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &status
); | 1651 overrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &status
); |
| 1619 assertSuccess("unum_open()", &status); | 1652 assertSuccess("unum_open()", &status); |
| 1620 udat_setNumberFormat(fmt, overrideFmt); // test the same override NF will
not crash | 1653 udat_setNumberFormat(fmt, overrideFmt); // test the same override NF will
not crash |
| 1621 unum_close(overrideFmt); | 1654 unum_close(overrideFmt); |
| 1622 } | 1655 } |
| 1623 udat_close(fmt); | 1656 udat_close(fmt); |
| 1624 | 1657 |
| 1625 for (j=0; i<sizeof(overrideNumberFormat)/sizeof(overrideNumberFormat[0]); i+
+){ | 1658 for (i=0; i<UPRV_LENGTHOF(overrideNumberFormat); i++){ |
| 1626 UChar ubuf[kUbufMax]; | 1659 UChar ubuf[kUbufMax]; |
| 1627 UDateFormat* fmt2; | 1660 UDateFormat* fmt2; |
| 1628 UNumberFormat* overrideFmt2; | 1661 UNumberFormat* overrideFmt2; |
| 1629 | 1662 |
| 1630 fmt2 =udat_open(UDAT_PATTERN, UDAT_PATTERN,"en_US",NULL,0,pattern, u_str
len(pattern), &status); | 1663 fmt2 =udat_open(UDAT_PATTERN, UDAT_PATTERN,"en_US", zoneGMT, -1, pattern
, u_strlen(pattern), &status); |
| 1631 assertSuccess("udat_open() with en_US", &status); | 1664 assertSuccess("udat_open() with en_US", &status); |
| 1632 | 1665 |
| 1633 overrideFmt2 = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &sta
tus); | 1666 overrideFmt2 = unum_open(UNUM_DEFAULT, NULL, 0, localeString, NULL, &sta
tus); |
| 1634 assertSuccess("unum_open() in loop", &status); | 1667 assertSuccess("unum_open() in loop", &status); |
| 1635 | 1668 |
| 1636 u_uastrcpy(fields, overrideNumberFormat[i][0]); | 1669 u_uastrcpy(fields, overrideNumberFormat[i][0]); |
| 1637 u_unescape(overrideNumberFormat[i][1], expected, 50); | 1670 u_unescape(overrideNumberFormat[i][1], expected, UPRV_LENGTHOF(expected)
); |
| 1638 | 1671 |
| 1639 if ( strcmp(overrideNumberFormat[i][0], "") == 0 ) { // use the one w/o
field | 1672 if ( strcmp(overrideNumberFormat[i][0], "") == 0 ) { // use the one w/o
field |
| 1640 udat_setNumberFormat(fmt2, overrideFmt2); | 1673 udat_adoptNumberFormat(fmt2, overrideFmt2); |
| 1641 } else if ( strcmp(overrideNumberFormat[i][0], "mixed") == 0 ) { // set
1 field at first but then full override, both(M & d) should be override | 1674 } else if ( strcmp(overrideNumberFormat[i][0], "mixed") == 0 ) { // set
1 field at first but then full override, both(M & d) should be override |
| 1642 const char* singleLocale = "en@numbers=hebr"; | 1675 const char* singleLocale = "en@numbers=hebr"; |
| 1643 UNumberFormat* singleOverrideFmt; | 1676 UNumberFormat* singleOverrideFmt; |
| 1644 u_uastrcpy(fields, "d"); | 1677 u_uastrcpy(fields, "d"); |
| 1645 | 1678 |
| 1646 singleOverrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, singleLocale, N
ULL, &status); | 1679 singleOverrideFmt = unum_open(UNUM_DEFAULT, NULL, 0, singleLocale, N
ULL, &status); |
| 1647 assertSuccess("unum_open() in mixed", &status); | 1680 assertSuccess("unum_open() in mixed", &status); |
| 1648 | 1681 |
| 1649 udat_adoptNumberFormatForFields(fmt2, fields, singleOverrideFmt, &st
atus); | 1682 udat_adoptNumberFormatForFields(fmt2, fields, singleOverrideFmt, &st
atus); |
| 1650 assertSuccess("udat_setNumberFormatForField() in mixed", &status); | 1683 assertSuccess("udat_setNumberFormatForField() in mixed", &status); |
| 1651 | 1684 |
| 1652 udat_setNumberFormat(fmt2, overrideFmt2); | 1685 udat_adoptNumberFormat(fmt2, overrideFmt2); |
| 1653 } else if ( strcmp(overrideNumberFormat[i][0], "do") == 0 ) { // o is an
invalid field | 1686 } else if ( strcmp(overrideNumberFormat[i][0], "do") == 0 ) { // o is an
invalid field |
| 1654 udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status)
; | 1687 udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status)
; |
| 1655 if(status == U_INVALID_FORMAT_ERROR) { | 1688 if(status == U_INVALID_FORMAT_ERROR) { |
| 1656 udat_close(fmt2); | 1689 udat_close(fmt2); |
| 1657 status = U_ZERO_ERROR; | 1690 status = U_ZERO_ERROR; |
| 1658 continue; | 1691 continue; |
| 1659 } | 1692 } |
| 1660 } else { | 1693 } else { |
| 1661 udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status)
; | 1694 udat_adoptNumberFormatForFields(fmt2, fields, overrideFmt2, &status)
; |
| 1662 assertSuccess("udat_setNumberFormatForField() in loop", &status); | 1695 assertSuccess("udat_setNumberFormatForField() in loop", &status); |
| 1663 } | 1696 } |
| 1664 | 1697 |
| 1665 udat_format(fmt2, july022008, ubuf, kUbufMax, NULL, &status); | 1698 udat_format(fmt2, july022008, ubuf, kUbufMax, NULL, &status); |
| 1666 assertSuccess("udat_format() july022008", &status); | 1699 assertSuccess("udat_format() july022008", &status); |
| 1667 | 1700 |
| 1668 if (u_strncmp(ubuf, expected, kUbufMax) != 0) | 1701 if (u_strncmp(ubuf, expected, kUbufMax) != 0) |
| 1669 log_err("fail: udat_format for locale, expected %s, got %s\n", | 1702 log_err("fail: udat_format for locale, expected %s, got %s\n", |
| 1670 u_austrncpy(bbuf1,expected,kUbufMax), u_austrncpy(bbuf2,ubuf
,kUbufMax) ); | 1703 u_austrncpy(bbuf1,expected,kUbufMax), u_austrncpy(bbuf2,ubuf
,kUbufMax) ); |
| 1671 | 1704 |
| 1672 udat_close(overrideFmt2); | |
| 1673 udat_close(fmt2); | 1705 udat_close(fmt2); |
| 1674 } | 1706 } |
| 1675 free(expected); | 1707 } |
| 1676 free(fields); | 1708 |
| 1709 /* |
| 1710 * Ticket #11523 |
| 1711 * udat_parse and udat_parseCalendar should have the same error code when given
the same invalid input. |
| 1712 */ |
| 1713 static void TestParseErrorReturnValue(void) { |
| 1714 UErrorCode status = U_ZERO_ERROR; |
| 1715 UErrorCode expectStatus = U_PARSE_ERROR; |
| 1716 UDateFormat* df; |
| 1717 UCalendar* cal; |
| 1718 |
| 1719 df = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, -1, &status
); |
| 1720 if (!assertSuccessCheck("udat_open()", &status, TRUE)) { |
| 1721 return; |
| 1722 } |
| 1723 |
| 1724 cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status); |
| 1725 if (!assertSuccess("ucal_open()", &status)) { |
| 1726 return; |
| 1727 } |
| 1728 |
| 1729 udat_parse(df, NULL, -1, NULL, &status); |
| 1730 if (status != expectStatus) { |
| 1731 log_err("%s should have been returned by udat_parse when given an invali
d input, instead got - %s\n", u_errorName(expectStatus), u_errorName(status)); |
| 1732 } |
| 1733 |
| 1734 status = U_ZERO_ERROR; |
| 1735 udat_parseCalendar(df, cal, NULL, -1, NULL, &status); |
| 1736 if (status != expectStatus) { |
| 1737 log_err("%s should have been returned by udat_parseCalendar when given a
n invalid input, instead got - %s\n", u_errorName(expectStatus), u_errorName(sta
tus)); |
| 1738 } |
| 1739 |
| 1740 ucal_close(cal); |
| 1741 udat_close(df); |
| 1742 } |
| 1743 |
| 1744 /* |
| 1745 * Ticket #11553 |
| 1746 * Test new udat_formatForFields, udat_formatCalendarForFields (and UFieldPositi
onIterator) |
| 1747 */ |
| 1748 static const char localeForFields[] = "en_US"; |
| 1749 /* zoneGMT[]defined above */ |
| 1750 static const UDate date2015Feb25 = 1424841000000.0; /* Wednesday, February 25, 2
015 at 5:10:00 AM GMT */ |
| 1751 |
| 1752 typedef struct { |
| 1753 int32_t field; |
| 1754 int32_t beginPos; |
| 1755 int32_t endPos; |
| 1756 } FieldsData; |
| 1757 static const FieldsData expectedFields[] = { |
| 1758 { UDAT_DAY_OF_WEEK_FIELD /* 9*/, 0, 9 }, |
| 1759 { UDAT_MONTH_FIELD /* 2*/, 11, 19 }, |
| 1760 { UDAT_DATE_FIELD /* 3*/, 20, 22 }, |
| 1761 { UDAT_YEAR_FIELD /* 1*/, 24, 28 }, |
| 1762 { UDAT_HOUR1_FIELD /*15*/, 32, 33 }, |
| 1763 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR |
| 1764 { UDAT_TIME_SEPARATOR_FIELD /*35*/, 33, 34 }, |
| 1765 #endif |
| 1766 { UDAT_MINUTE_FIELD /* 6*/, 34, 36 }, |
| 1767 #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR |
| 1768 { UDAT_TIME_SEPARATOR_FIELD /*35*/, 36, 37 }, |
| 1769 #endif |
| 1770 { UDAT_SECOND_FIELD /* 7*/, 37, 39 }, |
| 1771 { UDAT_AM_PM_FIELD /*14*/, 40, 42 }, |
| 1772 { UDAT_TIMEZONE_FIELD /*17*/, 43, 46 }, |
| 1773 { -1, -1, -1 }, |
| 1774 }; |
| 1775 |
| 1776 enum {kUBufFieldsLen = 128, kBBufFieldsLen = 256 }; |
| 1777 |
| 1778 static void TestFormatForFields(void) { |
| 1779 UErrorCode status = U_ZERO_ERROR; |
| 1780 UFieldPositionIterator* fpositer = ufieldpositer_open(&status); |
| 1781 if ( U_FAILURE(status) ) { |
| 1782 log_err("ufieldpositer_open fails, status %s\n", u_errorName(status)); |
| 1783 } else { |
| 1784 UDateFormat* udfmt = udat_open(UDAT_LONG, UDAT_FULL, localeForFields, zo
neGMT, -1, NULL, 0, &status); |
| 1785 UCalendar* ucal = ucal_open(zoneGMT, -1, localeForFields, UCAL_DEFAULT,
&status); |
| 1786 if ( U_FAILURE(status) ) { |
| 1787 log_data_err("udat_open or ucal_open fails for locale %s, status %s
(Are you missing data?)\n", localeForFields, u_errorName(status)); |
| 1788 } else { |
| 1789 int32_t ulen, field, beginPos, endPos; |
| 1790 UChar ubuf[kUBufFieldsLen]; |
| 1791 const FieldsData * fptr; |
| 1792 |
| 1793 status = U_ZERO_ERROR; |
| 1794 ulen = udat_formatForFields(udfmt, date2015Feb25, ubuf, kUBufFieldsL
en, fpositer, &status); |
| 1795 if ( U_FAILURE(status) ) { |
| 1796 log_err("udat_formatForFields fails, status %s\n", u_errorName(s
tatus)); |
| 1797 } else { |
| 1798 for (fptr = expectedFields; ; fptr++) { |
| 1799 field = ufieldpositer_next(fpositer, &beginPos, &endPos); |
| 1800 if (field != fptr->field || (field >= 0 && (beginPos != fptr
->beginPos || endPos != fptr->endPos))) { |
| 1801 if (fptr->field >= 0) { |
| 1802 log_err("udat_formatForFields as \"%s\"; expect fiel
d %d range %d-%d, get field %d range %d-%d\n", |
| 1803 aescstrdup(ubuf, ulen), fptr->field, fptr->b
eginPos, fptr->endPos, field, beginPos, endPos); |
| 1804 } else { |
| 1805 log_err("udat_formatForFields as \"%s\"; expect fiel
d < 0, get field %d range %d-%d\n", |
| 1806 aescstrdup(ubuf, ulen), field, beginPos, end
Pos); |
| 1807 } |
| 1808 break; |
| 1809 } |
| 1810 if (field < 0) { |
| 1811 break; |
| 1812 } |
| 1813 } |
| 1814 } |
| 1815 |
| 1816 ucal_setMillis(ucal, date2015Feb25, &status); |
| 1817 status = U_ZERO_ERROR; |
| 1818 ulen = udat_formatCalendarForFields(udfmt, ucal, ubuf, kUBufFieldsLe
n, fpositer, &status); |
| 1819 if ( U_FAILURE(status) ) { |
| 1820 log_err("udat_formatCalendarForFields fails, status %s\n", u_err
orName(status)); |
| 1821 } else { |
| 1822 for (fptr = expectedFields; ; fptr++) { |
| 1823 field = ufieldpositer_next(fpositer, &beginPos, &endPos); |
| 1824 if (field != fptr->field || (field >= 0 && (beginPos != fptr
->beginPos || endPos != fptr->endPos))) { |
| 1825 if (fptr->field >= 0) { |
| 1826 log_err("udat_formatFudat_formatCalendarForFieldsorF
ields as \"%s\"; expect field %d range %d-%d, get field %d range %d-%d\n", |
| 1827 aescstrdup(ubuf, ulen), fptr->field, fptr->b
eginPos, fptr->endPos, field, beginPos, endPos); |
| 1828 } else { |
| 1829 log_err("udat_formatCalendarForFields as \"%s\"; exp
ect field < 0, get field %d range %d-%d\n", |
| 1830 aescstrdup(ubuf, ulen), field, beginPos, end
Pos); |
| 1831 } |
| 1832 break; |
| 1833 } |
| 1834 if (field < 0) { |
| 1835 break; |
| 1836 } |
| 1837 } |
| 1838 } |
| 1839 |
| 1840 ucal_close(ucal); |
| 1841 udat_close(udfmt); |
| 1842 } |
| 1843 ufieldpositer_close(fpositer); |
| 1844 } |
| 1677 } | 1845 } |
| 1678 | 1846 |
| 1679 #endif /* #if !UCONFIG_NO_FORMATTING */ | 1847 #endif /* #if !UCONFIG_NO_FORMATTING */ |
| OLD | NEW |