< prev index next >

test/java/util/Calendar/NarrowNamesTest.java

Print this page




  69                 "Wed",       // abb Wednesday
  70                 "Thu",       // abb Thursday
  71                 "Fri",       // abb Friday
  72                 "Sat"        // abb Saturday
  73                 );
  74         testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null
  75         testMap(US, AM_PM, ALL_STYLES,
  76                 "AM", "PM",
  77                 RESET_INDEX,
  78                 "a", "p");
  79         testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE); // expect null
  80         testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT,
  81                 "", // 1-based indexing for DAY_OF_WEEK
  82                 "\u65e5",
  83                 "\u6708",
  84                 "\u706b",
  85                 "\u6c34",
  86                 "\u6728",
  87                 "\u91d1",
  88                 "\u571f");
  89         testMap(THTH, MONTH, NARROW_FORMAT); // expect null












  90         testMap(THTH, MONTH, NARROW_STANDALONE,
  91                 "\u0e21.\u0e04.",
  92                 "\u0e01.\u0e1e.",
  93                 "\u0e21\u0e35.\u0e04.",
  94                 "\u0e40\u0e21.\u0e22.",
  95                 "\u0e1e.\u0e04.",
  96                 "\u0e21\u0e34.\u0e22.",
  97                 "\u0e01.\u0e04.",
  98                 "\u0e2a.\u0e04.",
  99                 "\u0e01.\u0e22.",
 100                 "\u0e15.\u0e04.",
 101                 "\u0e1e.\u0e22.",
 102                 "\u0e18.\u0e04.");
 103 
 104         if (errors != 0) {
 105             throw new RuntimeException("test failed");
 106         }
 107     }
 108 
 109     private static void test(Locale locale, int field, String expected, int... data) {


 129     private static void testMap(Locale locale, int field, int style, String... expected) {
 130         Map<String, Integer> expectedMap = null;
 131         if (expected.length > 0) {
 132             expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);
 133             int index = 0;
 134             for (int i = 0; i < expected.length; i++) {
 135                 if (expected[i].isEmpty()) {
 136                     index++;
 137                     continue;
 138                 }
 139                 if (expected[i] == RESET_INDEX) {
 140                     index = 0;
 141                     continue;
 142                 }
 143                 expectedMap.put(expected[i], index++);
 144             }
 145         }
 146         Calendar cal = Calendar.getInstance(locale);
 147         Map<String, Integer> got = cal.getDisplayNames(field, style, locale);
 148         if (!(expectedMap == null && got == null)
 149             && !expectedMap.equals(got)) {
 150             System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",
 151                               locale, field, style, expectedMap, got);
 152             errors++;
 153         }
 154     }
 155 
 156     /**
 157      * Comparator implementation for TreeMap which iterates keys from longest
 158      * to shortest.
 159      */
 160     private static class LengthBasedComparator implements Comparator<String> {
 161         private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
 162 
 163         private LengthBasedComparator() {
 164         }
 165 
 166         @Override
 167         public int compare(String o1, String o2) {
 168             int n = o2.length() - o1.length();
 169             return (n == 0) ? o1.compareTo(o2) : n;


  69                 "Wed",       // abb Wednesday
  70                 "Thu",       // abb Thursday
  71                 "Fri",       // abb Friday
  72                 "Sat"        // abb Saturday
  73                 );
  74         testMap(US, DAY_OF_WEEK, NARROW_FORMAT); // expect null
  75         testMap(US, AM_PM, ALL_STYLES,
  76                 "AM", "PM",
  77                 RESET_INDEX,
  78                 "a", "p");
  79         testMap(JAJPJP, DAY_OF_WEEK, NARROW_STANDALONE); // expect null
  80         testMap(JAJPJP, DAY_OF_WEEK, NARROW_FORMAT,
  81                 "", // 1-based indexing for DAY_OF_WEEK
  82                 "\u65e5",
  83                 "\u6708",
  84                 "\u706b",
  85                 "\u6c34",
  86                 "\u6728",
  87                 "\u91d1",
  88                 "\u571f");
  89         testMap(THTH, MONTH, NARROW_FORMAT,
  90                 "\u0e21.\u0e04.",
  91                 "\u0e01.\u0e1e.",
  92                 "\u0e21\u0e35.\u0e04.",
  93                 "\u0e40\u0e21.\u0e22.",
  94                 "\u0e1e.\u0e04.",
  95                 "\u0e21\u0e34.\u0e22",  // no last dot
  96                 "\u0e01.\u0e04.",
  97                 "\u0e2a.\u0e04.",
  98                 "\u0e01.\u0e22.",
  99                 "\u0e15.\u0e04.",
 100                 "\u0e1e.\u0e22.",
 101                 "\u0e18.\u0e04.");
 102         testMap(THTH, MONTH, NARROW_STANDALONE,
 103                 "\u0e21.\u0e04.",
 104                 "\u0e01.\u0e1e.",
 105                 "\u0e21\u0e35.\u0e04.",
 106                 "\u0e40\u0e21.\u0e22.",
 107                 "\u0e1e.\u0e04.",
 108                 "\u0e21\u0e34.\u0e22.",
 109                 "\u0e01.\u0e04.",
 110                 "\u0e2a.\u0e04.",
 111                 "\u0e01.\u0e22.",
 112                 "\u0e15.\u0e04.",
 113                 "\u0e1e.\u0e22.",
 114                 "\u0e18.\u0e04.");
 115 
 116         if (errors != 0) {
 117             throw new RuntimeException("test failed");
 118         }
 119     }
 120 
 121     private static void test(Locale locale, int field, String expected, int... data) {


 141     private static void testMap(Locale locale, int field, int style, String... expected) {
 142         Map<String, Integer> expectedMap = null;
 143         if (expected.length > 0) {
 144             expectedMap = new TreeMap<>(LengthBasedComparator.INSTANCE);
 145             int index = 0;
 146             for (int i = 0; i < expected.length; i++) {
 147                 if (expected[i].isEmpty()) {
 148                     index++;
 149                     continue;
 150                 }
 151                 if (expected[i] == RESET_INDEX) {
 152                     index = 0;
 153                     continue;
 154                 }
 155                 expectedMap.put(expected[i], index++);
 156             }
 157         }
 158         Calendar cal = Calendar.getInstance(locale);
 159         Map<String, Integer> got = cal.getDisplayNames(field, style, locale);
 160         if (!(expectedMap == null && got == null)
 161             && !(expectedMap != null && expectedMap.equals(got))) {
 162             System.err.printf("testMap: locale=%s, field=%d, style=%d, expected=%s, got=%s%n",
 163                               locale, field, style, expectedMap, got);
 164             errors++;
 165         }
 166     }
 167 
 168     /**
 169      * Comparator implementation for TreeMap which iterates keys from longest
 170      * to shortest.
 171      */
 172     private static class LengthBasedComparator implements Comparator<String> {
 173         private static final LengthBasedComparator INSTANCE = new LengthBasedComparator();
 174 
 175         private LengthBasedComparator() {
 176         }
 177 
 178         @Override
 179         public int compare(String o1, String o2) {
 180             int n = o2.length() - o1.length();
 181             return (n == 0) ? o1.compareTo(o2) : n;
< prev index next >