001: /*
002: *******************************************************************************
003: * Copyright (C) 2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.tests;
009:
010: import java.math.BigInteger;
011: import java.text.FieldPosition;
012: import java.text.ParseException;
013: import java.text.ParsePosition;
014: import java.util.Locale;
015:
016: import com.ibm.icu.text.NumberFormat;
017: import com.ibm.icu.util.ULocale;
018:
019: public class NumberFormatTest extends ICUTestCase {
020:
021: /*
022: * Test method for 'com.ibm.icu.x.text.NumberFormat.NumberFormat(NumberFormat)'
023: */
024: public void testNumberFormat() {
025: NumberFormat nf = new NumberFormat(java.text.NumberFormat
026: .getInstance());
027: assertEquals(nf, NumberFormat.getInstance());
028: }
029:
030: /*
031: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(Object, StringBuffer, FieldPosition)'
032: */
033: public void testFormatObjectStringBufferFieldPosition() {
034: Number num = new Long(1234L);
035: StringBuffer buf = new StringBuffer();
036: FieldPosition fp = new FieldPosition(0);
037: NumberFormat.getInstance().format(num, buf, fp);
038: assertEquals("1,234", buf.toString());
039: }
040:
041: /*
042: * Test method for 'com.ibm.icu.x.text.NumberFormat.parseObject(String, ParsePosition)'
043: */
044: public void testParseObjectStringParsePosition() {
045: ParsePosition pp = new ParsePosition(0);
046: Object result = NumberFormat.getInstance().parse("1,234", pp);
047: assertEquals(result, new Long(1234));
048: }
049:
050: /*
051: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(double)'
052: */
053: public void testFormatDouble() {
054: assertEquals("1,234.567", NumberFormat.getInstance().format(
055: 1234.567));
056: }
057:
058: /*
059: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(long)'
060: */
061: public void testFormatLong() {
062: assertEquals("1,234", NumberFormat.getInstance().format(1234L));
063: }
064:
065: /*
066: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(BigInteger)'
067: */
068: public void testFormatBigInteger() {
069: // note, java doesn't handle biginteger with full precision.
070: BigInteger bi = new BigInteger("123456");
071: assertEquals("123,456", java.text.NumberFormat.getInstance()
072: .format(bi));
073: }
074:
075: /*
076: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(double, StringBuffer, FieldPosition)'
077: */
078: public void testFormatDoubleStringBufferFieldPosition() {
079: StringBuffer buf = new StringBuffer();
080: FieldPosition fp = new FieldPosition(0);
081: assertEquals("123,456.789", NumberFormat.getInstance().format(
082: 123456.789, buf, fp).toString());
083: }
084:
085: /*
086: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(long, StringBuffer, FieldPosition)'
087: */
088: public void testFormatLongStringBufferFieldPosition() {
089: StringBuffer buf = new StringBuffer();
090: FieldPosition fp = new FieldPosition(0);
091: assertEquals("123,456", NumberFormat.getInstance().format(
092: 123456L, buf, fp).toString());
093: }
094:
095: /*
096: * Test method for 'com.ibm.icu.x.text.NumberFormat.format(BigInteger, StringBuffer, FieldPosition)'
097: */
098: public void testFormatBigIntegerStringBufferFieldPosition() {
099: // note, java doesn't handle biginteger with full precision.
100: StringBuffer buf = new StringBuffer();
101: FieldPosition fp = new FieldPosition(0);
102: BigInteger bi = new BigInteger("123456");
103: assertEquals("123,456", java.text.NumberFormat.getInstance()
104: .format(bi, buf, fp).toString());
105: }
106:
107: /*
108: * Test method for 'com.ibm.icu.x.text.NumberFormat.parse(String, ParsePosition)'
109: */
110: public void testParseStringParsePosition() {
111: ParsePosition pp = new ParsePosition(3);
112: assertEquals(new Long(123456), NumberFormat.getInstance()
113: .parse("xxx123,456yyy", pp));
114: assertEquals(10, pp.getIndex());
115: }
116:
117: /*
118: * Test method for 'com.ibm.icu.x.text.NumberFormat.parse(String)'
119: */
120: public void testParseString() throws ParseException {
121: Number result = NumberFormat.getInstance().parse("123,456,yyy");
122: assertEquals(new Long(123456), result);
123: }
124:
125: /*
126: * Test method for 'com.ibm.icu.x.text.NumberFormat.isParseIntegerOnly()'
127: */
128: public void testIsParseIntegerOnly() {
129: NumberFormat nf = NumberFormat.getInstance();
130: nf.setParseIntegerOnly(true);
131: assertTrue(nf.isParseIntegerOnly());
132: nf.setParseIntegerOnly(false);
133: assertFalse(nf.isParseIntegerOnly());
134: }
135:
136: /*
137: * Test method for 'com.ibm.icu.x.text.NumberFormat.setParseIntegerOnly(boolean)'
138: */
139: public void testSetParseIntegerOnly() throws ParseException {
140: String str = "123.456,yyy";
141: NumberFormat nf = NumberFormat.getInstance();
142: assertEquals(new Double(123.456), nf.parse(str));
143: nf.setParseIntegerOnly(true);
144: assertEquals(new Long(123), nf.parse(str));
145: }
146:
147: /*
148: * Test method for 'com.ibm.icu.x.text.NumberFormat.getInstance()'
149: */
150: public void testGetInstance() {
151: // used everywhere, no need to test
152: }
153:
154: /*
155: * Test method for 'com.ibm.icu.x.text.NumberFormat.getInstance(Locale)'
156: */
157: public void testGetInstanceLocale() {
158: NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
159: assertEquals("123,456", nf.format(123.456));
160: }
161:
162: /*
163: * Test method for 'com.ibm.icu.x.text.NumberFormat.getInstance(ULocale)'
164: */
165: public void testGetInstanceULocale() {
166: NumberFormat nf = NumberFormat.getInstance(ULocale.GERMANY);
167: assertEquals("123,456", nf.format(123.456));
168: }
169:
170: /*
171: * Test method for 'com.ibm.icu.x.text.NumberFormat.getNumberInstance()'
172: */
173: public void testGetNumberInstance() {
174: NumberFormat nf = NumberFormat.getNumberInstance();
175: assertEquals("123,456.789", nf.format(123456.789));
176: }
177:
178: /*
179: * Test method for 'com.ibm.icu.x.text.NumberFormat.getNumberInstance(Locale)'
180: */
181: public void testGetNumberInstanceLocale() {
182: NumberFormat nf = NumberFormat
183: .getNumberInstance(Locale.GERMANY);
184: assertEquals("123.456,789", nf.format(123456.789));
185: }
186:
187: /*
188: * Test method for 'com.ibm.icu.x.text.NumberFormat.getNumberInstance(ULocale)'
189: */
190: public void testGetNumberInstanceULocale() {
191: NumberFormat nf = NumberFormat
192: .getNumberInstance(ULocale.GERMANY);
193: assertEquals("123.456,789", nf.format(123456.789));
194: }
195:
196: /*
197: * Test method for 'com.ibm.icu.x.text.NumberFormat.getIntegerInstance()'
198: */
199: public void testGetIntegerInstance() {
200: NumberFormat nf = NumberFormat.getIntegerInstance();
201: assertEquals("123,457", nf.format(123456.789)); // rounds
202: }
203:
204: /*
205: * Test method for 'com.ibm.icu.x.text.NumberFormat.getIntegerInstance(Locale)'
206: */
207: public void testGetIntegerInstanceLocale() {
208: NumberFormat nf = NumberFormat
209: .getIntegerInstance(Locale.GERMANY);
210: assertEquals("123.457", nf.format(123456.789)); // rounds
211: }
212:
213: /*
214: * Test method for 'com.ibm.icu.x.text.NumberFormat.getIntegerInstance(ULocale)'
215: */
216: public void testGetIntegerInstanceULocale() {
217: NumberFormat nf = NumberFormat
218: .getIntegerInstance(ULocale.GERMANY);
219: assertEquals("123.457", nf.format(123456.789)); // rounds
220: }
221:
222: /*
223: * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance()'
224: */
225: public void testGetCurrencyInstance() {
226: NumberFormat nf = NumberFormat.getCurrencyInstance();
227: assertEquals("$123,456.99", nf.format(123456.99));
228: }
229:
230: /*
231: * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance(Locale)'
232: */
233: public void testGetCurrencyInstanceLocale() {
234: NumberFormat nf = NumberFormat
235: .getCurrencyInstance(Locale.GERMANY);
236: assertEquals("123.456,99 €", nf.format(123456.99));
237: }
238:
239: /*
240: * Test method for 'com.ibm.icu.x.text.NumberFormat.getCurrencyInstance(ULocale)'
241: */
242: public void testGetCurrencyInstanceULocale() {
243: NumberFormat nf = NumberFormat
244: .getCurrencyInstance(ULocale.GERMANY);
245: assertEquals("123.456,99 €", nf.format(123456.99));
246: }
247:
248: /*
249: * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance()'
250: */
251: public void testGetPercentInstance() {
252: NumberFormat nf = NumberFormat.getPercentInstance();
253: assertEquals("123,456%", nf.format(1234.56));
254: }
255:
256: /*
257: * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance(Locale)'
258: */
259: public void testGetPercentInstanceLocale() {
260: NumberFormat nf = NumberFormat
261: .getPercentInstance(Locale.GERMANY);
262: assertEquals("123.456%", nf.format(1234.56));
263: }
264:
265: /*
266: * Test method for 'com.ibm.icu.x.text.NumberFormat.getPercentInstance(ULocale)'
267: */
268: public void testGetPercentInstanceULocale() {
269: NumberFormat nf = NumberFormat
270: .getPercentInstance(ULocale.GERMANY);
271: assertEquals("123.456%", nf.format(1234.56));
272: }
273:
274: /*
275: * Test method for 'com.ibm.icu.x.text.NumberFormat.getScientificInstance()'
276: */
277: public void testGetScientificInstance() {
278: NumberFormat nf = NumberFormat.getScientificInstance();
279: assertEquals(".123456E4", nf.format(1234.56));
280: }
281:
282: /*
283: * Test method for 'com.ibm.icu.x.text.NumberFormat.getScientificInstance(Locale)'
284: */
285: public void testGetScientificInstanceLocale() {
286: NumberFormat nf = NumberFormat
287: .getScientificInstance(Locale.GERMANY);
288: assertEquals(",123456E4", nf.format(1234.56));
289: }
290:
291: /*
292: * Test method for 'com.ibm.icu.x.text.NumberFormat.getScientificInstance(ULocale)'
293: */
294: public void testGetScientificInstanceULocale() {
295: NumberFormat nf = NumberFormat
296: .getScientificInstance(ULocale.GERMANY);
297: assertEquals(",123456E4", nf.format(1234.56));
298: }
299:
300: /*
301: * Test method for 'com.ibm.icu.x.text.NumberFormat.getAvailableLocales()'
302: */
303: public void testGetAvailableLocales() {
304: Locale[] ilocales = NumberFormat.getAvailableLocales();
305: if (ICUTestCase.testingWrapper) {
306: Locale[] jlocales = java.text.NumberFormat
307: .getAvailableLocales();
308: for (int i = 0; i < ilocales.length; ++i) {
309: assertEquals(jlocales[i], ilocales[i]);
310: }
311: }
312: }
313:
314: /*
315: * Test method for 'com.ibm.icu.x.text.NumberFormat.getAvailableULocales()'
316: */
317: public void testGetAvailableULocales() {
318: ULocale[] ulocales = NumberFormat.getAvailableULocales();
319: if (ICUTestCase.testingWrapper) {
320: Locale[] jlocales = java.text.NumberFormat
321: .getAvailableLocales();
322: for (int i = 0; i < ulocales.length; ++i) {
323: assertEquals(jlocales[i], ulocales[i].toLocale());
324: }
325: }
326: }
327:
328: /*
329: * Test method for 'com.ibm.icu.x.text.NumberFormat.isGroupingUsed()'
330: */
331: public void testIsGroupingUsed() {
332: NumberFormat nf = NumberFormat.getInstance();
333: nf.setGroupingUsed(true);
334: assertTrue(nf.isGroupingUsed());
335: nf.setGroupingUsed(false);
336: assertFalse(nf.isGroupingUsed());
337: }
338:
339: /*
340: * Test method for 'com.ibm.icu.x.text.NumberFormat.setGroupingUsed(boolean)'
341: */
342: public void testSetGroupingUsed() {
343: NumberFormat nf = NumberFormat.getInstance();
344: assertEquals("123,456,789", nf.format(123456789));
345: nf.setGroupingUsed(false);
346: assertEquals("123456789", nf.format(123456789));
347: }
348:
349: /*
350: * Test method for 'com.ibm.icu.x.text.NumberFormat.getMaximumIntegerDigits()'
351: */
352: public void testGetMaximumIntegerDigits() {
353: NumberFormat nf = NumberFormat.getInstance();
354: nf.setMaximumIntegerDigits(4);
355: assertEquals(4, nf.getMaximumIntegerDigits());
356: nf.setMaximumIntegerDigits(6);
357: assertEquals(6, nf.getMaximumIntegerDigits());
358: }
359:
360: /*
361: * Test method for 'com.ibm.icu.x.text.NumberFormat.setMaximumIntegerDigits(int)'
362: */
363: public void testSetMaximumIntegerDigits() {
364: NumberFormat nf = NumberFormat.getInstance();
365: nf.setMaximumIntegerDigits(4);
366: assertEquals("3,456", nf.format(123456)); // high digits truncated
367: }
368:
369: /*
370: * Test method for 'com.ibm.icu.x.text.NumberFormat.getMinimumIntegerDigits()'
371: */
372: public void testGetMinimumIntegerDigits() {
373: NumberFormat nf = NumberFormat.getInstance();
374: nf.setMinimumIntegerDigits(4);
375: assertEquals(4, nf.getMinimumIntegerDigits());
376: nf.setMinimumIntegerDigits(6);
377: assertEquals(6, nf.getMinimumIntegerDigits());
378: }
379:
380: /*
381: * Test method for 'com.ibm.icu.x.text.NumberFormat.setMinimumIntegerDigits(int)'
382: */
383: public void testSetMinimumIntegerDigits() {
384: NumberFormat nf = NumberFormat.getInstance();
385: nf.setMinimumIntegerDigits(4);
386: assertEquals("0,012", nf.format(12)); // pad out with zero, grouping still used
387: }
388:
389: /*
390: * Test method for 'com.ibm.icu.x.text.NumberFormat.getMaximumFractionDigits()'
391: */
392: public void testGetMaximumFractionDigits() {
393: NumberFormat nf = NumberFormat.getInstance();
394: nf.setMaximumFractionDigits(4);
395: assertEquals(4, nf.getMaximumFractionDigits());
396: nf.setMaximumFractionDigits(6);
397: assertEquals(6, nf.getMaximumFractionDigits());
398: }
399:
400: /*
401: * Test method for 'com.ibm.icu.x.text.NumberFormat.setMaximumFractionDigits(int)'
402: */
403: public void testSetMaximumFractionDigits() {
404: NumberFormat nf = NumberFormat.getInstance();
405: nf.setMaximumFractionDigits(4);
406: assertEquals("1.2346", nf.format(1.2345678)); // low digits rounded
407: }
408:
409: /*
410: * Test method for 'com.ibm.icu.x.text.NumberFormat.getMinimumFractionDigits()'
411: */
412: public void testGetMinimumFractionDigits() {
413: NumberFormat nf = NumberFormat.getInstance();
414: nf.setMinimumFractionDigits(4);
415: assertEquals(4, nf.getMinimumFractionDigits());
416: nf.setMinimumFractionDigits(6);
417: assertEquals(6, nf.getMinimumFractionDigits());
418: }
419:
420: /*
421: * Test method for 'com.ibm.icu.x.text.NumberFormat.setMinimumFractionDigits(int)'
422: */
423: public void testSetMinimumFractionDigits() {
424: NumberFormat nf = NumberFormat.getInstance();
425: nf.setMinimumFractionDigits(4);
426: assertEquals("1.2000", nf.format(1.2));
427: }
428:
429: /*
430: * Test method for 'com.ibm.icu.x.text.NumberFormat.toString()'
431: */
432: public void testToString() {
433: assertNotNull(NumberFormat.getInstance().toString());
434: }
435:
436: /*
437: * Test method for 'com.ibm.icu.x.text.NumberFormat.hashCode()'
438: */
439: public void testHashCode() {
440: NumberFormat nf = NumberFormat.getInstance();
441: NumberFormat eq = NumberFormat.getInstance(Locale.US);
442: NumberFormat neq = NumberFormat.getInstance(Locale.GERMANY);
443:
444: ICUTestCase.testEHCS(nf, eq, neq);
445: }
446:
447: /*
448: * Test method for 'com.ibm.icu.x.text.NumberFormat.clone()'
449: */
450: public void testClone() {
451: // see testHashCode
452: }
453:
454: /*
455: * Test method for 'com.ibm.icu.x.text.NumberFormat.equals(Object)'
456: */
457: public void testEqualsObject() {
458: // see testHashCode
459: }
460: }
|