Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestCNFRounding.java
41153 views
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 rounding of formatted number in compact number formatting
27
* @run testng/othervm TestCNFRounding
28
*/
29
30
import java.math.RoundingMode;
31
import java.text.NumberFormat;
32
import java.util.List;
33
import java.util.Locale;
34
import static org.testng.Assert.*;
35
import org.testng.annotations.DataProvider;
36
import org.testng.annotations.Test;
37
38
public class TestCNFRounding {
39
40
private static final List<RoundingMode> MODES = List.of(
41
RoundingMode.HALF_EVEN,
42
RoundingMode.HALF_UP,
43
RoundingMode.HALF_DOWN,
44
RoundingMode.UP,
45
RoundingMode.DOWN,
46
RoundingMode.CEILING,
47
RoundingMode.FLOOR);
48
49
@DataProvider(name = "roundingData")
50
Object[][] roundingData() {
51
return new Object[][]{
52
// Number, half_even, half_up, half_down, up, down, ceiling, floor
53
{5500, new String[]{"6K", "6K", "5K", "6K", "5K", "6K", "5K"}},
54
{2500, new String[]{"2K", "3K", "2K", "3K", "2K", "3K", "2K"}},
55
{1600, new String[]{"2K", "2K", "2K", "2K", "1K", "2K", "1K"}},
56
{1100, new String[]{"1K", "1K", "1K", "2K", "1K", "2K", "1K"}},
57
{1000, new String[]{"1K", "1K", "1K", "1K", "1K", "1K", "1K"}},
58
{-1000, new String[]{"-1K", "-1K", "-1K", "-1K", "-1K", "-1K", "-1K"}},
59
{-1100, new String[]{"-1K", "-1K", "-1K", "-2K", "-1K", "-1K", "-2K"}},
60
{-1600, new String[]{"-2K", "-2K", "-2K", "-2K", "-1K", "-1K", "-2K"}},
61
{-2500, new String[]{"-2K", "-3K", "-2K", "-3K", "-2K", "-2K", "-3K"}},
62
{-5500, new String[]{"-6K", "-6K", "-5K", "-6K", "-5K", "-5K", "-6K"}},
63
{5501, new String[]{"6K", "6K", "6K", "6K", "5K", "6K", "5K"}},
64
{-5501, new String[]{"-6K", "-6K", "-6K", "-6K", "-5K", "-5K", "-6K"}},
65
{1001, new String[]{"1K", "1K", "1K", "2K", "1K", "2K", "1K"}},
66
{-1001, new String[]{"-1K", "-1K", "-1K", "-2K", "-1K", "-1K", "-2K"}},
67
{4501, new String[]{"5K", "5K", "5K", "5K", "4K", "5K", "4K"}},
68
{-4501, new String[]{"-5K", "-5K", "-5K", "-5K", "-4K", "-4K", "-5K"}},
69
{4500, new String[]{"4K", "5K", "4K", "5K", "4K", "5K", "4K"}},
70
{-4500, new String[]{"-4K", "-5K", "-4K", "-5K", "-4K", "-4K", "-5K"}},};
71
}
72
73
@DataProvider(name = "roundingFract")
74
Object[][] roundingFract() {
75
return new Object[][]{
76
// Number, half_even, half_up, half_down, up, down, ceiling, floor
77
{5550, new String[]{"5.5K", "5.5K", "5.5K", "5.6K", "5.5K", "5.6K", "5.5K"}},
78
{2550, new String[]{"2.5K", "2.5K", "2.5K", "2.6K", "2.5K", "2.6K", "2.5K"}},
79
{1660, new String[]{"1.7K", "1.7K", "1.7K", "1.7K", "1.6K", "1.7K", "1.6K"}},
80
{1110, new String[]{"1.1K", "1.1K", "1.1K", "1.2K", "1.1K", "1.2K", "1.1K"}},
81
{1000, new String[]{"1.0K", "1.0K", "1.0K", "1.0K", "1.0K", "1.0K", "1.0K"}},
82
{-1000, new String[]{"-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K", "-1.0K"}},
83
{-1110, new String[]{"-1.1K", "-1.1K", "-1.1K", "-1.2K", "-1.1K", "-1.1K", "-1.2K"}},
84
{-1660, new String[]{"-1.7K", "-1.7K", "-1.7K", "-1.7K", "-1.6K", "-1.6K", "-1.7K"}},
85
{-2550, new String[]{"-2.5K", "-2.5K", "-2.5K", "-2.6K", "-2.5K", "-2.5K", "-2.6K"}},
86
{-5550, new String[]{"-5.5K", "-5.5K", "-5.5K", "-5.6K", "-5.5K", "-5.5K", "-5.6K"}},
87
{5551, new String[]{"5.6K", "5.6K", "5.6K", "5.6K", "5.5K", "5.6K", "5.5K"}},
88
{-5551, new String[]{"-5.6K", "-5.6K", "-5.6K", "-5.6K", "-5.5K", "-5.5K", "-5.6K"}},
89
{1001, new String[]{"1.0K", "1.0K", "1.0K", "1.1K", "1.0K", "1.1K", "1.0K"}},
90
{-1001, new String[]{"-1.0K", "-1.0K", "-1.0K", "-1.1K", "-1.0K", "-1.0K", "-1.1K"}},
91
{4551, new String[]{"4.6K", "4.6K", "4.6K", "4.6K", "4.5K", "4.6K", "4.5K"}},
92
{-4551, new String[]{"-4.6K", "-4.6K", "-4.6K", "-4.6K", "-4.5K", "-4.5K", "-4.6K"}},
93
{4500, new String[]{"4.5K", "4.5K", "4.5K", "4.5K", "4.5K", "4.5K", "4.5K"}},
94
{-4500, new String[]{"-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K", "-4.5K"}},};
95
}
96
97
@DataProvider(name = "rounding2Fract")
98
Object[][] rounding2Fract() {
99
return new Object[][]{
100
// Number, half_even, half_up, half_down
101
{1115, new String[]{"1.11K", "1.11K", "1.11K"}},
102
{1125, new String[]{"1.12K", "1.13K", "1.12K"}},
103
{1135, new String[]{"1.14K", "1.14K", "1.14K"}},
104
{3115, new String[]{"3.12K", "3.12K", "3.12K"}},
105
{3125, new String[]{"3.12K", "3.13K", "3.12K"}},
106
{3135, new String[]{"3.13K", "3.13K", "3.13K"}},
107
{6865, new String[]{"6.87K", "6.87K", "6.87K"}},
108
{6875, new String[]{"6.88K", "6.88K", "6.87K"}},
109
{6885, new String[]{"6.88K", "6.88K", "6.88K"}},
110
{3124, new String[]{"3.12K", "3.12K", "3.12K"}},
111
{3126, new String[]{"3.13K", "3.13K", "3.13K"}},
112
{3128, new String[]{"3.13K", "3.13K", "3.13K"}},
113
{6864, new String[]{"6.86K", "6.86K", "6.86K"}},
114
{6865, new String[]{"6.87K", "6.87K", "6.87K"}},
115
{6868, new String[]{"6.87K", "6.87K", "6.87K"}},
116
{4685, new String[]{"4.68K", "4.68K", "4.68K"}},
117
{4687, new String[]{"4.69K", "4.69K", "4.69K"}},
118
{4686, new String[]{"4.69K", "4.69K", "4.69K"}},};
119
}
120
121
@Test(expectedExceptions = NullPointerException.class)
122
public void testNullMode() {
123
NumberFormat fmt = NumberFormat
124
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
125
fmt.setRoundingMode(null);
126
}
127
128
@Test
129
public void testDefaultRoundingMode() {
130
NumberFormat fmt = NumberFormat
131
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
132
assertEquals(fmt.getRoundingMode(), RoundingMode.HALF_EVEN,
133
"Default RoundingMode should be " + RoundingMode.HALF_EVEN);
134
}
135
136
@Test(dataProvider = "roundingData")
137
public void testRounding(Object number, String[] expected) {
138
for (int index = 0; index < MODES.size(); index++) {
139
testRoundingMode(number, expected[index], 0, MODES.get(index));
140
}
141
}
142
143
@Test(dataProvider = "roundingFract")
144
public void testRoundingFract(Object number, String[] expected) {
145
for (int index = 0; index < MODES.size(); index++) {
146
testRoundingMode(number, expected[index], 1, MODES.get(index));
147
}
148
}
149
150
@Test(dataProvider = "rounding2Fract")
151
public void testRounding2Fract(Object number, String[] expected) {
152
List<RoundingMode> rModes = List.of(RoundingMode.HALF_EVEN,
153
RoundingMode.HALF_UP, RoundingMode.HALF_DOWN);
154
for (int index = 0; index < rModes.size(); index++) {
155
testRoundingMode(number, expected[index], 2, rModes.get(index));
156
}
157
}
158
159
private void testRoundingMode(Object number, String expected,
160
int fraction, RoundingMode rounding) {
161
NumberFormat fmt = NumberFormat
162
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
163
fmt.setRoundingMode(rounding);
164
assertEquals(fmt.getRoundingMode(), rounding,
165
"RoundingMode set is not returned by getRoundingMode");
166
167
fmt.setMinimumFractionDigits(fraction);
168
String result = fmt.format(number);
169
assertEquals(result, expected, "Incorrect formatting of number "
170
+ number + " using rounding mode: " + rounding);
171
}
172
173
}
174
175