1 /*
2 * Copyright (c) 2013, 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
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 private static void realMain(String[] args) throws Throwable {
42 Map<Integer, Long>[] maps = (Map<Integer, Long>[]) new Map[]{
43 new HashMap<>(),
44 new Hashtable<>(),
45 new IdentityHashMap<>(),
46 new LinkedHashMap<>(),
47 new TreeMap<>(),
48 new WeakHashMap<>(),
49 new ConcurrentHashMap<>(),
50 new ConcurrentSkipListMap<>()
51 };
52
53 // for each map type.
54 for (Map<Integer, Long> map : maps) {
55 try {
56 testMap(map);
57 } catch(Exception all) {
58 unexpected("Failed for " + map.getClass().getName(), all);
59 }
60 }
61 }
62
63 private static final Integer[] KEYS = new Integer[TEST_SIZE];
64
65 private static final Long[] VALUES = new Long[TEST_SIZE];
66
67 static {
68 for (int each = 0; each < TEST_SIZE; each++) {
69 KEYS[each] = Integer.valueOf(each);
70 VALUES[each] = Long.valueOf(each + TEST_SIZE);
71 }
72 }
73
74
75 private static void testMap(Map<Integer, Long> map) {
76 System.out.println("Testing " + map.getClass());
77 System.out.flush();
78
79 // Fill the map
80 for (int each = 0; each < TEST_SIZE; each++) {
81 map.put(KEYS[each], VALUES[each]);
82 }
83
84 // check the keys
85 Object[] keys = map.keySet().toArray();
86 Arrays.sort(keys);
87
88 for(int each = 0; each < TEST_SIZE; each++) {
89 check( "unexpected key", keys[each] == KEYS[each]);
90 }
91
92 // check the values
93 Object[] values = map.values().toArray();
94 Arrays.sort(values);
95
96 for(int each = 0; each < TEST_SIZE; each++) {
97 check( "unexpected value", values[each] == VALUES[each]);
98 }
99
100 // check the entries
101 Map.Entry<Integer,Long>[] entries = map.entrySet().toArray(new Map.Entry[TEST_SIZE]);
102 Arrays.sort( entries,new Comparator<Map.Entry<Integer,Long>>() {
103 public int compare(Map.Entry<Integer,Long> o1, Map.Entry<Integer,Long> o2) {
104 return o1.getKey().compareTo(o2.getKey());
105 }});
106
107 for(int each = 0; each < TEST_SIZE; each++) {
108 check( "unexpected entry", entries[each].getKey() == KEYS[each] && entries[each].getValue() == VALUES[each]);
109 }
110 }
111
112 //--------------------- Infrastructure ---------------------------
113 static volatile int passed = 0, failed = 0;
114
115 static void pass() {
116 passed++;
117 }
118
119 static void fail() {
120 failed++;
121 (new Error("Failure")).printStackTrace(System.err);
122 }
123
|
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
|