]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/test/intltest/tufmtts.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / tufmtts.cpp
index 20ffe253f27f064397dff584dd2835a47569b819..91a26678caf62bb6eebd79f47f180f7af1d6b8ab 100644 (file)
@@ -1,5 +1,7 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /********************************************************************
- * Copyright (c) 2008-2014, International Business Machines Corporation and
+ * Copyright (c) 2008-2016, International Business Machines Corporation and
  * others. All Rights Reserved.
  ********************************************************************/
 
@@ -11,8 +13,9 @@
 #include "unicode/tmunit.h"
 #include "unicode/tmutamt.h"
 #include "unicode/tmutfmt.h"
-#include "tufmtts.h"
 #include "unicode/ustring.h"
+#include "cmemory.h"
+#include "intltest.h"
 
 //TODO: put as compilation flag
 //#define TUFMTTS_DEBUG 1
 #include <iostream>
 #endif
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
-void TimeUnitTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) {
-    if (exec) logln("TestSuite TimeUnitTest");
-    switch (index) {
-        TESTCASE(0, testBasic);
-        TESTCASE(1, testAPI);
-        TESTCASE(2, testGreekWithFallback);
-        TESTCASE(3, testGreekWithSanitization);
-        TESTCASE(4, test10219Plurals);
-        default: name = ""; break;
+class TimeUnitTest : public IntlTest {
+    void runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/ ) {
+        if (exec) logln("TestSuite TimeUnitTest");
+        TESTCASE_AUTO_BEGIN;
+        TESTCASE_AUTO(testBasic);
+        TESTCASE_AUTO(testAPI);
+        TESTCASE_AUTO(testGreekWithFallback);
+        TESTCASE_AUTO(testGreekWithSanitization);
+        TESTCASE_AUTO(test10219Plurals);
+        TESTCASE_AUTO(TestBritishShortHourFallback);
+        TESTCASE_AUTO_END;
     }
+
+public:
+    /**
+     * Performs basic tests
+     **/
+    void testBasic();
+
+    /**
+     * Performs API tests
+     **/
+    void testAPI();
+
+    /**
+     * Performs tests for Greek
+     * This tests that requests for short unit names correctly fall back 
+     * to long unit names for a locale where the locale data does not 
+     * provide short unit names. As of CLDR 1.9, Greek is one such language.
+     **/
+    void testGreekWithFallback();
+
+    /**
+     * Performs tests for Greek
+     * This tests that if the plural count listed in time unit format does not
+     * match those in the plural rules for the locale, those plural count in
+     * time unit format will be ingored and subsequently, fall back will kick in
+     * which is tested above.
+     * Without data sanitization, setNumberFormat() would crash.
+     * As of CLDR shiped in ICU4.8, Greek is one such language.
+     */
+    void testGreekWithSanitization();
+
+    /**
+     * Performs unit test for ticket 10219 making sure that plurals work
+     * correctly with rounding.
+     */
+    void test10219Plurals();
+
+    void TestBritishShortHourFallback();
+};
+
+extern IntlTest *createTimeUnitTest() {
+    return new TimeUnitTest();
 }
 
 // This function is more lenient than equals operator as it considers integer 3 hours and
@@ -58,7 +103,7 @@ static UBool tmaEqual(const TimeUnitAmount& left, const TimeUnitAmount& right) {
 void TimeUnitTest::testBasic() {
     const char* locales[] = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant", "pa"};
     for ( unsigned int locIndex = 0; 
-          locIndex < sizeof(locales)/sizeof(locales[0]); 
+          locIndex < UPRV_LENGTHOF(locales); 
           ++locIndex ) {
         UErrorCode status = U_ZERO_ERROR;
         Locale loc(locales[locIndex]);
@@ -80,7 +125,7 @@ void TimeUnitTest::testBasic() {
             std::cout << "time unit: " << j << "\n";
 #endif
             double tests[] = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
-            for (unsigned int i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
+            for (unsigned int i = 0; i < UPRV_LENGTHOF(tests); ++i) {
 #ifdef TUFMTTS_DEBUG
                 std::cout << "number: " << tests[i] << "\n";
 #endif
@@ -276,15 +321,18 @@ void TimeUnitTest::testGreekWithFallback() {
     const UChar oneMonth[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b1, 0x03c2, 0};
     const UChar oneMonthShort[] = {0x0031, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x002e, 0};
     const UChar oneYear[] = {0x0031, 0x0020, 0x03ad, 0x03c4, 0x03bf, 0x03c2, 0};
+    const UChar oneYearShort[] = {0x0031, 0x0020, 0x03ad, 0x03c4, 0x002e, 0};
     const UChar sevenSeconds[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0x03b5, 0x03c1, 0x03cc, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03b1, 0};
     const UChar sevenSecondsShort[] = {0x0037, 0x0020, 0x03b4, 0x03b5, 0x03c5, 0x03c4, 0x002e, 0};
     const UChar sevenMinutes[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x03c4, 0x03ac, 0};
     const UChar sevenMinutesShort[] = {0x0037, 0x0020, 0x03bb, 0x03b5, 0x03c0, 0x002e, 0};
     const UChar sevenHours[] = {0x0037, 0x0020, 0x03ce, 0x03c1, 0x03b5, 0x03c2, 0};
+    const UChar sevenHoursShort[] = {0x0037, 0x0020, 0x03ce, 0x03c1, 0x002e, 0};
     const UChar sevenDays[] = {0x0037, 0x0020, 0x03b7, 0x03bc, 0x03ad, 0x03c1, 0x03b5, 0x03c2, 0};
     const UChar sevenMonths[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x03b5, 0x3c2, 0};
     const UChar sevenMonthsShort[] = {0x0037, 0x0020, 0x03bc, 0x03ae, 0x03bd, 0x002e, 0};
     const UChar sevenYears[] = {0x0037, 0x0020, 0x03ad, 0x03c4, 0x03b7, 0};
+    const UChar sevenYearsShort[] = {0x0037, 0x0020, 0x03ad, 0x03c4, 0x002e, 0};
 
     const UnicodeString oneSecondStr(oneSecond);
     const UnicodeString oneSecondShortStr(oneSecondShort);
@@ -295,42 +343,47 @@ void TimeUnitTest::testGreekWithFallback() {
     const UnicodeString oneMonthStr(oneMonth);
     const UnicodeString oneMonthShortStr(oneMonthShort);
     const UnicodeString oneYearStr(oneYear);
+    const UnicodeString oneYearShortStr(oneYearShort);
     const UnicodeString sevenSecondsStr(sevenSeconds);
     const UnicodeString sevenSecondsShortStr(sevenSecondsShort);
     const UnicodeString sevenMinutesStr(sevenMinutes);
     const UnicodeString sevenMinutesShortStr(sevenMinutesShort);
     const UnicodeString sevenHoursStr(sevenHours);
+    const UnicodeString sevenHoursShortStr(sevenHoursShort);
     const UnicodeString sevenDaysStr(sevenDays);
     const UnicodeString sevenMonthsStr(sevenMonths);
     const UnicodeString sevenMonthsShortStr(sevenMonthsShort);
     const UnicodeString sevenYearsStr(sevenYears);
+    const UnicodeString sevenYearsShortStr(sevenYearsShort);
+
+    const UnicodeString expected[] = {
+            oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneYearStr,
+            oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMonthShortStr, oneYearShortStr,
+            sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, sevenMonthsStr, sevenYearsStr,
+            sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursShortStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsShortStr,
 
-    const UnicodeString expected[] = {oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneYearStr,
-                              oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMonthShortStr, oneYearStr,
-                              sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, sevenMonthsStr, sevenYearsStr,
-                              sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsStr,
-                              oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneYearStr,
-                              oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMonthShortStr, oneYearStr,
-                              sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, sevenMonthsStr, sevenYearsStr,
-                              sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsStr};
+            oneSecondStr, oneMinuteStr, oneHourStr, oneDayStr, oneMonthStr, oneYearStr,
+            oneSecondShortStr, oneMinuteShortStr, oneHourStr, oneDayStr, oneMonthShortStr, oneYearShortStr,
+            sevenSecondsStr, sevenMinutesStr, sevenHoursStr, sevenDaysStr, sevenMonthsStr, sevenYearsStr,
+            sevenSecondsShortStr, sevenMinutesShortStr, sevenHoursShortStr, sevenDaysStr, sevenMonthsShortStr, sevenYearsShortStr};
 
     int counter = 0;
     for ( unsigned int locIndex = 0;
-        locIndex < sizeof(locales)/sizeof(locales[0]);
+        locIndex < UPRV_LENGTHOF(locales);
         ++locIndex ) {
 
         Locale l = Locale::createFromName(locales[locIndex]);
 
         for ( unsigned int numberIndex = 0;
-            numberIndex < sizeof(numbers)/sizeof(int);
+            numberIndex < UPRV_LENGTHOF(numbers);
             ++numberIndex ) {
 
             for ( unsigned int styleIndex = 0;
-                styleIndex < sizeof(styles)/sizeof(styles[0]);
+                styleIndex < UPRV_LENGTHOF(styles);
                 ++styleIndex ) {
 
                 for ( unsigned int unitIndex = 0;
-                    unitIndex < sizeof(tunits)/sizeof(tunits[0]);
+                    unitIndex < UPRV_LENGTHOF(tunits);
                     ++unitIndex ) {
 
                     TimeUnitAmount *tamt = new TimeUnitAmount(numbers[numberIndex], tunits[unitIndex], status);
@@ -422,8 +475,8 @@ void TimeUnitTest::test10219Plurals() {
         dataerrln("generating NumberFormat Object failed: %s", u_errorName(status));
         return;
     }
-    for (int32_t j = 0; j < LENGTHOF(values); ++j) {
-        for (int32_t i = 0; i < LENGTHOF(expected[j]); ++i) {
+    for (int32_t j = 0; j < UPRV_LENGTHOF(values); ++j) {
+        for (int32_t i = 0; i < UPRV_LENGTHOF(expected[j]); ++i) {
             nf->setMinimumFractionDigits(i);
             nf->setMaximumFractionDigits(i);
             nf->setRoundingMode(DecimalFormat::kRoundDown);
@@ -434,7 +487,8 @@ void TimeUnitTest::test10219Plurals() {
             }
             UnicodeString actual;
             Formattable fmt;
-            LocalPointer<TimeUnitAmount> tamt(new TimeUnitAmount(values[j], TimeUnit::UTIMEUNIT_MINUTE, status));
+            LocalPointer<TimeUnitAmount> tamt(
+                new TimeUnitAmount(values[j], TimeUnit::UTIMEUNIT_MINUTE, status), status);
             if (U_FAILURE(status)) {
                 dataerrln("generating TimeUnitAmount Object failed: %s", u_errorName(status));
                 return;
@@ -461,4 +515,16 @@ void TimeUnitTest::test10219Plurals() {
     }
 }
 
+void TimeUnitTest::TestBritishShortHourFallback() {
+    // See ticket #11986 "incomplete fallback in MeasureFormat".
+    UErrorCode status = U_ZERO_ERROR;
+    Formattable oneHour(new TimeUnitAmount(1, TimeUnit::UTIMEUNIT_HOUR, status));
+    Locale en_GB("en_GB");
+    TimeUnitFormat formatter(en_GB, UTMUTFMT_ABBREVIATED_STYLE, status);
+    UnicodeString result;
+    formatter.format(oneHour, result, status);
+    assertSuccess("TestBritishShortHourFallback()", status);
+    assertEquals("TestBritishShortHourFallback()", UNICODE_STRING_SIMPLE("1 hr"), result, TRUE);
+}
+
 #endif