Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Calendar/NarrowNamesTest.java
41149 views
1
/*
2
* Copyright (c) 2012, 2021, 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
/*
25
* @test
26
* @bug 8000983 8008577 8247781 8262108
27
* @summary Unit test for narrow names support. This test is locale data-dependent
28
* and assumes that both COMPAT and CLDR have the same narrow names if not
29
* explicitly specified.
30
* @modules jdk.localedata
31
* @comment Locale providers: COMPAT,SPI
32
* @run main/othervm -Djava.locale.providers=COMPAT,SPI NarrowNamesTest COMPAT,SPI
33
* @comment Locale providers: CLDR
34
* @run main/othervm -Djava.locale.providers=CLDR NarrowNamesTest CLDR
35
*/
36
37
import java.time.LocalDateTime;
38
import java.util.Calendar;
39
import java.util.Comparator;
40
import java.util.Locale;
41
import java.util.Map;
42
import java.util.TreeMap;
43
import static java.util.GregorianCalendar.*;
44
45
public class NarrowNamesTest {
46
private static final Locale US = Locale.US;
47
private static final Locale JAJPJP = new Locale("ja", "JP", "JP");
48
private static final Locale THTH = new Locale("th", "TH");
49
50
private static final String RESET_INDEX = "RESET_INDEX";
51
52
private static int errors = 0;
53
54
// This test is locale data-dependent.
55
public static void main(String[] args) {
56
String providers = args[0];
57
58
test(US, ERA, "B",
59
ERA, BC, YEAR, 1);
60
test(US, ERA, "A",
61
ERA, AD, YEAR, 2012);
62
test(US, DAY_OF_WEEK, "S",
63
YEAR, 2012, MONTH, DECEMBER, DAY_OF_MONTH, 23);
64
test(US, AM_PM, "a",
65
HOUR_OF_DAY, 10);
66
test(US, AM_PM, "p",
67
HOUR_OF_DAY, 23);
68
test(JAJPJP, DAY_OF_WEEK,
69
LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0)) ?
70
"\u65e5" : "\u706b", // "Sun" for HEISEI, "Tue" for REIWA
71
YEAR, 24, MONTH, DECEMBER, DAY_OF_MONTH, 23);
72
test(THTH, MONTH, NARROW_STANDALONE, "\u0e18.\u0e04.",
73
YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);
74
test(THTH, DAY_OF_WEEK, "\u0e1e",
75
YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);
76
77
testMap(US, DAY_OF_WEEK, ALL_STYLES, // shouldn't include any narrow names
78
"", // 1-based indexing for DAY_OF_WEEK
79
"Sunday", // Sunday
80
"Monday", // Monday
81
"Tuesday", // Tuesday
82
"Wednesday", // Wednesday
83
"Thursday", // Thursday
84
"Friday", // Friday
85
"Saturday", // Saturday
86
RESET_INDEX,
87
"", // 1-based indexing for DAY_OF_WEEK
88
"Sun", // abb Sunday
89
"Mon", // abb Monday
90
"Tue", // abb Tuesday
91
"Wed", // abb Wednesday
92
"Thu", // abb Thursday
93
"Fri", // abb Friday
94
"Sat" // abb Saturday
95
);
96
testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null
97
testMap(US, AM_PM, ALL_STYLES,
98
"AM", "PM",
99
RESET_INDEX,
100
"a", "p");
101
testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE,
102
"", // 1-based indexing for DAY_OF_WEEK
103
"\u65e5",
104
"\u6708",
105
"\u706b",
106
"\u6c34",
107
"\u6728",
108
"\u91d1",
109
"\u571f");
110
testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT,
111
"", // 1-based indexing for DAY_OF_WEEK
112
"\u65e5",
113
"\u6708",
114
"\u706b",
115
"\u6c34",
116
"\u6728",
117
"\u91d1",
118
"\u571f");
119
testMap(THTH, MONTH, NARROW_FORMAT,
120
"\u0e21.\u0e04.",
121
"\u0e01.\u0e1e.",
122
"\u0e21\u0e35.\u0e04.",
123
"\u0e40\u0e21.\u0e22.",
124
"\u0e1e.\u0e04.",
125
(providers.startsWith("CLDR") ?
126
"\u0e21\u0e34.\u0e22." :
127
"\u0e21\u0e34.\u0e22"), // no last dot in JRE
128
"\u0e01.\u0e04.",
129
"\u0e2a.\u0e04.",
130
"\u0e01.\u0e22.",
131
"\u0e15.\u0e04.",
132
"\u0e1e.\u0e22.",
133
"\u0e18.\u0e04.");
134
testMap(THTH, MONTH, NARROW_STANDALONE,
135
"\u0e21.\u0e04.",
136
"\u0e01.\u0e1e.",
137
"\u0e21\u0e35.\u0e04.",
138
"\u0e40\u0e21.\u0e22.",
139
"\u0e1e.\u0e04.",
140
"\u0e21\u0e34.\u0e22.",
141
"\u0e01.\u0e04.",
142
"\u0e2a.\u0e04.",
143
"\u0e01.\u0e22.",
144
"\u0e15.\u0e04.",
145
"\u0e1e.\u0e22.",
146
"\u0e18.\u0e04.");
147
148
if (errors != 0) {
149
throw new RuntimeException("test failed");
150
}
151
}
152
153
private static void test(Locale locale, int field, String expected, int... data) {
154
test(locale, field, NARROW_FORMAT, expected, data);
155
}
156
157
private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) {
158
Calendar cal = Calendar.getInstance(locale);
159
cal.clear();
160
for (int i = 0; i < fieldValuePairs.length;) {
161
int f = fieldValuePairs[i++];
162
int v = fieldValuePairs[i++];
163
cal.set(f, v);
164
}
165
String got = cal.getDisplayName(field, style, locale);
166
if (!expected.equals(got)) {
167
System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n",
168
locale, field, cal.get(field), style, got, expected);
169
errors++;
170
}
171
}
172
173
private static void testMap(Locale locale, int field, int style, String... expected) {
174
Map<String, Integer> expectedMap = null;
175
if (expected.length > 0) {
176
expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);
177
int index = 0;
178
for (String s : expected) {
179
if (s.isEmpty()) {
180
index++;
181
continue;
182
}
183
if (s == RESET_INDEX) {
184
index = 0;
185
continue;
186
}
187
expectedMap.put(s, index++);
188
}
189
}
190
Calendar cal = Calendar.getInstance(locale);
191
Map<String, Integer> got = cal.getDisplayNames(field, style, locale);
192
if (!(expectedMap == null && got == null)
193
&& !(expectedMap != null && expectedMap.equals(got))) {
194
System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",
195
locale, field, style, expectedMap, got);
196
errors++;
197
}
198
}
199
200
/**
201
* Comparator implementation for TreeMap which iterates keys from longest
202
* to shortest.
203
*/
204
private static class LengthBasedComparator implements Comparator<String> {
205
private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
206
207
private LengthBasedComparator() {
208
}
209
210
@Override
211
public int compare(String o1, String o2) {
212
int n = o2.length() - o1.length();
213
return (n == 0) ? o1.compareTo(o2) : n;
214
}
215
}
216
}
217
218