1 /*
  2  * Copyright (c) 2013, 2024, 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 8008785 8336669
 27  * @summary Ensure toArray() implementations return correct results.
 28  * @author Mike Duigou
 29  */
 30 import java.util.*;
 31 import java.util.concurrent.ConcurrentHashMap;
 32 import java.util.concurrent.ConcurrentSkipListMap;
 33 
 34 public class ToArray {
 35 
 36     /**
 37      * Number of elements per map.
 38      */
 39     private static final int TEST_SIZE = 5000;
 40 
 41     static String genString(int i) {
 42         return "BASE-" + HexFormat.of().toHexDigits(i);
 43     }
 44 
 45     private static void realMain(String[] args) throws Throwable {
 46         Map<String, Long>[] maps = (Map<String, Long>[]) new Map<?,?>[]{
 47                     new HashMap<>(),
 48                     new Hashtable<>(),
 49                     new IdentityHashMap<>(),
 50                     new LinkedHashMap<>(),
 51                     new TreeMap<>(),
 52                     new WeakHashMap<>(),
 53                     new ConcurrentHashMap<>(),
 54                     new ConcurrentSkipListMap<>()
 55                 };
 56 
 57         // for each map type.
 58         for (Map<String, Long> map : maps) {
 59              try {
 60                 testMap(map);
 61              } catch(Exception all) {
 62                 unexpected("Failed for " + map.getClass().getName(), all);
 63              }
 64         }
 65     }
 66 
 67     private static final String[] KEYS = new String[TEST_SIZE];
 68 
 69     private static final Long[] VALUES = new Long[TEST_SIZE];
 70 
 71     static {
 72         for (int each = 0; each < TEST_SIZE; each++) {
 73             // BUG: WeakHashMap of value object!
 74             KEYS[each]   = genString(each);
 75             VALUES[each] = Long.valueOf(each + TEST_SIZE);
 76         }
 77     }
 78 
 79 
 80     private static void testMap(Map<String, Long> map) {
 81         System.out.println("Testing " + map.getClass());
 82         System.out.flush();
 83 
 84         // Fill the map
 85         for (int each = 0; each < TEST_SIZE; each++) {
 86             map.put(KEYS[each], VALUES[each]);
 87         }
 88 
 89         // check the keys
 90         Object[] keys = map.keySet().toArray();
 91         Arrays.sort(keys);
 92 
 93         for(int each = 0; each < TEST_SIZE; each++) {
 94             check( "unexpected key", keys[each] == KEYS[each]);
 95         }
 96 
 97         // check the values
 98         Object[] values = map.values().toArray();
 99         Arrays.sort(values);
100 
101         for(int each = 0; each < TEST_SIZE; each++) {
102             check( "unexpected value", values[each] == VALUES[each]);
103         }
104 
105         // check the entries
106         Map.Entry<String,Long>[] entries = map.entrySet().toArray(new Map.Entry[TEST_SIZE]);
107         Arrays.sort( entries,new Comparator<Map.Entry<String,Long>>() {
108                 public int compare(Map.Entry<String,Long> o1, Map.Entry<String,Long> o2) {
109                         return o1.getKey().compareTo(o2.getKey());
110                 }});
111 
112         for(int each = 0; each < TEST_SIZE; each++) {
113             check( "unexpected entry", entries[each].getKey() == KEYS[each] && entries[each].getValue() == VALUES[each]);
114         }
115     }
116 
117     //--------------------- Infrastructure ---------------------------
118     static volatile int passed = 0, failed = 0;
119 
120     static void pass() {
121         passed++;
122     }
123 
124     static void fail() {
125         failed++;
126         (new Error("Failure")).printStackTrace(System.err);
127     }
128 
129     static void fail(String msg) {
130         failed++;
131         (new Error("Failure: " + msg)).printStackTrace(System.err);
132     }
133 
134     static void abort() {
135         fail();
136         System.exit(1);
137     }
138 
139     static void abort(String msg) {
140         fail(msg);
141         System.exit(1);
142     }
143 
144     static void unexpected(String msg, Throwable t) {
145         System.err.println("Unexpected: " + msg);
146         unexpected(t);
147     }
148 
149     static void unexpected(Throwable t) {
150         failed++;
151         t.printStackTrace(System.err);
152     }
153 
154     static void check(boolean cond) {
155         if (cond) {
156             pass();
157         } else {
158             fail();
159         }
160     }
161 
162     static void check(String desc, boolean cond) {
163         if (cond) {
164             pass();
165         } else {
166             fail(desc);
167         }
168     }
169 
170     static void equal(Object x, Object y) {
171         if (Objects.equals(x, y)) {
172             pass();
173         } else {
174             fail(x + " not equal to " + y);
175         }
176     }
177 
178     public static void main(String[] args) throws Throwable {
179         Thread.currentThread().setName(ToArray.class.getName());
180 //        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
181         try {
182             realMain(args);
183         } catch (Throwable t) {
184             unexpected(t);
185         }
186 
187         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
188         if (failed > 0) {
189             throw new Error("Some tests failed");
190         }
191     }
192 }