1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * @test 25 * @bug 8177552 26 * @summary Checks the functioning of compact number format 27 * @modules jdk.localedata 28 * @run testng/othervm TestCompactNumber 29 */ 30 import java.math.BigDecimal; 31 import java.math.BigInteger; 32 import java.text.FieldPosition; 33 import java.text.Format; 34 import java.text.NumberFormat; 35 import java.text.ParseException; 36 import java.text.ParsePosition; 37 import java.util.Locale; 38 import java.util.stream.Stream; 39 import static org.testng.Assert.*; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 43 public class TestCompactNumber { 44 45 private static final NumberFormat FORMAT_DZ_LONG = NumberFormat 509 {FORMAT_EN_US_SHORT, "Exponent: -1.05E4K", -1.05, 10, 15, -1}, 510 // Default instance does not allow grouping 511 {FORMAT_EN_US_SHORT, "12,347", 12L, 0, 2, -1}, 512 // Invalid suffix "KM" for en_US_SHORT patterns 513 {FORMAT_EN_US_SHORT, "12KM", 12000L, 0, 3, -1}, 514 // Invalid suffix 515 {FORMAT_HI_IN_LONG, "-1 \u00a0\u0915.", -1L, 0, 2, -1}, 516 {FORMAT_EN_LONG, "Number is: 12345679 trillion", 517 1.2345679E19, 11, 28, -1}, 518 {FORMAT_EN_LONG, "Number is: -12345679 trillion", 519 -1.2345679E19, 11, 29, -1}, 520 {FORMAT_EN_LONG, "parse 12 thousand and four", 12000L, 6, 17, -1},}; 521 } 522 523 @Test 524 public void testInstanceCreation() { 525 Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat 526 .getCompactNumberInstance(l, NumberFormat.Style.SHORT).format(10000)); 527 Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat 528 .getCompactNumberInstance(l, NumberFormat.Style.LONG).format(10000)); 529 } 530 531 @Test(dataProvider = "format") 532 public void testFormat(NumberFormat cnf, Object number, 533 String expected) { 534 CompactFormatAndParseHelper.testFormat(cnf, number, expected); 535 } 536 537 @Test(dataProvider = "parse") 538 public void testParse(NumberFormat cnf, String parseString, 539 Number expected, Class<? extends Number> returnType) throws ParseException { 540 CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, returnType); 541 } 542 543 @Test(dataProvider = "parse") 544 public void testParsePosition(NumberFormat cnf, String parseString, 545 Number expected, Class<? extends Number> returnType) throws ParseException { 546 ParsePosition pos = new ParsePosition(0); 547 CompactFormatAndParseHelper.testParse(cnf, parseString, expected, pos, returnType); 548 assertEquals(pos.getIndex(), parseString.length()); | 1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * @test 25 * @bug 8177552 8217721 26 * @summary Checks the functioning of compact number format 27 * @modules jdk.localedata 28 * @run testng/othervm TestCompactNumber 29 */ 30 import java.math.BigDecimal; 31 import java.math.BigInteger; 32 import java.text.FieldPosition; 33 import java.text.Format; 34 import java.text.NumberFormat; 35 import java.text.ParseException; 36 import java.text.ParsePosition; 37 import java.util.Locale; 38 import java.util.stream.Stream; 39 import static org.testng.Assert.*; 40 import org.testng.annotations.DataProvider; 41 import org.testng.annotations.Test; 42 43 public class TestCompactNumber { 44 45 private static final NumberFormat FORMAT_DZ_LONG = NumberFormat 509 {FORMAT_EN_US_SHORT, "Exponent: -1.05E4K", -1.05, 10, 15, -1}, 510 // Default instance does not allow grouping 511 {FORMAT_EN_US_SHORT, "12,347", 12L, 0, 2, -1}, 512 // Invalid suffix "KM" for en_US_SHORT patterns 513 {FORMAT_EN_US_SHORT, "12KM", 12000L, 0, 3, -1}, 514 // Invalid suffix 515 {FORMAT_HI_IN_LONG, "-1 \u00a0\u0915.", -1L, 0, 2, -1}, 516 {FORMAT_EN_LONG, "Number is: 12345679 trillion", 517 1.2345679E19, 11, 28, -1}, 518 {FORMAT_EN_LONG, "Number is: -12345679 trillion", 519 -1.2345679E19, 11, 29, -1}, 520 {FORMAT_EN_LONG, "parse 12 thousand and four", 12000L, 6, 17, -1},}; 521 } 522 523 @Test 524 public void testInstanceCreation() { 525 Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat 526 .getCompactNumberInstance(l, NumberFormat.Style.SHORT).format(10000)); 527 Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat 528 .getCompactNumberInstance(l, NumberFormat.Style.LONG).format(10000)); 529 } 530 531 @Test(expectedExceptions = IllegalArgumentException.class) 532 public void testFormatWithNullParam() { 533 FORMAT_EN_US_SHORT.format(null); 534 } 535 536 @Test(dataProvider = "format") 537 public void testFormat(NumberFormat cnf, Object number, 538 String expected) { 539 CompactFormatAndParseHelper.testFormat(cnf, number, expected); 540 } 541 542 @Test(dataProvider = "parse") 543 public void testParse(NumberFormat cnf, String parseString, 544 Number expected, Class<? extends Number> returnType) throws ParseException { 545 CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, returnType); 546 } 547 548 @Test(dataProvider = "parse") 549 public void testParsePosition(NumberFormat cnf, String parseString, 550 Number expected, Class<? extends Number> returnType) throws ParseException { 551 ParsePosition pos = new ParsePosition(0); 552 CompactFormatAndParseHelper.testParse(cnf, parseString, expected, pos, returnType); 553 assertEquals(pos.getIndex(), parseString.length()); |