use of android.icu.text.NumberFormat in project j2objc by google.
the class PluralFormatUnitTest method TestSetLocale.
@Test
public void TestSetLocale() {
// Create rules for testing.
PluralRules oddAndEven = PluralRules.createRules("odd__: n mod 2 is 1");
PluralFormat plFmt = new PluralFormat(oddAndEven);
plFmt.applyPattern("odd__{odd} other{even}");
plFmt.setLocale(ULocale.ENGLISH);
// Check that pattern gets deleted.
NumberFormat nrFmt = NumberFormat.getInstance(ULocale.ENGLISH);
assertEquals("pattern was not resetted by setLocale() call.", nrFmt.format(5), plFmt.format(5));
// Check that rules got updated.
plFmt.applyPattern("odd__{odd} other{even}");
assertEquals("SetLocale should reset rules but did not.", "even", plFmt.format(1));
plFmt.applyPattern("one{one} other{not one}");
for (int i = 0; i < 20; ++i) {
assertEquals("Wrong ruleset loaded by setLocale()", ((i == 1) ? "one" : "not one"), plFmt.format(i));
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class DateFormatTest method TestCoverage.
@Test
public void TestCoverage() {
Date now = new Date();
Calendar cal = new GregorianCalendar();
DateFormat f = DateFormat.getTimeInstance();
logln("time: " + f.format(now));
// sigh, everyone overrides this
int hash = f.hashCode();
f = DateFormat.getInstance(cal);
if (hash == f.hashCode()) {
errln("FAIL: hashCode equal for inequal objects");
}
logln("time again: " + f.format(now));
f = DateFormat.getTimeInstance(cal, DateFormat.FULL);
logln("time yet again: " + f.format(now));
f = DateFormat.getDateInstance();
logln("time yet again: " + f.format(now));
ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "de_DE");
DateFormatSymbols sym = new DateFormatSymbols(rb, Locale.GERMANY);
DateFormatSymbols sym2 = (DateFormatSymbols) sym.clone();
if (sym.hashCode() != sym2.hashCode()) {
errln("fail, date format symbols hashcode not equal");
}
if (!sym.equals(sym2)) {
errln("fail, date format symbols not equal");
}
Locale foo = new Locale("fu", "FU", "BAR");
rb = null;
sym = new DateFormatSymbols(GregorianCalendar.class, foo);
sym.equals(null);
sym = new ChineseDateFormatSymbols();
sym = new ChineseDateFormatSymbols(new Locale("en_US"));
try {
sym = new ChineseDateFormatSymbols(null, new Locale("en_US"));
errln("ChineseDateFormatSymbols(Calender, Locale) was suppose to return a null " + "pointer exception for a null paramater.");
} catch (Exception e) {
}
sym = new ChineseDateFormatSymbols(new ChineseCalendar(), new Locale("en_US"));
try {
sym = new ChineseDateFormatSymbols(null, new ULocale("en_US"));
errln("ChineseDateFormatSymbols(Calender, ULocale) was suppose to return a null " + "pointer exception for a null paramater.");
} catch (Exception e) {
}
sym = new ChineseDateFormatSymbols(new ChineseCalendar(), foo);
// cover new ChineseDateFormatSymbols(Calendar, ULocale)
ChineseCalendar ccal = new ChineseCalendar();
// gclsh1 add
sym = new ChineseDateFormatSymbols(ccal, ULocale.CHINA);
StringBuffer buf = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
f.format((Object) cal, buf, pos);
f.format((Object) now, buf, pos);
f.format((Object) new Long(now.getTime()), buf, pos);
try {
f.format((Object) "Howdy", buf, pos);
} catch (Exception e) {
}
NumberFormat nf = f.getNumberFormat();
f.setNumberFormat(nf);
boolean lenient = f.isLenient();
f.setLenient(lenient);
ULocale uloc = f.getLocale(ULocale.ACTUAL_LOCALE);
DateFormat sdfmt = new SimpleDateFormat();
if (f.hashCode() != f.hashCode()) {
errln("hashCode is not stable");
}
if (!f.equals(f)) {
errln("f != f");
}
if (f.equals(null)) {
errln("f should not equal null");
}
if (f.equals(sdfmt)) {
errln("A time instance shouldn't equal a default date format");
}
Date d;
{
ChineseDateFormat fmt = new ChineseDateFormat("yymm", Locale.US);
try {
// fewer symbols than required 2
fmt.parse("2");
errln("whoops");
} catch (ParseException e) {
logln("ok");
}
try {
// should succeed with obeycount
fmt.parse("2255");
logln("ok");
} catch (ParseException e) {
errln("whoops");
}
try {
// not a number, should fail
fmt.parse("ni hao");
errln("whoops ni hao");
} catch (ParseException e) {
logln("ok ni hao");
}
}
{
Calendar xcal = new GregorianCalendar();
xcal.set(Calendar.HOUR_OF_DAY, 0);
DateFormat fmt = new SimpleDateFormat("k");
StringBuffer xbuf = new StringBuffer();
FieldPosition fpos = new FieldPosition(Calendar.HOUR_OF_DAY);
fmt.format(xcal, xbuf, fpos);
try {
fmt.parse(xbuf.toString());
logln("ok");
xbuf.setLength(0);
xcal.set(Calendar.HOUR_OF_DAY, 25);
fmt.format(xcal, xbuf, fpos);
Date d2 = fmt.parse(xbuf.toString());
logln("ok again - d2=" + d2);
} catch (ParseException e) {
errln("whoops");
}
}
{
// cover gmt+hh:mm
DateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
try {
d = fmt.parse("07/10/53 GMT+10:00");
logln("ok : d = " + d);
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+10:00 for pattern MM/dd/yy z");
}
// cover invalid separator after GMT
{
ParsePosition pp = new ParsePosition(0);
String text = "07/10/53 GMT=10:00";
d = fmt.parse(text, pp);
if (pp.getIndex() != 12) {
errln("Parse of 07/10/53 GMT=10:00 for pattern MM/dd/yy z");
}
logln("Parsing of the text stopped at pos: " + pp.getIndex() + " as expected and length is " + text.length());
}
// cover bad text after GMT+.
try {
fmt.parse("07/10/53 GMT+blecch");
logln("ok GMT+blecch");
} catch (ParseException e) {
errln("whoops GMT+blecch");
}
// cover bad text after GMT+hh:.
try {
fmt.parse("07/10/53 GMT+07:blecch");
logln("ok GMT+xx:blecch");
} catch (ParseException e) {
errln("whoops GMT+xx:blecch");
}
// cover no ':' GMT+#, # < 24 (hh)
try {
d = fmt.parse("07/10/53 GMT+07");
logln("ok GMT+07");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # > 24 (hhmm)
try {
d = fmt.parse("07/10/53 GMT+0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+0730 for pattern MM/dd/yy z");
}
// cover GMT+#, # with second field
try {
d = fmt.parse("07/10/53 GMT+07:30:15");
logln("ok GMT+07:30:15");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07:30:15 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with second field, no leading zero
try {
d = fmt.parse("07/10/53 GMT+73015");
logln("ok GMT+73015");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+73015 for pattern MM/dd/yy z");
}
// cover no ':' GMT+#, # with 1 digit second field
try {
d = fmt.parse("07/10/53 GMT+07300");
logln("ok GMT+07300");
} catch (ParseException e) {
errln("Parse of 07/10/53 GMT+07300 for pattern MM/dd/yy z");
}
// cover raw digits with no leading sign (bad RFC822)
try {
d = fmt.parse("07/10/53 07");
errln("Parse of 07/10/53 07 for pattern MM/dd/yy z passed!");
} catch (ParseException e) {
logln("ok");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 +07");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 +07 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822)
try {
d = fmt.parse("07/10/53 -0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 -00730 for pattern MM/dd/yy z failed");
}
// cover raw digits (RFC822) in DST
try {
fmt.setTimeZone(TimeZone.getTimeZone("PDT"));
d = fmt.parse("07/10/53 -0730");
logln("ok");
} catch (ParseException e) {
errln("Parse of 07/10/53 -0730 for pattern MM/dd/yy z failed");
}
}
// TODO: revisit toLocalizedPattern
if (false) {
SimpleDateFormat fmt = new SimpleDateFormat("aabbcc");
try {
String pat = fmt.toLocalizedPattern();
errln("whoops, shouldn't have been able to localize aabbcc");
} catch (IllegalArgumentException e) {
logln("aabbcc localize ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("'aabbcc");
try {
fmt.toLocalizedPattern();
errln("whoops, localize unclosed quote");
} catch (IllegalArgumentException e) {
logln("localize unclosed quote ok");
}
}
{
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// bogus time zone
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("recognized bogus time zone DBDY");
} catch (ParseException e) {
logln("time zone ex ok");
}
}
{
// force fallback to default timezone when fmt timezone
// is not named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// force fallback to default time zone, still fails
// not in equivalency group
fmt.setTimeZone(TimeZone.getTimeZone("GMT+0147"));
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
} catch (ParseException e) {
logln("time zone ex2 ok");
}
// force success on fallback
text = "08/15/58 " + TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
try {
fmt.parse(text);
logln("found default tz");
} catch (ParseException e) {
errln("whoops, got parse exception");
}
}
{
// force fallback to symbols list of timezones when neither
// fmt and default timezone is named
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
TimeZone oldtz = TimeZone.getDefault();
// nonstandard tz
TimeZone newtz = TimeZone.getTimeZone("GMT+0137");
fmt.setTimeZone(newtz);
// todo: fix security issue
TimeZone.setDefault(newtz);
// fallback to symbol list, but fail
// try to parse the bogus time zone
String text = "08/15/58 DBDY";
try {
fmt.parse(text);
errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
} catch (ParseException e) {
logln("time zone ex3 ok");
} catch (Exception e) {
// hmmm... this shouldn't happen. don't want to exit this
// fn with timezone improperly set, so just in case
TimeZone.setDefault(oldtz);
throw new IllegalStateException(e.getMessage());
}
}
{
// cover getAvailableULocales
final ULocale[] locales = DateFormat.getAvailableULocales();
long count = locales.length;
if (count == 0) {
errln(" got a empty list for getAvailableULocales");
} else {
logln("" + count + " available ulocales");
}
}
{
// cover DateFormatSymbols.getDateFormatBundle
cal = new GregorianCalendar();
Locale loc = Locale.getDefault();
DateFormatSymbols mysym = new DateFormatSymbols(cal, loc);
if (mysym == null)
errln("FAIL: constructs DateFormatSymbols with calendar and locale failed");
uloc = ULocale.getDefault();
// These APIs are obsolete and return null
ResourceBundle resb = DateFormatSymbols.getDateFormatBundle(cal, loc);
ResourceBundle resb2 = DateFormatSymbols.getDateFormatBundle(cal, uloc);
ResourceBundle resb3 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), loc);
ResourceBundle resb4 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), uloc);
if (resb != null) {
logln("resb is not null");
}
if (resb2 != null) {
logln("resb2 is not null");
}
if (resb3 != null) {
logln("resb3 is not null");
}
if (resb4 != null) {
logln("resb4 is not null");
}
}
{
// cover DateFormatSymbols.getInstance
DateFormatSymbols datsym1 = DateFormatSymbols.getInstance();
DateFormatSymbols datsym2 = new DateFormatSymbols();
if (!datsym1.equals(datsym2)) {
errln("FAIL: DateFormatSymbols returned by getInstance()" + "does not match new DateFormatSymbols().");
}
datsym1 = DateFormatSymbols.getInstance(Locale.JAPAN);
datsym2 = DateFormatSymbols.getInstance(ULocale.JAPAN);
if (!datsym1.equals(datsym2)) {
errln("FAIL: DateFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
}
}
{
// cover DateFormatSymbols.getAvailableLocales/getAvailableULocales
Locale[] allLocales = DateFormatSymbols.getAvailableLocales();
if (allLocales.length == 0) {
errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allLocales.length + " available locales returned by DateFormatSymbols.getAvailableLocales");
}
ULocale[] allULocales = DateFormatSymbols.getAvailableULocales();
if (allULocales.length == 0) {
errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
} else {
logln("PASS: " + allULocales.length + " available locales returned by DateFormatSymbols.getAvailableULocales");
}
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class DateFormatTest method TestOverrideNumberForamt.
@Test
public void TestOverrideNumberForamt() {
SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
// test override get/set NumberFormat
for (int i = 0; i < 100; i++) {
NumberFormat check_nf = NumberFormat.getInstance(new ULocale("en_US"));
fmt.setNumberFormat("y", check_nf);
NumberFormat get_nf = fmt.getNumberFormat('y');
if (!get_nf.equals(check_nf))
errln("FAIL: getter and setter do not work");
}
NumberFormat reused_nf = NumberFormat.getInstance(new ULocale("en_US"));
fmt.setNumberFormat("y", reused_nf);
// test the same override NF will not crash
fmt.setNumberFormat(reused_nf);
// DATA[i][0] is to tell which field to set, DATA[i][1] is the expected result
String[][] DATA = { { "", "\u521D\u516D \u5341\u4E94" }, { "M", "\u521D\u516D 15" }, { "Mo", "\u521D\u516D \u5341\u4E94" }, { "Md", "\u521D\u516D \u5341\u4E94" }, { "MdMMd", "\u521D\u516D \u5341\u4E94" }, { "mixed", "\u521D\u516D \u5341\u4E94" } };
NumberFormat override = NumberFormat.getInstance(new ULocale("en@numbers=hanidays"));
Calendar cal = Calendar.getInstance();
cal.set(1997, Calendar.JUNE, 15);
Date test_date = cal.getTime();
for (int i = 0; i < DATA.length; i++) {
fmt = new SimpleDateFormat("MM d", new ULocale("en_US"));
String field = DATA[i][0];
if (field == "") {
// use the one w/o field
fmt.setNumberFormat(override);
} else if (field == "mixed") {
// set 1 field at first but then full override, both(M & d) should be override
NumberFormat single_override = NumberFormat.getInstance(new ULocale("en@numbers=hebr"));
fmt.setNumberFormat("M", single_override);
fmt.setNumberFormat(override);
} else if (field == "Mo") {
// o is invalid field
try {
fmt.setNumberFormat(field, override);
} catch (IllegalArgumentException e) {
logln("IllegalArgumentException is thrown for invalid fields");
continue;
}
} else {
fmt.setNumberFormat(field, override);
}
String result = fmt.format(test_date);
String expected = DATA[i][1];
if (!result.equals(expected))
errln((String) "FAIL: -> " + result.toString() + " expected -> " + expected);
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class GlobalizationPreferencesTest method TestDefault.
@Test
public void TestDefault() {
GlobalizationPreferences gp = new GlobalizationPreferences();
ULocale defLocale = new ULocale("en_US");
ULocale defFallbackLocale = new ULocale("en");
if (!defLocale.equals(ULocale.getDefault())) {
// Locale.US is always used as the default locale in the test environment
// If not, some test cases will fail...
errln("FAIL: The default locale of the test environment must be en_US");
}
logln("Default locale: " + defLocale.toString());
// First locale is en_US
ULocale gpLocale0 = gp.getLocale(0);
logln("Primary locale: " + gpLocale0.toString());
if (!gpLocale0.equals(defLocale)) {
errln("FAIL: The primary locale is not en_US");
}
// Second locale is en
ULocale gpLocale1 = gp.getLocale(1);
logln("Secondary locale: " + gpLocale1.toString());
if (!gpLocale1.equals(defFallbackLocale)) {
errln("FAIL: The secondary locale is not en");
}
// Third locale is null
ULocale gpLocale2 = gp.getLocale(2);
if (gpLocale2 != null) {
errln("FAIL: Number of locales must be 2");
}
// Calendar locale
Calendar cal = gp.getCalendar();
ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE);
logln("Calendar locale: " + calLocale.toString());
if (!calLocale.equals(defLocale)) {
errln("FAIL: The calendar locale must match with the default JVM locale");
}
// Collator locale
Collator coll = gp.getCollator();
ULocale collLocale = coll.getLocale(ULocale.VALID_LOCALE);
logln("Collator locale: " + collLocale.toString());
if (!collLocale.equals(defLocale)) {
errln("FAIL: The collator locale must match with the default JVM locale");
}
// BreakIterator locale
BreakIterator brk = gp.getBreakIterator(GlobalizationPreferences.BI_CHARACTER);
ULocale brkLocale = brk.getLocale(ULocale.VALID_LOCALE);
logln("BreakIterator locale: " + brkLocale.toString());
if (!brkLocale.equals(defLocale)) {
errln("FAIL: The break iterator locale must match with the default JVM locale");
}
/* Skip - Bug#5209
// DateFormat locale
DateFormat df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
ULocale dfLocale = df.getLocale(ULocale.VALID_LOCALE);
logln("DateFormat locale: " + dfLocale.toString());
if (!dfLocale.equals(defLocale)) {
errln("FAIL: The date format locale must match with the default JVM locale");
}
*/
// NumberFormat locale
NumberFormat nf = gp.getNumberFormat(GlobalizationPreferences.NF_NUMBER);
ULocale nfLocale = nf.getLocale(ULocale.VALID_LOCALE);
logln("NumberFormat locale: " + nfLocale.toString());
if (!nfLocale.equals(defLocale)) {
errln("FAIL: The number format locale must match with the default JVM locale");
}
}
use of android.icu.text.NumberFormat in project j2objc by google.
the class NumberFormatRegressionTest method Test4161100.
/**
* alphaWorks upgrade
*/
@Test
public void Test4161100() {
NumberFormat nf = NumberFormat.getInstance(Locale.US);
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
double a = -0.09;
String s = nf.format(a);
logln(a + " x " + ((DecimalFormat) nf).toPattern() + " = " + s);
if (!s.equals("-0.1")) {
errln("FAIL");
}
}
Aggregations