Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java
41153 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 8177552 8217254 825149925* @summary Checks the validity of compact number patterns specified through26* CompactNumberFormat constructor27* @run testng/othervm TestCompactPatternsValidity28*/2930import java.math.BigDecimal;31import java.math.BigInteger;32import java.text.CompactNumberFormat;33import java.text.DecimalFormatSymbols;34import java.text.ParseException;35import java.util.List;36import java.util.Locale;37import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940public class TestCompactPatternsValidity {4142// Max range 10^443private static final String[] COMPACT_PATTERN1 = new String[]{"0", "0", "0", "0K", "00K"};44// Quoted special character '.' as prefix45private static final String[] COMPACT_PATTERN2 = new String[]{"0", "'.'K0"};46// Quoted special character '.' as suffix47private static final String[] COMPACT_PATTERN3 = new String[]{"0", "0", "0", "0K", "00K'.'"};48// Containing both prefix and suffix49private static final String[] COMPACT_PATTERN4 = new String[]{"", "", "H0H", "0K", "00K", "H0G"};50// Differing while specifying prefix and suffix51private static final String[] COMPACT_PATTERN5 = new String[]{"", "", "", "0K", "K0"};52// Containing both prefix ('.') and suffix (K)53private static final String[] COMPACT_PATTERN6 = new String[]{"0", "", "", "'.'0K"};54// Quoted special character ',' as suffix55private static final String[] COMPACT_PATTERN7 = new String[]{"", "0", "0", "0K','"};56// Most commonly used type of compact patterns with 15 elements57private static final String[] COMPACT_PATTERN8 = new String[]{"", "", "", "0K", "00K", "000K", "0M",58"00M", "000M", "0B", "00B", "000B", "0T", "00T", "000T"};59// All empty or special patterns; checking the default formatting behaviour60private static final String[] COMPACT_PATTERN9 = new String[]{"", "", "", "0", "0", "", "", "", "", "", "", "", "", "", ""};61// Patterns beyond 10^19; divisors beyond long range62private static final String[] COMPACT_PATTERN10 = new String[]{"", "", "", "0K", "00K", "000K", "0M", "00M",63"000M", "0B", "00B", "000B", "0T", "00T", "000T", "0L", "00L", "000L", "0XL", "00XL"};64// Containing positive;negative subpatterns65private static final String[] COMPACT_PATTERN11 = new String[]{"", "", "", "elfu 0;elfu -0", "elfu 00;elfu -00",66"elfu 000;elfu -000", "milioni 0;milioni -0", "milioni 00;milioni -00", "milioni 000;milioni -000"};67// Containing both prefix and suffix and positive;negative subpatern68private static final String[] COMPACT_PATTERN12 = new String[]{"", "", "H0H;H-0H", "0K;0K-", "00K;-00K", "H0G;-H0G"};69// A non empty pattern containing no 0s (min integer digits)70private static final String[] COMPACT_PATTERN13 =71new String[]{"", "", "", "Thousand", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "BeyondLong"};72// A non empty pattern containing no 0s (min integer digits), with plural rules73private static final String[] COMPACT_PATTERN14 =74new String[]{"", "", "", "{one:Kun other:0' 'Kun}"}; // from Somali in CLDR 387576@DataProvider(name = "invalidPatterns")77Object[][] invalidCompactPatterns() {78return new Object[][] {79// compact patterns80// Pattern containing unquoted special character '.'81{new String[]{"", "", "", "0K", "00K."}},82// Pattern containing invalid single quote83{new String[]{"", "", "", "0 'do", "00K"}},84{new String[]{"", "", "", "0K", "00 don't"}},85// 0s (min integer digits) exceeding for the range at index 386{new String[]{"", "", "0K", "00000K"}},87// null as a compact pattern88{new String[]{"", "", null, "00K"}},89};90}9192@DataProvider(name = "validPatternsFormat")93Object[][] validPatternsFormat() {94return new Object[][] {95// compact patterns, numbers, expected output96{COMPACT_PATTERN1, List.of(200, 1000, 3000, 500000), List.of("200", "1K", "3K", "500K")},97{COMPACT_PATTERN2, List.of(1, 20, 3000), List.of("1", ".K2", ".K300")},98{COMPACT_PATTERN3, List.of(100.99, 1000, 30000), List.of("101", "1K", "30K.")},99{COMPACT_PATTERN4, List.of(0.0, 500, -500, 30000, 5000000), List.of("0", "H5H", "-H5H", "30K", "H50G")},100{COMPACT_PATTERN5, List.of(100, 1000, 30000), List.of("100", "1K", "K3")},101{COMPACT_PATTERN6, List.of(20.99, 1000, 30000), List.of("21", ".1K", ".30K")},102{COMPACT_PATTERN7, List.of(100, 1000, new BigInteger("12345678987654321")), List.of("100", "1K,", "12345678987654K,")},103{COMPACT_PATTERN8, List.of(new BigInteger("223565686837667632"), new BigDecimal("12322456774334.89766"), 30000, 3456.78),104List.of("223566T", "12T", "30K", "3K")},105{COMPACT_PATTERN9, List.of(new BigInteger("223566000000000000"), new BigDecimal("12345678987654567"), 30000, 3000),106List.of("223,566,000,000,000,000", "12,345,678,987,654,567", "30,000", "3,000")},107{COMPACT_PATTERN10, List.of(new BigInteger("100000000000000000"), new BigInteger("10000000000000000000"), new BigDecimal("555555555555555555555.89766"), 30000),108List.of("100L", "10XL", "556XL", "30K")},109{COMPACT_PATTERN11, List.of(20.99, -20.99, 1000, -1000, 30000, -30000, new BigInteger("12345678987654321"), new BigInteger("-12345678987654321")),110List.of("21", "-21", "elfu 1", "elfu -1", "elfu 30", "elfu -30", "milioni 12345678988", "milioni -12345678988")},111{COMPACT_PATTERN12, List.of(0, 500, -500, 30000, -3000, 5000000), List.of("0", "H5H", "H-5H", "30K", "3K-", "H50G")},112{COMPACT_PATTERN13, List.of(1000, new BigInteger("10000000000000000000")), List.of("Thousand", "BeyondLong")},113};114}115116@DataProvider(name = "validPatternsParse")117Object[][] validPatternsParse() {118return new Object[][] {119// compact patterns, parse string, expected output120{COMPACT_PATTERN1, List.of(".56", "200", ".1K", "3K", "500K"), List.of(0.56, 200L, 100L, 3000L, 500000L)},121{COMPACT_PATTERN2, List.of("1", ".K2", ".K300"), List.of(1L, 20L, 3000L)},122{COMPACT_PATTERN3, List.of("101", "1K", "30K."), List.of(101L, 1000L, 30000L)},123{COMPACT_PATTERN4, List.of("0", "H5H", "-H5H", "30K", "H50G"), List.of(0L, 500L, -500L, 30000L, 5000000L)},124{COMPACT_PATTERN5, List.of("100", "1K", "K3"), List.of(100L, 1000L, 30000L)},125{COMPACT_PATTERN6, List.of("21", ".1K", ".30K"), List.of(21L, 1000L, 30000L)},126{COMPACT_PATTERN7, List.of("100", "1K,", "12345678987654K,"), List.of(100L, 1000L, 12345678987654000L)},127{COMPACT_PATTERN8, List.of("223566T", "12T", "30K", "3K"), List.of(223566000000000000L, 12000000000000L, 30000L, 3000L)},128{COMPACT_PATTERN10, List.of("1L", "100L", "10XL", "556XL", "30K"), List.of(1000000000000000L, 100000000000000000L, 1.0E19, 5.56E20, 30000L)},129{COMPACT_PATTERN11, List.of("21", "-21", "100.90", "-100.90", "elfu 1", "elfu -1", "elfu 30", "elfu -30", "milioni 12345678988", "milioni -12345678988"),130List.of(21L, -21L, 100.90, -100.90, 1000L, -1000L, 30000L, -30000L, 12345678988000000L, -12345678988000000L)},131{COMPACT_PATTERN12, List.of("0", "H5H", "H-5H", "30K", "30K-", "H50G"), List.of(0L, 500L, -500L, 30000L, -30000L, 5000000L)},132{COMPACT_PATTERN13, List.of("Thousand", "BeyondLong"), List.of(1000L, new BigInteger("10000000000000000000"))},133};134}135136@DataProvider(name = "validPatternsFormatWithPluralRules")137Object[][] validPatternsFormatWithPluralRules() {138return new Object[][] {139// compact patterns, plural rules, numbers, expected output140{COMPACT_PATTERN14, "one:n = 1", List.of(1000, 2345), List.of("Kun", "2 Kun")},141};142}143144@DataProvider(name = "validPatternsParseWithPluralRules")145Object[][] validPatternsParseWithPluralRules() {146return new Object[][] {147// compact patterns, plural rules, parse string, expected output148{COMPACT_PATTERN14, "one:n = 1", List.of("Kun", "2 Kun"), List.of(1000L, 2000L)},149};150}151152@Test(dataProvider = "invalidPatterns",153expectedExceptions = IllegalArgumentException.class)154public void testInvalidCompactPatterns(String[] compactPatterns) {155new CompactNumberFormat("#,##0.0#", DecimalFormatSymbols156.getInstance(Locale.US), compactPatterns);157}158159@Test(dataProvider = "validPatternsFormat")160public void testValidPatternsFormat(String[] compactPatterns,161List<Object> numbers, List<String> expected) {162CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",163DecimalFormatSymbols.getInstance(Locale.US), compactPatterns);164for (int index = 0; index < numbers.size(); index++) {165CompactFormatAndParseHelper.testFormat(fmt, numbers.get(index),166expected.get(index));167}168}169170@Test(dataProvider = "validPatternsParse")171public void testValidPatternsParse(String[] compactPatterns,172List<String> parseString, List<Number> numbers) throws ParseException {173CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",174DecimalFormatSymbols.getInstance(Locale.US), compactPatterns);175for (int index = 0; index < parseString.size(); index++) {176CompactFormatAndParseHelper.testParse(fmt, parseString.get(index),177numbers.get(index), null, null);178}179}180181@Test(dataProvider = "validPatternsFormatWithPluralRules")182public void testValidPatternsFormatWithPluralRules(String[] compactPatterns, String pluralRules,183List<Object> numbers, List<String> expected) {184CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",185DecimalFormatSymbols.getInstance(Locale.US), compactPatterns, pluralRules);186for (int index = 0; index < numbers.size(); index++) {187CompactFormatAndParseHelper.testFormat(fmt, numbers.get(index),188expected.get(index));189}190}191192@Test(dataProvider = "validPatternsParseWithPluralRules")193public void testValidPatternsParsewithPluralRules(String[] compactPatterns, String pluralRules,194List<String> parseString, List<Number> numbers) throws ParseException {195CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",196DecimalFormatSymbols.getInstance(Locale.US), compactPatterns, pluralRules);197for (int index = 0; index < parseString.size(); index++) {198CompactFormatAndParseHelper.testParse(fmt, parseString.get(index),199numbers.get(index), null, null);200}201}202}203204205