Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Calendar/NarrowNamesTest.java
47182 views
/*1* Copyright (c) 2012, 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*/2223import java.time.LocalDateTime;24import java.util.*;25import static java.util.GregorianCalendar.*;2627public class NarrowNamesTest {28private static final Locale US = Locale.US;29private static final Locale JAJPJP = new Locale("ja", "JP", "JP");30private static final Locale THTH = new Locale("th", "TH");3132private static final String RESET_INDEX = "RESET_INDEX";3334private static int errors = 0;3536// This test is locale data-dependent.37public static void main(String[] args) {38test(US, ERA, "B",39ERA, BC, YEAR, 1);40test(US, ERA, "A",41ERA, AD, YEAR, 2012);42test(US, DAY_OF_WEEK, "S",43YEAR, 2012, MONTH, DECEMBER, DAY_OF_MONTH, 23);44test(US, AM_PM, "a",45HOUR_OF_DAY, 10);46test(US, AM_PM, "p",47HOUR_OF_DAY, 23);48test(JAJPJP, DAY_OF_WEEK,49LocalDateTime.now().isBefore(LocalDateTime.of(2019, 5, 1, 0, 0)) ?50"\u65e5" : "\u706b", // "Sun" for HEISEI, "Tue" for REIWA51YEAR, 24, MONTH, DECEMBER, DAY_OF_MONTH, 23);52test(THTH, MONTH, NARROW_STANDALONE, "\u0e18.\u0e04.",53YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);54test(THTH, DAY_OF_WEEK, "\u0e1e",55YEAR, 2555, MONTH, DECEMBER, DAY_OF_MONTH, 5);5657testMap(US, DAY_OF_WEEK, ALL_STYLES, // shouldn't include any narrow names58"", // 1-based indexing for DAY_OF_WEEK59"Sunday", // Sunday60"Monday", // Monday61"Tuesday", // Tuesday62"Wednesday", // Wednesday63"Thursday", // Thursday64"Friday", // Friday65"Saturday", // Saturday66RESET_INDEX,67"", // 1-based indexing for DAY_OF_WEEK68"Sun", // abb Sunday69"Mon", // abb Monday70"Tue", // abb Tuesday71"Wed", // abb Wednesday72"Thu", // abb Thursday73"Fri", // abb Friday74"Sat" // abb Saturday75);76testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null77testMap(US, AM_PM, ALL_STYLES,78"AM", "PM",79RESET_INDEX,80"a", "p");81testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE); // expect null82testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT,83"", // 1-based indexing for DAY_OF_WEEK84"\u65e5",85"\u6708",86"\u706b",87"\u6c34",88"\u6728",89"\u91d1",90"\u571f");91testMap(THTH, MONTH, NARROW_FORMAT,92"\u0e21.\u0e04.",93"\u0e01.\u0e1e.",94"\u0e21\u0e35.\u0e04.",95"\u0e40\u0e21.\u0e22.",96"\u0e1e.\u0e04.",97"\u0e21\u0e34.\u0e22", // no last dot98"\u0e01.\u0e04.",99"\u0e2a.\u0e04.",100"\u0e01.\u0e22.",101"\u0e15.\u0e04.",102"\u0e1e.\u0e22.",103"\u0e18.\u0e04.");104testMap(THTH, MONTH, NARROW_STANDALONE,105"\u0e21.\u0e04.",106"\u0e01.\u0e1e.",107"\u0e21\u0e35.\u0e04.",108"\u0e40\u0e21.\u0e22.",109"\u0e1e.\u0e04.",110"\u0e21\u0e34.\u0e22.",111"\u0e01.\u0e04.",112"\u0e2a.\u0e04.",113"\u0e01.\u0e22.",114"\u0e15.\u0e04.",115"\u0e1e.\u0e22.",116"\u0e18.\u0e04.");117118if (errors != 0) {119throw new RuntimeException("test failed");120}121}122123private static void test(Locale locale, int field, String expected, int... data) {124test(locale, field, NARROW_FORMAT, expected, data);125}126127private static void test(Locale locale, int field, int style, String expected, int... fieldValuePairs) {128Calendar cal = Calendar.getInstance(locale);129cal.clear();130for (int i = 0; i < fieldValuePairs.length;) {131int f = fieldValuePairs[i++];132int v = fieldValuePairs[i++];133cal.set(f, v);134}135String got = cal.getDisplayName(field, style, locale);136if (!expected.equals(got)) {137System.err.printf("test: locale=%s, field=%d, value=%d, style=%d, got=\"%s\", expected=\"%s\"%n",138locale, field, cal.get(field), style, got, expected);139errors++;140}141}142143private static void testMap(Locale locale, int field, int style, String... expected) {144Map<String, Integer> expectedMap = null;145if (expected.length > 0) {146expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);147int index = 0;148for (int i = 0; i < expected.length; i++) {149if (expected[i].isEmpty()) {150index++;151continue;152}153if (expected[i] == RESET_INDEX) {154index = 0;155continue;156}157expectedMap.put(expected[i], index++);158}159}160Calendar cal = Calendar.getInstance(locale);161Map<String, Integer> got = cal.getDisplayNames(field, style, locale);162if (!(expectedMap == null && got == null)163&& !(expectedMap != null && expectedMap.equals(got))) {164System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",165locale, field, style, expectedMap, got);166errors++;167}168}169170/**171* Comparator implementation for TreeMap which iterates keys from longest172* to shortest.173*/174private static class LengthBasedComparator implements Comparator<String> {175private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();176177private LengthBasedComparator() {178}179180@Override181public int compare(String o1, String o2) {182int n = o2.length() - o1.length();183return (n == 0) ? o1.compareTo(o2) : n;184}185}186}187188189