blob: 980c8c0b23c5c487f4d644af02b626e11c4e9302 [file] [log] [blame]
/*
*******************************************************************************
* Copyright (C) 2008, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.dev.test.format;
import java.text.ParseException;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.TimeUnitFormat;
import com.ibm.icu.util.TimeUnit;
import com.ibm.icu.util.TimeUnitAmount;
import com.ibm.icu.util.ULocale;
/**
* @author markdavis
*
*/
public class TimeUnitTest extends TestFmwk {
public static void main(String[] args) throws Exception{
new TimeUnitTest().run(args);
}
public void TestBasic() {
String[] locales = {"en", "sl", "fr", "zh", "ar", "ru", "zh_Hant"};
for ( int locIndex = 0; locIndex < locales.length; ++locIndex ) {
//System.out.println("locale: " + locales[locIndex]);
Object[] formats = new Object[] {
new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME),
new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME)
};
for (int style = TimeUnitFormat.FULL_NAME;
style <= TimeUnitFormat.ABBREVIATED_NAME;
++style) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = {0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35};
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = ((TimeUnitFormat)formats[style]).format(source);
//System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) ((TimeUnitFormat)formats[style]).parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
// mix style parsing
result = (TimeUnitAmount) ((TimeUnitFormat)formats[1 - style]).parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
}
}
public void TestAPI() {
TimeUnitFormat format = new TimeUnitFormat();
format.setLocale(new ULocale("pt_BR"));
formatParsing(format);
format = new TimeUnitFormat(new ULocale("de"));
formatParsing(format);
format = new TimeUnitFormat(new ULocale("ja"));
format.setNumberFormat(NumberFormat.getNumberInstance(new ULocale("en")));
formatParsing(format);
format = new TimeUnitFormat();
ULocale es = new ULocale("es");
format.setNumberFormat(NumberFormat.getNumberInstance(es));
format.setLocale(es);
formatParsing(format);
}
private void formatParsing(TimeUnitFormat format) {
final TimeUnit[] values = TimeUnit.values();
for (int j = 0; j < values.length; ++j) {
final TimeUnit timeUnit = values[j];
double[] tests = {0, 0.5, 1, 2, 3, 5};
for (int i = 0; i < tests.length; ++i) {
TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
String formatted = format.format(source);
//System.out.println(formatted);
logln(tests[i] + " => " + formatted);
try {
TimeUnitAmount result = (TimeUnitAmount) format.parseObject(formatted);
if (result == null || !source.equals(result)) {
errln("No round trip: " + source + " => " + formatted + " => " + result);
}
} catch (ParseException e) {
errln(e.getMessage());
}
}
}
}
}