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 package test.java.util.Map;
31 
32 import java.util.*;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentSkipListMap;
35 
36 import org.testng.annotations.Test;
37 import org.testng.Assert;
38 
39 public class ToArray {
40 
41     /**
42      * Number of elements per map.
43      */
44     private static final int TEST_SIZE = 5000;
45 
46     @Test
testToArray()47     public void testToArray() throws Throwable {
48         Map<Integer, Long>[] maps = (Map<Integer, Long>[]) new Map[]{
49                 new HashMap<>(),
50                 new Hashtable<>(),
51                 new IdentityHashMap<>(),
52                 new LinkedHashMap<>(),
53                 new TreeMap<>(),
54                 new WeakHashMap<>(),
55                 new ConcurrentHashMap<>(),
56                 new ConcurrentSkipListMap<>()
57         };
58 
59         // for each map type.
60         for (Map<Integer, Long> map : maps) {
61             testMap(map);
62         }
63     }
64 
65     private static final Integer[] KEYS = new Integer[TEST_SIZE];
66 
67     private static final Long[] VALUES = new Long[TEST_SIZE];
68 
69     static {
70         for (int each = 0; each < TEST_SIZE; each++) {
71             KEYS[each]   = Integer.valueOf(each);
72             VALUES[each] = Long.valueOf(each + TEST_SIZE);
73         }
74     }
75 
76 
testMap(Map<Integer, Long> map)77     private static void testMap(Map<Integer, Long> map) {
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             Assert.assertTrue( 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             Assert.assertTrue( 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             Assert.assertTrue( entries[each].getKey() == KEYS[each] && entries[each].getValue() == VALUES[each]);
109         }
110     }
111 }