Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestPlurals.java
41153 views
/*1* Copyright (c) 2019, 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 822275625* @summary Tests plurals support in CompactNumberFormat26* @run testng/othervm TestPlurals27*/2829import java.text.CompactNumberFormat;30import java.text.DecimalFormatSymbols;31import java.util.Locale;3233import static org.testng.Assert.*;34import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637public class TestPlurals {3839private final static DecimalFormatSymbols DFS = DecimalFormatSymbols.getInstance(Locale.ROOT);40private final static String[] PATTERN = {41"{zero:0->zero one:0->one two:0->two few:0->few many:0->many other:0->other}"};42private final static String RULE_1 = "zero:n = 0; one:n = 1; two:n = 2; few:n = 3..4; many:n = 5..6,8";43private final static String RULE_2 = "one:n % 2 = 1 or n / 3 = 2;";44private final static String RULE_3 = "one:n%2=0andn/3=2;";454647@DataProvider48Object[][] pluralRules() {49return new Object[][]{50// rules, number, expected51{RULE_1, 0, "0->zero"},52{RULE_1, 1, "1->one"},53{RULE_1, 2, "2->two"},54{RULE_1, 3, "3->few"},55{RULE_1, 4, "4->few"},56{RULE_1, 5, "5->many"},57{RULE_1, 6, "6->many"},58{RULE_1, 7, "7->other"},59{RULE_1, 8, "8->many"},60{RULE_1, 9, "9->other"},6162{RULE_2, 0, "0->other"},63{RULE_2, 1, "1->one"},64{RULE_2, 2, "2->other"},65{RULE_2, 3, "3->one"},66{RULE_2, 4, "4->other"},67{RULE_2, 5, "5->one"},68{RULE_2, 6, "6->one"},6970{RULE_3, 0, "0->other"},71{RULE_3, 1, "1->other"},72{RULE_3, 2, "2->other"},73{RULE_3, 3, "3->other"},74{RULE_3, 4, "4->other"},75{RULE_3, 5, "5->other"},76{RULE_3, 6, "6->one"},77};78}7980@DataProvider81Object[][] invalidRules() {82return new Object [][] {83{"one:a = 1"},84{"on:n = 1"},85{"one:n = 1...2"},86{"one:n = 1.2"},87{"one:n = 1..2,"},88{"one:n = 1;one:n = 2"},89{"foo:n = 1"},90{"one:n = 1..2 andor v % 10 != 0"},91};92}9394@Test(expectedExceptions = NullPointerException.class)95public void testNullPluralRules() {96String[] pattern = {""};97new CompactNumberFormat("#", DFS, PATTERN, null);98}99100@Test(dataProvider = "pluralRules")101public void testPluralRules(String rules, Number n, String expected) {102var cnp = new CompactNumberFormat("#", DFS, PATTERN, rules);103assertEquals(cnp.format(n), expected);104}105106@Test(dataProvider = "invalidRules", expectedExceptions = IllegalArgumentException.class)107public void testInvalidRules(String rules) {108new CompactNumberFormat("#", DFS, PATTERN, rules);109}110111@Test(expectedExceptions = IllegalArgumentException.class)112public void testLimitExceedingRules() {113String andCond = " and n = 1";114String invalid = "one: n = 1" + andCond.repeat(2_048 / andCond.length());115new CompactNumberFormat("#", DFS, PATTERN, invalid);116}117}118119120