2017-01-20 00:20:31 +00:00
// © 2016 and later: Unicode, Inc. and others.
2016-06-15 18:58:17 +00:00
// License & terms of use: http://www.unicode.org/copyright.html
2008-09-19 19:05:37 +00:00
/********************************************************************
2016-05-31 21:45:07 +00:00
* Copyright ( c ) 2008 - 2016 , International Business Machines Corporation and
* others . All Rights Reserved .
2008-09-19 19:05:37 +00:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "unicode/utypes.h"
# if !UCONFIG_NO_FORMATTING
2013-10-10 21:52:15 +00:00
# include "unicode/decimfmt.h"
2008-09-19 19:05:37 +00:00
# include "unicode/tmunit.h"
# include "unicode/tmutamt.h"
# include "unicode/tmutfmt.h"
2010-10-19 08:28:02 +00:00
# include "unicode/ustring.h"
2015-11-12 22:09:04 +00:00
# include "cmemory.h"
# include "intltest.h"
2008-09-19 19:05:37 +00:00
2008-10-22 06:06:56 +00:00
//TODO: put as compilation flag
2008-09-19 19:05:37 +00:00
//#define TUFMTTS_DEBUG 1
# ifdef TUFMTTS_DEBUG
# include <iostream>
# endif
2015-11-12 22:09:04 +00:00
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 ;
2008-09-19 19:05:37 +00:00
}
2015-11-12 22:09:04 +00:00
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 ( ) ;
2008-09-19 19:05:37 +00:00
}
2013-10-10 21:52:15 +00:00
// This function is more lenient than equals operator as it considers integer 3 hours and
// double 3.0 hours to be equal
static UBool tmaEqual ( const TimeUnitAmount & left , const TimeUnitAmount & right ) {
if ( left . getTimeUnitField ( ) ! = right . getTimeUnitField ( ) ) {
return FALSE ;
}
UErrorCode status = U_ZERO_ERROR ;
if ( ! left . getNumber ( ) . isNumeric ( ) | | ! right . getNumber ( ) . isNumeric ( ) ) {
return FALSE ;
}
UBool result = left . getNumber ( ) . getDouble ( status ) = = right . getNumber ( ) . getDouble ( status ) ;
if ( U_FAILURE ( status ) ) {
return FALSE ;
}
return result ;
}
2008-09-19 19:05:37 +00:00
/**
* Test basic
*/
void TimeUnitTest : : testBasic ( ) {
2010-10-19 08:28:02 +00:00
const char * locales [ ] = { " en " , " sl " , " fr " , " zh " , " ar " , " ru " , " zh_Hant " , " pa " } ;
2008-09-19 19:05:37 +00:00
for ( unsigned int locIndex = 0 ;
2016-02-23 10:40:09 +00:00
locIndex < UPRV_LENGTHOF ( locales ) ;
2008-09-19 19:05:37 +00:00
+ + locIndex ) {
UErrorCode status = U_ZERO_ERROR ;
Locale loc ( locales [ locIndex ] ) ;
TimeUnitFormat * * formats = new TimeUnitFormat * [ 2 ] ;
2011-04-28 16:42:58 +00:00
formats [ UTMUTFMT_FULL_STYLE ] = new TimeUnitFormat ( loc , status ) ;
2009-06-12 19:34:21 +00:00
if ( ! assertSuccess ( " TimeUnitFormat(full) " , status , TRUE ) ) return ;
2011-04-28 16:42:58 +00:00
formats [ UTMUTFMT_ABBREVIATED_STYLE ] = new TimeUnitFormat ( loc , UTMUTFMT_ABBREVIATED_STYLE , status ) ;
2008-09-19 19:05:37 +00:00
if ( ! assertSuccess ( " TimeUnitFormat(short) " , status ) ) return ;
# ifdef TUFMTTS_DEBUG
std : : cout < < " locale: " < < locales [ locIndex ] < < " \n " ;
# endif
2011-04-28 16:42:58 +00:00
for ( int style = UTMUTFMT_FULL_STYLE ;
style < = UTMUTFMT_ABBREVIATED_STYLE ;
2008-09-19 19:05:37 +00:00
+ + style ) {
for ( TimeUnit : : UTimeUnitFields j = TimeUnit : : UTIMEUNIT_YEAR ;
j < TimeUnit : : UTIMEUNIT_FIELD_COUNT ;
j = ( TimeUnit : : UTimeUnitFields ) ( j + 1 ) ) {
# ifdef TUFMTTS_DEBUG
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 } ;
2016-02-23 10:40:09 +00:00
for ( unsigned int i = 0 ; i < UPRV_LENGTHOF ( tests ) ; + + i ) {
2008-09-19 19:05:37 +00:00
# ifdef TUFMTTS_DEBUG
std : : cout < < " number: " < < tests [ i ] < < " \n " ;
# endif
TimeUnitAmount * source = new TimeUnitAmount ( tests [ i ] , j , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount() " , status ) ) return ;
UnicodeString formatted ;
Formattable formattable ;
formattable . adoptObject ( source ) ;
formatted = ( ( Format * ) formats [ style ] ) - > format ( formattable , formatted , status ) ;
if ( ! assertSuccess ( " format() " , status ) ) return ;
# ifdef TUFMTTS_DEBUG
char formatResult [ 1000 ] ;
formatted . extract ( 0 , formatted . length ( ) , formatResult , " UTF-8 " ) ;
std : : cout < < " format result: " < < formatResult < < " \n " ;
# endif
Formattable result ;
( ( Format * ) formats [ style ] ) - > parseObject ( formatted , result , status ) ;
if ( ! assertSuccess ( " parseObject() " , status ) ) return ;
2013-10-10 21:52:15 +00:00
if ( ! tmaEqual ( * ( ( TimeUnitAmount * ) result . getObject ( ) ) , * ( ( TimeUnitAmount * ) formattable . getObject ( ) ) ) ) {
2008-09-19 19:05:37 +00:00
dataerrln ( " No round trip: " ) ;
}
// other style parsing
Formattable result_1 ;
( ( Format * ) formats [ 1 - style ] ) - > parseObject ( formatted , result_1 , status ) ;
if ( ! assertSuccess ( " parseObject() " , status ) ) return ;
2013-10-10 21:52:15 +00:00
if ( ! tmaEqual ( * ( ( TimeUnitAmount * ) result_1 . getObject ( ) ) , * ( ( TimeUnitAmount * ) formattable . getObject ( ) ) ) ) {
2008-09-19 19:05:37 +00:00
dataerrln ( " No round trip: " ) ;
}
}
}
}
2011-04-28 16:42:58 +00:00
delete formats [ UTMUTFMT_FULL_STYLE ] ;
delete formats [ UTMUTFMT_ABBREVIATED_STYLE ] ;
2008-09-19 19:05:37 +00:00
delete [ ] formats ;
}
}
void TimeUnitTest : : testAPI ( ) {
//================= TimeUnit =================
UErrorCode status = U_ZERO_ERROR ;
TimeUnit * tmunit = TimeUnit : : createInstance ( TimeUnit : : UTIMEUNIT_YEAR , status ) ;
if ( ! assertSuccess ( " TimeUnit::createInstance " , status ) ) return ;
TimeUnit * another = ( TimeUnit * ) tmunit - > clone ( ) ;
TimeUnit third ( * tmunit ) ;
TimeUnit fourth = third ;
assertTrue ( " orig and clone are equal " , ( * tmunit = = * another ) ) ;
assertTrue ( " copied and assigned are equal " , ( third = = fourth ) ) ;
TimeUnit * tmunit_m = TimeUnit : : createInstance ( TimeUnit : : UTIMEUNIT_MONTH , status ) ;
assertTrue ( " year != month " , ( * tmunit ! = * tmunit_m ) ) ;
TimeUnit : : UTimeUnitFields field = tmunit_m - > getTimeUnitField ( ) ;
assertTrue ( " field of month time unit is month " , ( field = = TimeUnit : : UTIMEUNIT_MONTH ) ) ;
2014-02-04 00:29:17 +00:00
2014-03-11 23:33:52 +00:00
//===== Interoperability with MeasureUnit ======
2014-02-04 00:29:17 +00:00
MeasureUnit * * ptrs = new MeasureUnit * [ TimeUnit : : UTIMEUNIT_FIELD_COUNT ] ;
ptrs [ TimeUnit : : UTIMEUNIT_YEAR ] = MeasureUnit : : createYear ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_MONTH ] = MeasureUnit : : createMonth ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_DAY ] = MeasureUnit : : createDay ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_WEEK ] = MeasureUnit : : createWeek ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_HOUR ] = MeasureUnit : : createHour ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_MINUTE ] = MeasureUnit : : createMinute ( status ) ;
ptrs [ TimeUnit : : UTIMEUNIT_SECOND ] = MeasureUnit : : createSecond ( status ) ;
if ( ! assertSuccess ( " TimeUnit::createInstance " , status ) ) return ;
for ( TimeUnit : : UTimeUnitFields j = TimeUnit : : UTIMEUNIT_YEAR ;
j < TimeUnit : : UTIMEUNIT_FIELD_COUNT ;
j = ( TimeUnit : : UTimeUnitFields ) ( j + 1 ) ) {
MeasureUnit * ptr = TimeUnit : : createInstance ( j , status ) ;
if ( ! assertSuccess ( " TimeUnit::createInstance " , status ) ) return ;
2014-03-11 17:40:23 +00:00
// We have to convert *ptr to a MeasureUnit or else == will fail over
// differing types (TimeUnit vs. MeasureUnit).
2014-02-04 00:29:17 +00:00
assertTrue (
" Time unit should be equal to corresponding MeasureUnit " ,
2014-03-11 17:40:23 +00:00
MeasureUnit ( * ptr ) = = * ptrs [ j ] ) ;
2014-02-04 00:29:17 +00:00
delete ptr ;
}
2008-09-19 19:05:37 +00:00
delete tmunit ;
delete another ;
delete tmunit_m ;
2014-02-04 00:29:17 +00:00
for ( int i = 0 ; i < TimeUnit : : UTIMEUNIT_FIELD_COUNT ; + + i ) {
delete ptrs [ i ] ;
}
delete [ ] ptrs ;
2008-09-19 19:05:37 +00:00
//
//================= TimeUnitAmount =================
2008-09-22 22:43:55 +00:00
Formattable formattable ( ( int32_t ) 2 ) ;
2008-09-19 19:05:37 +00:00
TimeUnitAmount tma_long ( formattable , TimeUnit : : UTIMEUNIT_DAY , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount(formattable...) " , status ) ) return ;
formattable . setDouble ( 2 ) ;
TimeUnitAmount tma_double ( formattable , TimeUnit : : UTIMEUNIT_DAY , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount(formattable...) " , status ) ) return ;
formattable . setDouble ( 3 ) ;
TimeUnitAmount tma_double_3 ( formattable , TimeUnit : : UTIMEUNIT_DAY , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount(formattable...) " , status ) ) return ;
TimeUnitAmount tma ( 2 , TimeUnit : : UTIMEUNIT_DAY , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount(number...) " , status ) ) return ;
TimeUnitAmount tma_h ( 2 , TimeUnit : : UTIMEUNIT_HOUR , status ) ;
if ( ! assertSuccess ( " TimeUnitAmount(number...) " , status ) ) return ;
TimeUnitAmount second ( tma ) ;
TimeUnitAmount third_tma = tma ;
TimeUnitAmount * fourth_tma = ( TimeUnitAmount * ) tma . clone ( ) ;
assertTrue ( " orig and copy are equal " , ( second = = tma ) ) ;
assertTrue ( " clone and assigned are equal " , ( third_tma = = * fourth_tma ) ) ;
assertTrue ( " different if number diff " , ( tma_double ! = tma_double_3 ) ) ;
assertTrue ( " different if number type diff " , ( tma_double ! = tma_long ) ) ;
assertTrue ( " different if time unit diff " , ( tma ! = tma_h ) ) ;
assertTrue ( " same even different constructor " , ( tma_double = = tma ) ) ;
assertTrue ( " getTimeUnitField " , ( tma . getTimeUnitField ( ) = = TimeUnit : : UTIMEUNIT_DAY ) ) ;
delete fourth_tma ;
//
//================= TimeUnitFormat =================
//
TimeUnitFormat * tmf_en = new TimeUnitFormat ( Locale ( " en " ) , status ) ;
2009-06-12 19:34:21 +00:00
if ( ! assertSuccess ( " TimeUnitFormat(en...) " , status , TRUE ) ) return ;
2008-09-19 19:05:37 +00:00
TimeUnitFormat tmf_fr ( Locale ( " fr " ) , status ) ;
if ( ! assertSuccess ( " TimeUnitFormat(fr...) " , status ) ) return ;
assertTrue ( " TimeUnitFormat: en and fr diff " , ( * tmf_en ! = tmf_fr ) ) ;
TimeUnitFormat tmf_assign = * tmf_en ;
assertTrue ( " TimeUnitFormat: orig and assign are equal " , ( * tmf_en = = tmf_assign ) ) ;
TimeUnitFormat tmf_copy ( tmf_fr ) ;
assertTrue ( " TimeUnitFormat: orig and copy are equal " , ( tmf_fr = = tmf_copy ) ) ;
TimeUnitFormat * tmf_clone = ( TimeUnitFormat * ) tmf_en - > clone ( ) ;
assertTrue ( " TimeUnitFormat: orig and clone are equal " , ( * tmf_en = = * tmf_clone ) ) ;
delete tmf_clone ;
tmf_en - > setLocale ( Locale ( " fr " ) , status ) ;
if ( ! assertSuccess ( " setLocale(fr...) " , status ) ) return ;
NumberFormat * numberFmt = NumberFormat : : createInstance (
Locale ( " fr " ) , status ) ;
if ( ! assertSuccess ( " NumberFormat::createInstance() " , status ) ) return ;
tmf_en - > setNumberFormat ( * numberFmt , status ) ;
if ( ! assertSuccess ( " setNumberFormat(en...) " , status ) ) return ;
assertTrue ( " TimeUnitFormat: setLocale " , ( * tmf_en = = tmf_fr ) ) ;
delete tmf_en ;
2011-04-28 16:42:58 +00:00
TimeUnitFormat * en_long = new TimeUnitFormat ( Locale ( " en " ) , UTMUTFMT_FULL_STYLE , status ) ;
2008-09-19 19:05:37 +00:00
if ( ! assertSuccess ( " TimeUnitFormat(en...) " , status ) ) return ;
delete en_long ;
2011-04-28 16:42:58 +00:00
TimeUnitFormat * en_short = new TimeUnitFormat ( Locale ( " en " ) , UTMUTFMT_ABBREVIATED_STYLE , status ) ;
2008-09-19 19:05:37 +00:00
if ( ! assertSuccess ( " TimeUnitFormat(en...) " , status ) ) return ;
delete en_short ;
TimeUnitFormat * format = new TimeUnitFormat ( status ) ;
format - > setLocale ( Locale ( " zh " ) , status ) ;
format - > setNumberFormat ( * numberFmt , status ) ;
if ( ! assertSuccess ( " TimeUnitFormat(en...) " , status ) ) return ;
delete numberFmt ;
delete format ;
}
2010-10-19 08:28:02 +00:00
/* @bug 7902
* Tests for Greek Language .
* 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 .
*/
2012-06-08 19:02:03 +00:00
void TimeUnitTest : : testGreekWithFallback ( ) {
2010-10-19 08:28:02 +00:00
UErrorCode status = U_ZERO_ERROR ;
const char * locales [ ] = { " el-GR " , " el " } ;
TimeUnit : : UTimeUnitFields tunits [ ] = { TimeUnit : : UTIMEUNIT_SECOND , TimeUnit : : UTIMEUNIT_MINUTE , TimeUnit : : UTIMEUNIT_HOUR , TimeUnit : : UTIMEUNIT_DAY , TimeUnit : : UTIMEUNIT_MONTH , TimeUnit : : UTIMEUNIT_YEAR } ;
2011-04-28 16:42:58 +00:00
UTimeUnitFormatStyle styles [ ] = { UTMUTFMT_FULL_STYLE , UTMUTFMT_ABBREVIATED_STYLE } ;
2010-10-19 08:28:02 +00:00
const int numbers [ ] = { 1 , 7 } ;
const UChar oneSecond [ ] = { 0x0031 , 0x0020 , 0x03b4 , 0x03b5 , 0x03c5 , 0x03c4 , 0x03b5 , 0x03c1 , 0x03cc , 0x03bb , 0x03b5 , 0x03c0 , 0x03c4 , 0x03bf , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar oneSecondShort [ ] = { 0x0031 , 0x0020 , 0x03b4 , 0x03b5 , 0x03c5 , 0x03c4 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar oneMinute [ ] = { 0x0031 , 0x0020 , 0x03bb , 0x03b5 , 0x03c0 , 0x03c4 , 0x03cc , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar oneMinuteShort [ ] = { 0x0031 , 0x0020 , 0x03bb , 0x03b5 , 0x03c0 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar oneHour [ ] = { 0x0031 , 0x0020 , 0x03ce , 0x03c1 , 0x03b1 , 0 } ;
const UChar oneDay [ ] = { 0x0031 , 0x0020 , 0x03b7 , 0x03bc , 0x03ad , 0x03c1 , 0x03b1 , 0 } ;
const UChar oneMonth [ ] = { 0x0031 , 0x0020 , 0x03bc , 0x03ae , 0x03bd , 0x03b1 , 0x03c2 , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar oneMonthShort [ ] = { 0x0031 , 0x0020 , 0x03bc , 0x03ae , 0x03bd , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar oneYear [ ] = { 0x0031 , 0x0020 , 0x03ad , 0x03c4 , 0x03bf , 0x03c2 , 0 } ;
2014-09-02 23:18:20 +00:00
const UChar oneYearShort [ ] = { 0x0031 , 0x0020 , 0x03ad , 0x03c4 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar sevenSeconds [ ] = { 0x0037 , 0x0020 , 0x03b4 , 0x03b5 , 0x03c5 , 0x03c4 , 0x03b5 , 0x03c1 , 0x03cc , 0x03bb , 0x03b5 , 0x03c0 , 0x03c4 , 0x03b1 , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar sevenSecondsShort [ ] = { 0x0037 , 0x0020 , 0x03b4 , 0x03b5 , 0x03c5 , 0x03c4 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar sevenMinutes [ ] = { 0x0037 , 0x0020 , 0x03bb , 0x03b5 , 0x03c0 , 0x03c4 , 0x03ac , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar sevenMinutesShort [ ] = { 0x0037 , 0x0020 , 0x03bb , 0x03b5 , 0x03c0 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar sevenHours [ ] = { 0x0037 , 0x0020 , 0x03ce , 0x03c1 , 0x03b5 , 0x03c2 , 0 } ;
2014-09-02 23:18:20 +00:00
const UChar sevenHoursShort [ ] = { 0x0037 , 0x0020 , 0x03ce , 0x03c1 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar sevenDays [ ] = { 0x0037 , 0x0020 , 0x03b7 , 0x03bc , 0x03ad , 0x03c1 , 0x03b5 , 0x03c2 , 0 } ;
const UChar sevenMonths [ ] = { 0x0037 , 0x0020 , 0x03bc , 0x03ae , 0x03bd , 0x03b5 , 0x3c2 , 0 } ;
2013-09-07 20:46:42 +00:00
const UChar sevenMonthsShort [ ] = { 0x0037 , 0x0020 , 0x03bc , 0x03ae , 0x03bd , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UChar sevenYears [ ] = { 0x0037 , 0x0020 , 0x03ad , 0x03c4 , 0x03b7 , 0 } ;
2014-09-02 23:18:20 +00:00
const UChar sevenYearsShort [ ] = { 0x0037 , 0x0020 , 0x03ad , 0x03c4 , 0x002e , 0 } ;
2010-10-19 08:28:02 +00:00
const UnicodeString oneSecondStr ( oneSecond ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString oneSecondShortStr ( oneSecondShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString oneMinuteStr ( oneMinute ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString oneMinuteShortStr ( oneMinuteShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString oneHourStr ( oneHour ) ;
const UnicodeString oneDayStr ( oneDay ) ;
const UnicodeString oneMonthStr ( oneMonth ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString oneMonthShortStr ( oneMonthShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString oneYearStr ( oneYear ) ;
2014-09-02 23:18:20 +00:00
const UnicodeString oneYearShortStr ( oneYearShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString sevenSecondsStr ( sevenSeconds ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString sevenSecondsShortStr ( sevenSecondsShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString sevenMinutesStr ( sevenMinutes ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString sevenMinutesShortStr ( sevenMinutesShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString sevenHoursStr ( sevenHours ) ;
2014-09-02 23:18:20 +00:00
const UnicodeString sevenHoursShortStr ( sevenHoursShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString sevenDaysStr ( sevenDays ) ;
const UnicodeString sevenMonthsStr ( sevenMonths ) ;
2013-09-07 20:46:42 +00:00
const UnicodeString sevenMonthsShortStr ( sevenMonthsShort ) ;
2010-10-19 08:28:02 +00:00
const UnicodeString sevenYearsStr ( sevenYears ) ;
2014-09-02 23:18:20 +00:00
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 ,
oneSecondStr , oneMinuteStr , oneHourStr , oneDayStr , oneMonthStr , oneYearStr ,
oneSecondShortStr , oneMinuteShortStr , oneHourStr , oneDayStr , oneMonthShortStr , oneYearShortStr ,
sevenSecondsStr , sevenMinutesStr , sevenHoursStr , sevenDaysStr , sevenMonthsStr , sevenYearsStr ,
sevenSecondsShortStr , sevenMinutesShortStr , sevenHoursShortStr , sevenDaysStr , sevenMonthsShortStr , sevenYearsShortStr } ;
2010-10-19 08:28:02 +00:00
int counter = 0 ;
for ( unsigned int locIndex = 0 ;
2016-02-23 10:40:09 +00:00
locIndex < UPRV_LENGTHOF ( locales ) ;
2010-10-19 08:28:02 +00:00
+ + locIndex ) {
Locale l = Locale : : createFromName ( locales [ locIndex ] ) ;
for ( unsigned int numberIndex = 0 ;
2016-02-24 21:48:56 +00:00
numberIndex < UPRV_LENGTHOF ( numbers ) ;
2010-10-19 08:28:02 +00:00
+ + numberIndex ) {
for ( unsigned int styleIndex = 0 ;
2016-02-23 10:40:09 +00:00
styleIndex < UPRV_LENGTHOF ( styles ) ;
2010-10-19 08:28:02 +00:00
+ + styleIndex ) {
for ( unsigned int unitIndex = 0 ;
2016-02-23 10:40:09 +00:00
unitIndex < UPRV_LENGTHOF ( tunits ) ;
2010-10-19 08:28:02 +00:00
+ + unitIndex ) {
TimeUnitAmount * tamt = new TimeUnitAmount ( numbers [ numberIndex ] , tunits [ unitIndex ] , status ) ;
2010-11-11 05:37:40 +00:00
if ( U_FAILURE ( status ) ) {
dataerrln ( " generating TimeUnitAmount Object failed. " ) ;
2010-10-19 08:28:02 +00:00
# ifdef TUFMTTS_DEBUG
std : : cout < < " Failed to get TimeUnitAmount for " < < tunits [ unitIndex ] < < " \n " ;
# endif
return ;
}
TimeUnitFormat * tfmt = new TimeUnitFormat ( l , styles [ styleIndex ] , status ) ;
2010-11-11 05:37:40 +00:00
if ( U_FAILURE ( status ) ) {
dataerrln ( " generating TimeUnitAmount Object failed. " ) ;
2010-10-19 08:28:02 +00:00
# ifdef TUFMTTS_DEBUG
std : : cout < < " Failed to get TimeUnitFormat for " < < locales [ locIndex ] < < " \n " ;
# endif
return ;
}
Formattable fmt ;
UnicodeString str ;
fmt . adoptObject ( tamt ) ;
str = ( ( Format * ) tfmt ) - > format ( fmt , str , status ) ;
if ( ! assertSuccess ( " formatting relative time failed " , status ) ) {
delete tfmt ;
# ifdef TUFMTTS_DEBUG
std : : cout < < " Failed to format " < < " \n " ;
# endif
return ;
}
# ifdef TUFMTTS_DEBUG
char tmp [ 128 ] ; //output
char tmp1 [ 128 ] ; //expected
int len = 0 ;
u_strToUTF8 ( tmp , 128 , & len , str . getTerminatedBuffer ( ) , str . length ( ) , & status ) ;
u_strToUTF8 ( tmp1 , 128 , & len , expected [ counter ] . unescape ( ) . getTerminatedBuffer ( ) , expected [ counter ] . unescape ( ) . length ( ) , & status ) ;
std : : cout < < " Formatted string : " < < tmp < < " expected : " < < tmp1 < < " \n " ;
# endif
if ( ! assertEquals ( " formatted time string is not expected, locale: " + UnicodeString ( locales [ locIndex ] ) + " style: " + ( int ) styles [ styleIndex ] + " units: " + ( int ) tunits [ unitIndex ] , expected [ counter ] , str ) ) {
delete tfmt ;
str . remove ( ) ;
return ;
}
delete tfmt ;
str . remove ( ) ;
+ + counter ;
}
}
}
}
}
2008-09-19 19:05:37 +00:00
2012-06-08 19:02:03 +00:00
// Test bug9042
void TimeUnitTest : : testGreekWithSanitization ( ) {
UErrorCode status = U_ZERO_ERROR ;
Locale elLoc ( " el " ) ;
NumberFormat * numberFmt = NumberFormat : : createInstance ( Locale ( " el " ) , status ) ;
2012-06-22 16:48:30 +00:00
if ( ! assertSuccess ( " NumberFormat::createInstance for el locale " , status , TRUE ) ) return ;
2012-06-08 19:02:03 +00:00
numberFmt - > setMaximumFractionDigits ( 1 ) ;
TimeUnitFormat * timeUnitFormat = new TimeUnitFormat ( elLoc , status ) ;
if ( ! assertSuccess ( " TimeUnitFormat::TimeUnitFormat for el locale " , status ) ) return ;
timeUnitFormat - > setNumberFormat ( * numberFmt , status ) ;
delete numberFmt ;
delete timeUnitFormat ;
}
2013-10-10 21:52:15 +00:00
void TimeUnitTest : : test10219Plurals ( ) {
Locale usLocale ( " en_US " ) ;
2014-01-16 22:58:52 +00:00
double values [ 2 ] = { 1.588 , 1.011 } ;
UnicodeString expected [ 2 ] [ 3 ] = {
{ " 1 minute " , " 1.5 minutes " , " 1.58 minutes " } ,
{ " 1 minute " , " 1.0 minutes " , " 1.01 minutes " }
} ;
2013-10-10 21:52:15 +00:00
UErrorCode status = U_ZERO_ERROR ;
TimeUnitFormat tuf ( usLocale , status ) ;
if ( U_FAILURE ( status ) ) {
dataerrln ( " generating TimeUnitFormat Object failed: %s " , u_errorName ( status ) ) ;
return ;
}
LocalPointer < DecimalFormat > nf ( ( DecimalFormat * ) NumberFormat : : createInstance ( usLocale , status ) ) ;
if ( U_FAILURE ( status ) ) {
dataerrln ( " generating NumberFormat Object failed: %s " , u_errorName ( status ) ) ;
return ;
}
2014-08-28 22:13:45 +00:00
for ( int32_t j = 0 ; j < UPRV_LENGTHOF ( values ) ; + + j ) {
for ( int32_t i = 0 ; i < UPRV_LENGTHOF ( expected [ j ] ) ; + + i ) {
2014-01-16 22:58:52 +00:00
nf - > setMinimumFractionDigits ( i ) ;
nf - > setMaximumFractionDigits ( i ) ;
nf - > setRoundingMode ( DecimalFormat : : kRoundDown ) ;
tuf . setNumberFormat ( * nf , status ) ;
if ( U_FAILURE ( status ) ) {
dataerrln ( " setting NumberFormat failed: %s " , u_errorName ( status ) ) ;
return ;
}
UnicodeString actual ;
Formattable fmt ;
2014-11-20 00:27:17 +00:00
LocalPointer < TimeUnitAmount > tamt (
new TimeUnitAmount ( values [ j ] , TimeUnit : : UTIMEUNIT_MINUTE , status ) , status ) ;
2014-01-16 22:58:52 +00:00
if ( U_FAILURE ( status ) ) {
dataerrln ( " generating TimeUnitAmount Object failed: %s " , u_errorName ( status ) ) ;
return ;
}
fmt . adoptObject ( tamt . orphan ( ) ) ;
tuf . format ( fmt , actual , status ) ;
if ( U_FAILURE ( status ) ) {
dataerrln ( " Actual formatting failed: %s " , u_errorName ( status ) ) ;
return ;
}
if ( expected [ j ] [ i ] ! = actual ) {
errln ( " Expected " + expected [ j ] [ i ] + " , got " + actual ) ;
}
2013-10-10 21:52:15 +00:00
}
}
// test parsing
Formattable result ;
ParsePosition pos ;
UnicodeString formattedString = " 1 minutes " ;
tuf . parseObject ( formattedString , result , pos ) ;
if ( formattedString . length ( ) ! = pos . getIndex ( ) ) {
errln ( " Expect parsing to go all the way to the end of the string. " ) ;
}
}
2015-11-12 22:09:04 +00:00
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 ) ;
2016-02-01 18:02:21 +00:00
assertEquals ( " TestBritishShortHourFallback() " , UNICODE_STRING_SIMPLE ( " 1 hr " ) , result , TRUE ) ;
2015-11-12 22:09:04 +00:00
}
2008-09-19 19:05:37 +00:00
# endif