ICU-4094 Add API/Method Coverage tests for ICU4J

X-SVN-Rev: 16670
This commit is contained in:
GCL Shanghai 2004-10-29 14:23:51 +00:00
parent 8bcd6c39e7
commit b975d6f016
4 changed files with 197 additions and 6 deletions

View file

@ -250,6 +250,34 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
", expected " + DATA[i+3]);
}
}
// format currency with CurrencyAmount
for (int i=0; i<DATA.length; i+=4) {
Locale locale = new Locale(DATA[i], DATA[i+1], DATA[i+2]);
Currency curr = Currency.getInstance(locale);
logln("\nName of the currency is: " + curr.getName(locale, Currency.LONG_NAME, new boolean[] {false}));
CurrencyAmount cAmt = new CurrencyAmount(1.5, curr);
logln("CurrencyAmount object's hashCode is: " + cAmt.hashCode()); //cover hashCode
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
String sCurr = fmt.format(cAmt);
if (sCurr.equals(DATA[i+3])) {
logln("Ok: 1.50 x " + locale + " => " + sCurr);
} else {
errln("FAIL: 1.50 x " + locale + " => " + sCurr +
", expected " + DATA[i+3]);
}
}
//Cover MeasureFormat.getCurrencyFormat()
ULocale save = ULocale.getDefault();
ULocale.setDefault(ULocale.US);
MeasureFormat curFmt = MeasureFormat.getCurrencyFormat();
//Not knowing how to test this factory method,
// because the concreat class of MeasureFormat -
// CurrencyFormat is just for internal use.
ULocale.setDefault(save);
}
/**
@ -483,6 +511,8 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
//when the pattern problem is finalized, delete comment mark'//'
//of the following code
expect2(NumberFormat.getScientificInstance(Locale.US), 12345.678901, "1.2345678901E4");
logln("Testing NumberFormat.getScientificInstance(ULocale) ...");
expect2(NumberFormat.getScientificInstance(ULocale.US), 12345.678901, "1.2345678901E4");
expect(new DecimalFormat("##0.###E0", US), 12345.0, "12.34E3");
expect(new DecimalFormat("##0.###E0", US), 12345.00001, "12.35E3");
@ -491,6 +521,8 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
// pattern of NumberFormat.getScientificInstance(Locale.US) = "0.######E0" not "#E0"
// so result = 1.234568E4 not 1.2345678901E4
expect2(NumberFormat.getScientificInstance(Locale.FRANCE), 12345.678901, "1,2345678901E4");
logln("Testing NumberFormat.getScientificInstance(ULocale) ...");
expect2(NumberFormat.getScientificInstance(ULocale.FRANCE), 12345.678901, "1,2345678901E4");
expect(new DecimalFormat("##0.####E0", US), 789.12345e-9, "789.12E-9");
expect2(new DecimalFormat("##0.####E0", US), 780.e-9, "780E-9");
@ -869,12 +901,12 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
}
public void TestCurrencyKeyword() {
ULocale locale = new ULocale("th_TH@currency=QQQ");
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
String result = format.format(12.34f);
if (!"QQQ12.34".equals(result)) {
errln("got unexpected currency: " + result);
}
ULocale locale = new ULocale("th_TH@currency=QQQ");
NumberFormat format = NumberFormat.getCurrencyInstance(locale);
String result = format.format(12.34f);
if (!"QQQ12.34".equals(result)) {
errln("got unexpected currency: " + result);
}
}
public void TestThreadedFormat() {

View file

@ -8,6 +8,7 @@ package com.ibm.icu.dev.test.format;
import com.ibm.icu.text.RuleBasedNumberFormat;
import com.ibm.icu.dev.test.TestFmwk;
import com.ibm.icu.util.ULocale;
import java.math.BigInteger;
import java.math.BigDecimal;
@ -813,6 +814,68 @@ public class RbnfTest extends TestFmwk {
doTest(formatter, testData, true);
}
public void TestRuleSetDisplayName() {
ULocale.setDefault(ULocale.US);
String[][] localizations = new String[][] {
/* public rule sets*/
{"%simplified", "%default", "%ordinal"},
/* display names in "en_US" locale*/
{"en_US", "Simplified", "Default", "Ordinal"},
/* display names in "zh_Hans" locale*/
{"zh_Hans", "\u7B80\u5316", "\u7F3A\u7701", "\u5E8F\u5217"},
/* display names in a fake locale*/
{"foo_Bar_BAZ", "Simplified", "Default", "Ordinal"}
};
//Construct RuleBasedNumberFormat by rule sets and localizations list
RuleBasedNumberFormat formatter
= new RuleBasedNumberFormat(ukEnglish, localizations, ULocale.US);
RuleBasedNumberFormat f2= new RuleBasedNumberFormat(ukEnglish, localizations);
assertTrue("Check the two formatters' equality", formatter.equals(f2));
//get displayName by name
String[] ruleSetNames = formatter.getRuleSetNames();
for (int i=0; i<ruleSetNames.length; i++) {
logln("Rule set name: " + ruleSetNames[i]);
String RSName_defLoc = formatter.getRuleSetDisplayName(ruleSetNames[i]);
assertEquals("Display name in default locale", localizations[1][i+1], RSName_defLoc);
String RSName_loc = formatter.getRuleSetDisplayName(ruleSetNames[i], ULocale.CHINA);
assertEquals("Display name in Chinese", localizations[2][i+1], RSName_loc);
}
// getDefaultRuleSetName
String defaultRS = formatter.getDefaultRuleSetName();
//you know that the default rule set is %simplified according to rule sets string ukEnglish
assertEquals("getDefaultRuleSetName", "%simplified", defaultRS);
//get locales of localizations
ULocale[] locales = formatter.getRuleSetDisplayNameLocales();
for (int i=0; i<locales.length; i++) {
logln(locales[i].getName());
}
//get displayNames
String[] RSNames_defLoc = formatter.getRuleSetDisplayNames();
for (int i=0; i<RSNames_defLoc.length; i++) {
assertEquals("getRuleSetDisplayNames in default locale", localizations[1][i+1], RSNames_defLoc[i]);
}
String[] RSNames_loc = formatter.getRuleSetDisplayNames(ULocale.UK);
for (int i=0; i<RSNames_loc.length; i++) {
assertEquals("getRuleSetDisplayNames in English", localizations[1][i+1], RSNames_loc[i]);
}
RSNames_loc = formatter.getRuleSetDisplayNames(ULocale.CHINA);
for (int i=0; i<RSNames_loc.length; i++) {
assertEquals("getRuleSetDisplayNames in Chinese", localizations[2][i+1], RSNames_loc[i]);
}
RSNames_loc = formatter.getRuleSetDisplayNames(new ULocale("foo_Bar_BAZ"));
for (int i=0; i<RSNames_loc.length; i++) {
assertEquals("getRuleSetDisplayNames in fake locale", localizations[3][i+1], RSNames_loc[i]);
}
}
void doTest(RuleBasedNumberFormat formatter, String[][] testData,
boolean testParsing) {
// NumberFormat decFmt = NumberFormat.getInstance(Locale.US);
@ -870,5 +933,85 @@ public class RbnfTest extends TestFmwk {
e.printStackTrace();
}
}
/**
* Spellout rules for U.K. English.
* I borrow the rule sets for TestRuleSetDisplayName()
*/
public static final String ukEnglish =
"%simplified:\n"
+ " -x: minus >>;\n"
+ " x.x: << point >>;\n"
+ " zero; one; two; three; four; five; six; seven; eight; nine;\n"
+ " ten; eleven; twelve; thirteen; fourteen; fifteen; sixteen;\n"
+ " seventeen; eighteen; nineteen;\n"
+ " 20: twenty[->>];\n"
+ " 30: thirty[->>];\n"
+ " 40: forty[->>];\n"
+ " 50: fifty[->>];\n"
+ " 60: sixty[->>];\n"
+ " 70: seventy[->>];\n"
+ " 80: eighty[->>];\n"
+ " 90: ninety[->>];\n"
+ " 100: << hundred[ >>];\n"
+ " 1000: << thousand[ >>];\n"
+ " 1,000,000: << million[ >>];\n"
+ " 1,000,000,000,000: << billion[ >>];\n"
+ " 1,000,000,000,000,000: =#,##0=;\n"
+ "%alt-teens:\n"
+ " =%simplified=;\n"
+ " 1000>: <%%alt-hundreds<[ >>];\n"
+ " 10,000: =%simplified=;\n"
+ " 1,000,000: << million[ >%simplified>];\n"
+ " 1,000,000,000,000: << billion[ >%simplified>];\n"
+ " 1,000,000,000,000,000: =#,##0=;\n"
+ "%%alt-hundreds:\n"
+ " 0: SHOULD NEVER GET HERE!;\n"
+ " 10: <%simplified< thousand;\n"
+ " 11: =%simplified= hundred>%%empty>;\n"
+ "%%empty:\n"
+ " 0:;"
+ "%ordinal:\n"
+ " zeroth; first; second; third; fourth; fifth; sixth; seventh;\n"
+ " eighth; ninth;\n"
+ " tenth; eleventh; twelfth; thirteenth; fourteenth;\n"
+ " fifteenth; sixteenth; seventeenth; eighteenth;\n"
+ " nineteenth;\n"
+ " twentieth; twenty->>;\n"
+ " 30: thirtieth; thirty->>;\n"
+ " 40: fortieth; forty->>;\n"
+ " 50: fiftieth; fifty->>;\n"
+ " 60: sixtieth; sixty->>;\n"
+ " 70: seventieth; seventy->>;\n"
+ " 80: eightieth; eighty->>;\n"
+ " 90: ninetieth; ninety->>;\n"
+ " 100: <%simplified< hundredth; <%simplified< hundred >>;\n"
+ " 1000: <%simplified< thousandth; <%simplified< thousand >>;\n"
+ " 1,000,000: <%simplified< millionth; <%simplified< million >>;\n"
+ " 1,000,000,000,000: <%simplified< billionth;\n"
+ " <%simplified< billion >>;\n"
+ " 1,000,000,000,000,000: =#,##0=;"
+ "%default:\n"
+ " -x: minus >>;\n"
+ " x.x: << point >>;\n"
+ " =%simplified=;\n"
+ " 100: << hundred[ >%%and>];\n"
+ " 1000: << thousand[ >%%and>];\n"
+ " 100,000>>: << thousand[>%%commas>];\n"
+ " 1,000,000: << million[>%%commas>];\n"
+ " 1,000,000,000,000: << billion[>%%commas>];\n"
+ " 1,000,000,000,000,000: =#,##0=;\n"
+ "%%and:\n"
+ " and =%default=;\n"
+ " 100: =%default=;\n"
+ "%%commas:\n"
+ " ' and =%default=;\n"
+ " 100: , =%default=;\n"
+ " 1000: , <%default< thousand, >%default>;\n"
+ " 1,000,000: , =%default=;"
+ "%%lenient-parse:\n"
+ " & ' ' , ',' ;\n";
}

View file

@ -1337,6 +1337,13 @@ lineSelectionData.addElement("(\u0e1b\u0e23\u0e30\u0e40\u0e17\u0e28\u0e44\u0e17\
if (locList.length == 0)
errln("getAvailableLocales() returned an empty list!");
// I have no idea how to test this function...
com.ibm.icu.util.ULocale[] ulocList = BreakIterator.getAvailableULocales();
if (ulocList.length == 0) {
errln("getAvailableULocales() returned an empty list!");
} else {
logln("getAvailableULocales() returned " + ulocList.length + " locales");
}
}
/**

View file

@ -11,6 +11,7 @@ import com.ibm.icu.dev.test.*;
import com.ibm.icu.impl.Utility;
import com.ibm.icu.impl.UtilityExtensions;
import com.ibm.icu.util.CaseInsensitiveString;
import com.ibm.icu.util.ULocale;
import java.util.*;
/***********************************************************************
@ -2360,6 +2361,14 @@ public class TransliteratorTest extends TestFmwk {
} else {
logln("Ok: " + t.getID() + ".getDisplayName() => " + name);
}
// Cover getDisplayName(String)
ULocale save = ULocale.getDefault();
ULocale.setDefault(ULocale.US);
String name2 = Transliterator.getDisplayName(t.getID());
if (!name.equals(name2))
errln("FAIL: getDisplayName with default locale failed");
ULocale.setDefault(save);
}
}