Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestCNFRounding.java
41153 views
/*1* Copyright (c) 2018, 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 817755225* @summary Checks the rounding of formatted number in compact number formatting26* @run testng/othervm TestCNFRounding27*/2829import java.math.RoundingMode;30import java.text.NumberFormat;31import java.util.List;32import java.util.Locale;33import static org.testng.Assert.*;34import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637public class TestCNFRounding {3839private static final List<RoundingMode> MODES = List.of(40RoundingMode.HALF_EVEN,41RoundingMode.HALF_UP,42RoundingMode.HALF_DOWN,43RoundingMode.UP,44RoundingMode.DOWN,45RoundingMode.CEILING,46RoundingMode.FLOOR);4748@DataProvider(name = "roundingData")49Object[][] roundingData() {50return new Object[][]{51// Number, half_even, half_up, half_down, up, down, ceiling, floor52{5500, new String[]{"6K", "6K", "5K", "6K", "5K", "6K", "5K"}},53{2500, new String[]{"2K", "3K", "2K", "3K", "2K", "3K", "2K"}},54{1600, new String[]{"2K", "2K", "2K", "2K", "1K", "2K", "1K"}},55{1100, new String[]{"1K", "1K", "1K", "2K", "1K", "2K", "1K"}},56{1000, new String[]{"1K", "1K", "1K", "1K", "1K", "1K", "1K"}},57{-1000, new String[]{"-1K", "-1K", "-1K", "-1K", "-1K", "-1K", "-1K"}},58{-1100, new String[]{"-1K", "-1K", "-1K", "-2K", "-1K", "-1K", "-2K"}},59{-1600, new String[]{"-2K", "-2K", "-2K", "-2K", "-1K", "-1K", "-2K"}},60{-2500, new String[]{"-2K", "-3K", "-2K", "-3K", "-2K", "-2K", "-3K"}},61{-5500, new String[]{"-6K", "-6K", "-5K", "-6K", "-5K", "-5K", "-6K"}},62{5501, new String[]{"6K", "6K", "6K", "6K", "5K", "6K", "5K"}},63{-5501, new String[]{"-6K", "-6K", "-6K", "-6K", "-5K", "-5K", "-6K"}},64{1001, new String[]{"1K", "1K", "1K", "2K", "1K", "2K", "1K"}},65{-1001, new String[]{"-1K", "-1K", "-1K", "-2K", "-1K", "-1K", "-2K"}},66{4501, new String[]{"5K", "5K", "5K", "5K", "4K", "5K", "4K"}},67{-4501, new String[]{"-5K", "-5K", "-5K", "-5K", "-4K", "-4K", "-5K"}},68{4500, new String[]{"4K", "5K", "4K", "5K", "4K", "5K", "4K"}},69{-4500, new String[]{"-4K", "-5K", "-4K", "-5K", "-4K", "-4K", "-5K"}},};70}7172@DataProvider(name = "roundingFract")73Object[][] roundingFract() {74return new Object[][]{75// Number, half_even, half_up, half_down, up, down, ceiling, floor76{5550, new String[]{"5.5K", "5.5K", "5.5K", "5.6K", "5.5K", "5.6K", "5.5K"}},77{2550, new String[]{"2.5K", "2.5K", "2.5K", "2.6K", "2.5K", "2.6K", "2.5K"}},78{1660, new String[]{"1.7K", "1.7K", "1.7K", "1.7K", "1.6K", "1.7K", "1.6K"}},79{1110, new String[]{"1.1K", "1.1K", "1.1K", "1.2K", "1.1K", "1.2K", "1.1K"}},80{1000, new String[]{"1.0K", "1.0K", "1.0K", "1.0K", "1.0K", "1.0K", "1.0K"}},81{-1000, new String[]{"-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K"}},82{-1110, new String[]{"-1.1K", "-1.1K", "-1.1K", "-1.2K", "-1.1K", "-1.1K", "-1.2K"}},83{-1660, new String[]{"-1.7K", "-1.7K", "-1.7K", "-1.7K", "-1.6K", "-1.6K", "-1.7K"}},84{-2550, new String[]{"-2.5K", "-2.5K", "-2.5K", "-2.6K", "-2.5K", "-2.5K", "-2.6K"}},85{-5550, new String[]{"-5.5K", "-5.5K", "-5.5K", "-5.6K", "-5.5K", "-5.5K", "-5.6K"}},86{5551, new String[]{"5.6K", "5.6K", "5.6K", "5.6K", "5.5K", "5.6K", "5.5K"}},87{-5551, new String[]{"-5.6K", "-5.6K", "-5.6K", "-5.6K", "-5.5K", "-5.5K", "-5.6K"}},88{1001, new String[]{"1.0K", "1.0K", "1.0K", "1.1K", "1.0K", "1.1K", "1.0K"}},89{-1001, new String[]{"-1.0K", "-1.0K", "-1.0K", "-1.1K", "-1.0K", "-1.0K", "-1.1K"}},90{4551, new String[]{"4.6K", "4.6K", "4.6K", "4.6K", "4.5K", "4.6K", "4.5K"}},91{-4551, new String[]{"-4.6K", "-4.6K", "-4.6K", "-4.6K", "-4.5K", "-4.5K", "-4.6K"}},92{4500, new String[]{"4.5K", "4.5K", "4.5K", "4.5K", "4.5K", "4.5K", "4.5K"}},93{-4500, new String[]{"-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K"}},};94}9596@DataProvider(name = "rounding2Fract")97Object[][] rounding2Fract() {98return new Object[][]{99// Number, half_even, half_up, half_down100{1115, new String[]{"1.11K", "1.11K", "1.11K"}},101{1125, new String[]{"1.12K", "1.13K", "1.12K"}},102{1135, new String[]{"1.14K", "1.14K", "1.14K"}},103{3115, new String[]{"3.12K", "3.12K", "3.12K"}},104{3125, new String[]{"3.12K", "3.13K", "3.12K"}},105{3135, new String[]{"3.13K", "3.13K", "3.13K"}},106{6865, new String[]{"6.87K", "6.87K", "6.87K"}},107{6875, new String[]{"6.88K", "6.88K", "6.87K"}},108{6885, new String[]{"6.88K", "6.88K", "6.88K"}},109{3124, new String[]{"3.12K", "3.12K", "3.12K"}},110{3126, new String[]{"3.13K", "3.13K", "3.13K"}},111{3128, new String[]{"3.13K", "3.13K", "3.13K"}},112{6864, new String[]{"6.86K", "6.86K", "6.86K"}},113{6865, new String[]{"6.87K", "6.87K", "6.87K"}},114{6868, new String[]{"6.87K", "6.87K", "6.87K"}},115{4685, new String[]{"4.68K", "4.68K", "4.68K"}},116{4687, new String[]{"4.69K", "4.69K", "4.69K"}},117{4686, new String[]{"4.69K", "4.69K", "4.69K"}},};118}119120@Test(expectedExceptions = NullPointerException.class)121public void testNullMode() {122NumberFormat fmt = NumberFormat123.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);124fmt.setRoundingMode(null);125}126127@Test128public void testDefaultRoundingMode() {129NumberFormat fmt = NumberFormat130.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);131assertEquals(fmt.getRoundingMode(), RoundingMode.HALF_EVEN,132"Default RoundingMode should be " + RoundingMode.HALF_EVEN);133}134135@Test(dataProvider = "roundingData")136public void testRounding(Object number, String[] expected) {137for (int index = 0; index < MODES.size(); index++) {138testRoundingMode(number, expected[index], 0, MODES.get(index));139}140}141142@Test(dataProvider = "roundingFract")143public void testRoundingFract(Object number, String[] expected) {144for (int index = 0; index < MODES.size(); index++) {145testRoundingMode(number, expected[index], 1, MODES.get(index));146}147}148149@Test(dataProvider = "rounding2Fract")150public void testRounding2Fract(Object number, String[] expected) {151List<RoundingMode> rModes = List.of(RoundingMode.HALF_EVEN,152RoundingMode.HALF_UP, RoundingMode.HALF_DOWN);153for (int index = 0; index < rModes.size(); index++) {154testRoundingMode(number, expected[index], 2, rModes.get(index));155}156}157158private void testRoundingMode(Object number, String expected,159int fraction, RoundingMode rounding) {160NumberFormat fmt = NumberFormat161.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);162fmt.setRoundingMode(rounding);163assertEquals(fmt.getRoundingMode(), rounding,164"RoundingMode set is not returned by getRoundingMode");165166fmt.setMinimumFractionDigits(fraction);167String result = fmt.format(number);168assertEquals(result, expected, "Incorrect formatting of number "169+ number + " using rounding mode: " + rounding);170}171172}173174175