1 /******************************************************************** 2 * Copyright (c) 2008-2009, International Business Machines Corporation and 3 * others. All Rights Reserved. 4 ********************************************************************/ 5 6 #include "unicode/utypes.h" 7 8 #if !UCONFIG_NO_FORMATTING 9 10 #include "unicode/tmunit.h" 11 #include "unicode/tmutamt.h" 12 #include "unicode/tmutfmt.h" 13 #include "tufmtts.h" 14 15 16 //TODO: put as compilation flag 17 //#define TUFMTTS_DEBUG 1 18 19 #ifdef TUFMTTS_DEBUG 20 #include <iostream> 21 #endif 22 23 void TimeUnitTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) { 24 if (exec) logln("TestSuite TimeUnitTest"); 25 switch (index) { 26 TESTCASE(0, testBasic); 27 TESTCASE(1, testAPI); 28 default: name = ""; break; 29 } 30 } 31 32 /** 33 * Test basic 34 */ 35 void TimeUnitTest::testBasic() { 36 const char* locales[] = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant"}; 37 for ( unsigned int locIndex = 0; 38 locIndex < sizeof(locales)/sizeof(locales[0]); 39 ++locIndex ) { 40 UErrorCode status = U_ZERO_ERROR; 41 Locale loc(locales[locIndex]); 42 TimeUnitFormat** formats = new TimeUnitFormat*[2]; 43 formats[TimeUnitFormat::kFull] = new TimeUnitFormat(loc, status); 44 if (!assertSuccess("TimeUnitFormat(full)", status, TRUE)) return; 45 formats[TimeUnitFormat::kAbbreviate] = new TimeUnitFormat(loc, TimeUnitFormat::kAbbreviate, status); 46 if (!assertSuccess("TimeUnitFormat(short)", status)) return; 47 #ifdef TUFMTTS_DEBUG 48 std::cout << "locale: " << locales[locIndex] << "\n"; 49 #endif 50 for (int style = TimeUnitFormat::kFull; 51 style <= TimeUnitFormat::kAbbreviate; 52 ++style) { 53 for (TimeUnit::UTimeUnitFields j = TimeUnit::UTIMEUNIT_YEAR; 54 j < TimeUnit::UTIMEUNIT_FIELD_COUNT; 55 j = (TimeUnit::UTimeUnitFields)(j+1)) { 56 #ifdef TUFMTTS_DEBUG 57 std::cout << "time unit: " << j << "\n"; 58 #endif 59 double tests[] = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35}; 60 for (unsigned int i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) { 61 #ifdef TUFMTTS_DEBUG 62 std::cout << "number: " << tests[i] << "\n"; 63 #endif 64 TimeUnitAmount* source = new TimeUnitAmount(tests[i], j, status); 65 if (!assertSuccess("TimeUnitAmount()", status)) return; 66 UnicodeString formatted; 67 Formattable formattable; 68 formattable.adoptObject(source); 69 formatted = ((Format*)formats[style])->format(formattable, formatted, status); 70 if (!assertSuccess("format()", status)) return; 71 #ifdef TUFMTTS_DEBUG 72 char formatResult[1000]; 73 formatted.extract(0, formatted.length(), formatResult, "UTF-8"); 74 std::cout << "format result: " << formatResult << "\n"; 75 #endif 76 Formattable result; 77 ((Format*)formats[style])->parseObject(formatted, result, status); 78 if (!assertSuccess("parseObject()", status)) return; 79 if (result != formattable) { 80 dataerrln("No round trip: "); 81 } 82 // other style parsing 83 Formattable result_1; 84 ((Format*)formats[1-style])->parseObject(formatted, result_1, status); 85 if (!assertSuccess("parseObject()", status)) return; 86 if (result_1 != formattable) { 87 dataerrln("No round trip: "); 88 } 89 } 90 } 91 } 92 delete formats[TimeUnitFormat::kFull]; 93 delete formats[TimeUnitFormat::kAbbreviate]; 94 delete[] formats; 95 } 96 } 97 98 99 void TimeUnitTest::testAPI() { 100 //================= TimeUnit ================= 101 UErrorCode status = U_ZERO_ERROR; 102 103 TimeUnit* tmunit = TimeUnit::createInstance(TimeUnit::UTIMEUNIT_YEAR, status); 104 if (!assertSuccess("TimeUnit::createInstance", status)) return; 105 106 TimeUnit* another = (TimeUnit*)tmunit->clone(); 107 TimeUnit third(*tmunit); 108 TimeUnit fourth = third; 109 110 assertTrue("orig and clone are equal", (*tmunit == *another)); 111 assertTrue("copied and assigned are equal", (third == fourth)); 112 113 TimeUnit* tmunit_m = TimeUnit::createInstance(TimeUnit::UTIMEUNIT_MONTH, status); 114 assertTrue("year != month", (*tmunit != *tmunit_m)); 115 116 TimeUnit::UTimeUnitFields field = tmunit_m->getTimeUnitField(); 117 assertTrue("field of month time unit is month", (field == TimeUnit::UTIMEUNIT_MONTH)); 118 119 delete tmunit; 120 delete another; 121 delete tmunit_m; 122 // 123 //================= TimeUnitAmount ================= 124 125 Formattable formattable((int32_t)2); 126 TimeUnitAmount tma_long(formattable, TimeUnit::UTIMEUNIT_DAY, status); 127 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; 128 129 formattable.setDouble(2); 130 TimeUnitAmount tma_double(formattable, TimeUnit::UTIMEUNIT_DAY, status); 131 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; 132 133 formattable.setDouble(3); 134 TimeUnitAmount tma_double_3(formattable, TimeUnit::UTIMEUNIT_DAY, status); 135 if (!assertSuccess("TimeUnitAmount(formattable...)", status)) return; 136 137 TimeUnitAmount tma(2, TimeUnit::UTIMEUNIT_DAY, status); 138 if (!assertSuccess("TimeUnitAmount(number...)", status)) return; 139 140 TimeUnitAmount tma_h(2, TimeUnit::UTIMEUNIT_HOUR, status); 141 if (!assertSuccess("TimeUnitAmount(number...)", status)) return; 142 143 TimeUnitAmount second(tma); 144 TimeUnitAmount third_tma = tma; 145 TimeUnitAmount* fourth_tma = (TimeUnitAmount*)tma.clone(); 146 147 assertTrue("orig and copy are equal", (second == tma)); 148 assertTrue("clone and assigned are equal", (third_tma == *fourth_tma)); 149 assertTrue("different if number diff", (tma_double != tma_double_3)); 150 assertTrue("different if number type diff", (tma_double != tma_long)); 151 assertTrue("different if time unit diff", (tma != tma_h)); 152 assertTrue("same even different constructor", (tma_double == tma)); 153 154 assertTrue("getTimeUnitField", (tma.getTimeUnitField() == TimeUnit::UTIMEUNIT_DAY)); 155 delete fourth_tma; 156 // 157 //================= TimeUnitFormat ================= 158 // 159 TimeUnitFormat* tmf_en = new TimeUnitFormat(Locale("en"), status); 160 if (!assertSuccess("TimeUnitFormat(en...)", status, TRUE)) return; 161 TimeUnitFormat tmf_fr(Locale("fr"), status); 162 if (!assertSuccess("TimeUnitFormat(fr...)", status)) return; 163 164 assertTrue("TimeUnitFormat: en and fr diff", (*tmf_en != tmf_fr)); 165 166 TimeUnitFormat tmf_assign = *tmf_en; 167 assertTrue("TimeUnitFormat: orig and assign are equal", (*tmf_en == tmf_assign)); 168 169 TimeUnitFormat tmf_copy(tmf_fr); 170 assertTrue("TimeUnitFormat: orig and copy are equal", (tmf_fr == tmf_copy)); 171 172 TimeUnitFormat* tmf_clone = (TimeUnitFormat*)tmf_en->clone(); 173 assertTrue("TimeUnitFormat: orig and clone are equal", (*tmf_en == *tmf_clone)); 174 delete tmf_clone; 175 176 tmf_en->setLocale(Locale("fr"), status); 177 if (!assertSuccess("setLocale(fr...)", status)) return; 178 179 NumberFormat* numberFmt = NumberFormat::createInstance( 180 Locale("fr"), status); 181 if (!assertSuccess("NumberFormat::createInstance()", status)) return; 182 tmf_en->setNumberFormat(*numberFmt, status); 183 if (!assertSuccess("setNumberFormat(en...)", status)) return; 184 assertTrue("TimeUnitFormat: setLocale", (*tmf_en == tmf_fr)); 185 186 delete tmf_en; 187 188 TimeUnitFormat* en_long = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kFull, status); 189 if (!assertSuccess("TimeUnitFormat(en...)", status)) return; 190 delete en_long; 191 192 TimeUnitFormat* en_short = new TimeUnitFormat(Locale("en"), TimeUnitFormat::kAbbreviate, status); 193 if (!assertSuccess("TimeUnitFormat(en...)", status)) return; 194 delete en_short; 195 196 TimeUnitFormat* format = new TimeUnitFormat(status); 197 format->setLocale(Locale("zh"), status); 198 format->setNumberFormat(*numberFmt, status); 199 if (!assertSuccess("TimeUnitFormat(en...)", status)) return; 200 delete numberFmt; 201 delete format; 202 } 203 204 205 #endif 206