Home | History | Annotate | Download | only in intltest
      1 /*
      2 *******************************************************************************
      3 *
      4 *   Copyright (C) 2012, International Business Machines
      5 *   Corporation and others.  All Rights Reserved.
      6 *
      7 *******************************************************************************
      8 *   file name:  listformattertest.cpp
      9 *   encoding:   US-ASCII
     10 *   tab size:   8 (not used)
     11 *   indentation:4
     12 *
     13 *   created on: 2012aug27
     14 *   created by: Umesh P. Nair
     15 */
     16 
     17 #include "listformattertest.h"
     18 #include <string.h>
     19 
     20 ListFormatterTest::ListFormatterTest() :
     21         prefix("Prefix: ", -1, US_INV),
     22         one("Alice", -1, US_INV), two("Bob", -1, US_INV),
     23         three("Charlie", -1, US_INV), four("Delta", -1, US_INV) {
     24 }
     25 
     26 void ListFormatterTest::CheckFormatting(const ListFormatter* formatter, UnicodeString data[], int32_t dataSize,
     27                                         const UnicodeString& expected_result) {
     28     UnicodeString actualResult(prefix);
     29     UErrorCode errorCode = U_ZERO_ERROR;
     30     formatter->format(data, dataSize, actualResult, errorCode);
     31     UnicodeString expectedStringWithPrefix = prefix + expected_result;
     32     if (expectedStringWithPrefix != actualResult) {
     33         errln(UnicodeString("Expected: |") + expectedStringWithPrefix +  "|, Actual: |" + actualResult + "|");
     34     }
     35 }
     36 
     37 void ListFormatterTest::CheckFourCases(const char* locale_string, UnicodeString one, UnicodeString two,
     38         UnicodeString three, UnicodeString four, UnicodeString results[4]) {
     39     UErrorCode errorCode = U_ZERO_ERROR;
     40     ListFormatter* formatter = ListFormatter::createInstance(Locale(locale_string), errorCode);
     41     if (formatter == NULL ||  U_FAILURE(errorCode)) {
     42         errln("Allocation problem\n");
     43         return;
     44     }
     45     UnicodeString input1[] = {one};
     46     CheckFormatting(formatter, input1, 1, results[0]);
     47 
     48     UnicodeString input2[] = {one, two};
     49     CheckFormatting(formatter, input2, 2, results[1]);
     50 
     51     UnicodeString input3[] = {one, two, three};
     52     CheckFormatting(formatter, input3, 3, results[2]);
     53 
     54     UnicodeString input4[] = {one, two, three, four};
     55     CheckFormatting(formatter, input4, 4, results[3]);
     56 
     57     delete formatter;
     58 }
     59 
     60 
     61 void ListFormatterTest::TestLocaleFallback() {
     62     const char* testData[][4] = {
     63         {"en_US", "en", "", ""},    // ULocale.getFallback("") should return ""
     64         {"EN_us_Var", "en_US", "en", ""},   // Case is always normalized
     65         {"de_DE@collation=phonebook", "de@collation=phonebook", "@collation=phonebook", "@collation=phonebook"},    // Keyword is preserved
     66         {"en__POSIX", "en", "", ""},    // Trailing empty segment should be truncated
     67         {"_US_POSIX", "_US", "", ""},   // Same as above
     68         {"root", "", "", ""},               // No canonicalization
     69     };
     70     for (int i = 0; i < 6; ++i) {
     71         for(int j = 1; j < 4; ++j) {
     72             Locale in(testData[i][j-1]);
     73             Locale out;
     74             UErrorCode errorCode = U_ZERO_ERROR;
     75             ListFormatter::getFallbackLocale(in, out, errorCode);
     76             if (U_FAILURE(errorCode)) {
     77                 errln("Error in getLocaleFallback: %s", u_errorName(errorCode));
     78             }
     79 
     80             if (::strcmp(testData[i][j], out.getName())) {
     81                 errln("Expected: |%s|, Actual: |%s|\n", testData[i][j], out.getName());
     82             }
     83         }
     84     }
     85 }
     86 
     87 void ListFormatterTest::TestRoot() {
     88     UnicodeString results[4] = {
     89         one,
     90         one + ", " + two,
     91         one + ", " + two + ", " + three,
     92         one + ", " + two + ", " + three + ", " + four
     93     };
     94 
     95     CheckFourCases("", one, two, three, four, results);
     96 }
     97 
     98 // Bogus locale should fallback to root.
     99 void ListFormatterTest::TestBogus() {
    100     UnicodeString results[4] = {
    101         one,
    102         one + ", " + two,
    103         one + ", " + two + ", " + three,
    104         one + ", " + two + ", " + three + ", " + four
    105     };
    106 
    107     CheckFourCases("ex_PY", one, two, three, four, results);
    108 }
    109 
    110 // Formatting in English.
    111 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
    112 void ListFormatterTest::TestEnglish() {
    113     UnicodeString results[4] = {
    114         one,
    115         one + " and " + two,
    116         one + ", " + two + ", and " + three,
    117         one + ", " + two + ", " + three + ", and " + four
    118     };
    119 
    120     CheckFourCases("en", one, two, three, four, results);
    121 }
    122 
    123 void ListFormatterTest::TestEnglishUS() {
    124     UnicodeString results[4] = {
    125         one,
    126         one + " and " + two,
    127         one + ", " + two + ", and " + three,
    128         one + ", " + two + ", " + three + ", and " + four
    129     };
    130 
    131     CheckFourCases("en_US", one, two, three, four, results);
    132 }
    133 
    134 // Formatting in Russian.
    135 // "\\u0438" is used before the last element, and all elements up to (but not including) the penultimate are followed by a comma.
    136 void ListFormatterTest::TestRussian() {
    137     UnicodeString and_string = UnicodeString(" \\u0438 ", -1, US_INV).unescape();
    138     UnicodeString results[4] = {
    139         one,
    140         one + and_string + two,
    141         one + ", " + two + and_string + three,
    142         one + ", " + two + ", " + three + and_string + four
    143     };
    144 
    145     CheckFourCases("ru", one, two, three, four, results);
    146 }
    147 
    148 // Formatting in Malayalam.
    149 // For two elements, "\\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46" is inserted in between.
    150 // For more than two elements, comma is inserted between all elements up to (and including) the penultimate,
    151 // and the word \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35 is inserted in the end.
    152 void ListFormatterTest::TestMalayalam() {
    153     UnicodeString pair_string = UnicodeString(" \\u0d15\\u0d42\\u0d1f\\u0d3e\\u0d24\\u0d46 ", -1, US_INV).unescape();
    154     UnicodeString total_string = UnicodeString(" \\u0d0e\\u0d28\\u0d4d\\u0d28\\u0d3f\\u0d35", -1, US_INV).unescape();
    155     UnicodeString results[4] = {
    156         one,
    157         one + pair_string + two,
    158         one + ", " + two + ", " + three + total_string,
    159         one + ", " + two + ", " + three + ", " + four + total_string
    160     };
    161 
    162     CheckFourCases("ml", one, two, three, four, results);
    163 }
    164 
    165 // Formatting in Zulu.
    166 // "and" is used before the last element, and all elements up to (and including) the penultimate are followed by a comma.
    167 void ListFormatterTest::TestZulu() {
    168     UnicodeString results[4] = {
    169         one,
    170         "I-" + one + " ne-" + two,
    171         one + ", " + two + ", no-" + three,
    172         one + ", " + two + ", " + three + ", no-" + four
    173     };
    174 
    175     CheckFourCases("zu", one, two, three, four, results);
    176 }
    177 
    178 void ListFormatterTest::TestOutOfOrderPatterns() {
    179     UnicodeString results[4] = {
    180         one,
    181         two + " after " + one,
    182         three + " in the last after " + two + " after the first " + one,
    183         four + " in the last after " + three + " after " + two + " after the first " + one
    184     };
    185 
    186     UErrorCode errorCode = U_ZERO_ERROR;
    187     ListFormatData data("{1} after {0}", "{1} after the first {0}",
    188                         "{1} after {0}", "{1} in the last after {0}");
    189     ListFormatter formatter(data);
    190 
    191     UnicodeString input1[] = {one};
    192     CheckFormatting(&formatter, input1, 1, results[0]);
    193 
    194     UnicodeString input2[] = {one, two};
    195     CheckFormatting(&formatter, input2, 2, results[1]);
    196 
    197     UnicodeString input3[] = {one, two, three};
    198     CheckFormatting(&formatter, input3, 3, results[2]);
    199 
    200     UnicodeString input4[] = {one, two, three, four};
    201     CheckFormatting(&formatter, input4, 4, results[3]);
    202 }
    203 
    204 void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
    205                                        const char* &name, char* /*par */) {
    206     switch(index) {
    207         case 0: name = "TestRoot"; if (exec) TestRoot(); break;
    208         case 1: name = "TestBogus"; if (exec) TestBogus(); break;
    209         case 2: name = "TestEnglish"; if (exec) TestEnglish(); break;
    210         case 3: name = "TestEnglishUS"; if (exec) TestEnglishUS(); break;
    211         case 4: name = "TestRussian"; if (exec) TestRussian(); break;
    212         case 5: name = "TestMalayalam"; if (exec) TestMalayalam(); break;
    213         case 6: name = "TestZulu"; if (exec) TestZulu(); break;
    214         case 7: name = "TestLocaleFallback"; if (exec) TestLocaleFallback(); break;
    215         case 8: name = "TestOutOfOrderPatterns"; if (exec) TestLocaleFallback(); break;
    216 
    217         default: name = ""; break;
    218     }
    219 }
    220