Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestEquality.java
41153 views
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 8222756
26
* @summary Checks the equals and hashCode method of CompactNumberFormat
27
* @modules jdk.localedata
28
* @run testng/othervm TestEquality
29
*
30
*/
31
32
import java.text.CompactNumberFormat;
33
import java.text.DecimalFormatSymbols;
34
import java.text.NumberFormat;
35
import java.util.Locale;
36
import org.testng.annotations.Test;
37
38
public class TestEquality {
39
40
@Test
41
public void testEquality() {
42
CompactNumberFormat cnf1 = (CompactNumberFormat) NumberFormat
43
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
44
45
CompactNumberFormat cnf2 = (CompactNumberFormat) NumberFormat
46
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
47
48
// A custom compact instance with the same state as
49
// compact number instance of "en_US" locale with SHORT style
50
String decimalPattern = "#,##0.###";
51
String[] compactPatterns = new String[]{
52
"",
53
"",
54
"",
55
"{one:0K other:0K}",
56
"{one:00K other:00K}",
57
"{one:000K other:000K}",
58
"{one:0M other:0M}",
59
"{one:00M other:00M}",
60
"{one:000M other:000M}",
61
"{one:0B other:0B}",
62
"{one:00B other:00B}",
63
"{one:000B other:000B}",
64
"{one:0T other:0T}",
65
"{one:00T other:00T}",
66
"{one:000T other:000T}"
67
};
68
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
69
CompactNumberFormat cnf3 =
70
new CompactNumberFormat(decimalPattern, symbols, compactPatterns, "one:i = 1 and v = 0");
71
72
// A compact instance created with different decimalPattern than cnf3
73
CompactNumberFormat cnf4 = new CompactNumberFormat("#,#0.0#", symbols, compactPatterns);
74
75
// A compact instance created with different format symbols than cnf3
76
CompactNumberFormat cnf5 = new CompactNumberFormat(decimalPattern,
77
DecimalFormatSymbols.getInstance(Locale.JAPAN), compactPatterns);
78
79
// A compact instance created with different compact patterns than cnf3
80
CompactNumberFormat cnf6 = new CompactNumberFormat(decimalPattern,
81
symbols, new String[]{"", "", "", "0K", "00K", "000K"});
82
83
// Checking reflexivity
84
if (!cnf1.equals(cnf1)) {
85
throw new RuntimeException("[testEquality() reflexivity FAILED: The compared"
86
+ " objects must be equal]");
87
}
88
89
// Checking symmetry, checking equality of two same objects
90
if (!cnf1.equals(cnf2) || !cnf2.equals(cnf1)) {
91
throw new RuntimeException("[testEquality() symmetry FAILED: The compared"
92
+ " objects must be equal]");
93
}
94
95
// Checking transitivity, three objects must be equal
96
if (!cnf1.equals(cnf2) || !cnf2.equals(cnf3) || !cnf1.equals(cnf3)) {
97
throw new RuntimeException("[testEquality() transitivity FAILED: The compared"
98
+ " objects must be equal]");
99
}
100
101
// Objects must not be equal as the decimalPattern is different
102
checkEquals(cnf3, cnf4, false, "1st", "different decimal pattern");
103
104
// Objects must not be equal as the format symbols instance is different
105
checkEquals(cnf3, cnf5, false, "2nd", "different format symbols");
106
107
// Objects must not be equal as the compact patters are different
108
checkEquals(cnf3, cnf6, false, "3rd", "different compact patterns");
109
110
// Changing the min integer digits of first object; objects must not
111
// be equal
112
cnf1.setMinimumIntegerDigits(5);
113
checkEquals(cnf1, cnf2, false, "4th", "different min integer digits");
114
115
// Changing the min integer digits of second object; objects must
116
// be equal
117
cnf2.setMinimumIntegerDigits(5);
118
checkEquals(cnf1, cnf2, true, "5th", "");
119
120
// Changing the grouping size of first object; objects must not
121
// be equal
122
cnf1.setGroupingSize(4);
123
checkEquals(cnf1, cnf2, false, "6th", "different grouping size");
124
125
// Changing the grouping size if second object; objects must be equal
126
cnf2.setGroupingSize(4);
127
checkEquals(cnf1, cnf2, true, "7th", "");
128
129
// Changing the parseBigDecimal of first object; objects must not
130
// be equal
131
cnf1.setParseBigDecimal(true);
132
checkEquals(cnf1, cnf2, false, "8th", "different parse big decimal");
133
134
}
135
136
private void checkEquals(CompactNumberFormat cnf1, CompactNumberFormat cnf2,
137
boolean mustEqual, String nthComparison, String message) {
138
if (cnf1.equals(cnf2) != mustEqual) {
139
if (mustEqual) {
140
throw new RuntimeException("[testEquality() " + nthComparison
141
+ " comparison FAILED: The compared objects must be equal]");
142
} else {
143
throw new RuntimeException("[testEquality() " + nthComparison
144
+ " comparison FAILED: The compared objects must"
145
+ " not be equal because of " + message + "]");
146
}
147
}
148
}
149
150
@Test
151
public void testHashCode() {
152
NumberFormat cnf1 = NumberFormat
153
.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
154
NumberFormat cnf2 = NumberFormat
155
.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
156
157
if (cnf1.hashCode() != cnf2.hashCode()) {
158
throw new RuntimeException("[testHashCode() FAILED: hashCode of the"
159
+ " compared objects must match]");
160
}
161
}
162
163
// Test the property of equals and hashCode i.e. two equal object must
164
// always have the same hashCode
165
@Test
166
public void testEqualsAndHashCode() {
167
NumberFormat cnf1 = NumberFormat
168
.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);
169
cnf1.setMinimumIntegerDigits(5);
170
NumberFormat cnf2 = NumberFormat
171
.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.SHORT);
172
cnf2.setMinimumIntegerDigits(5);
173
if (cnf1.equals(cnf2)) {
174
if (cnf1.hashCode() != cnf2.hashCode()) {
175
throw new RuntimeException("[testEqualsAndHashCode() FAILED: two"
176
+ " equal objects must have same hashCode]");
177
}
178
} else {
179
throw new RuntimeException("[testEqualsAndHashCode() FAILED: The"
180
+ " compared objects must be equal]");
181
}
182
}
183
184
}
185
186