1 |
/* |
2 |
* Copyright (c) 2003, 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 4904067 5023830 7129185 8072015 |
27 |
* @summary Unit test for Collections.checkedMap |
28 |
* @author Josh Bloch |
29 |
* @run testng CheckedMapBash |
30 |
* @key randomness |
31 |
*/ |
32 |
|
33 |
import org.testng.annotations.DataProvider; |
34 |
import org.testng.annotations.Test; |
35 |
|
36 |
import java.util.ArrayList; |
37 |
import java.util.Arrays; |
38 |
import java.util.Collection; |
39 |
import java.util.Collections; |
40 |
import java.util.HashMap; |
41 |
import java.util.Iterator; |
42 |
import java.util.Map; |
43 |
import java.util.Random; |
44 |
import java.util.Set; |
45 |
import java.util.TreeMap; |
46 |
import java.util.function.Supplier; |
47 |
|
48 |
import static org.testng.Assert.fail; |
49 |
|
50 |
public class CheckedMapBash { |
51 |
static final Random rnd = new Random(); |
52 |
static final Object nil = new Integer(0); |
53 |
static final int numItr = 100; |
54 |
static final int mapSize = 100; |
55 |
|
56 |
@Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>") |
57 |
public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) { |
58 |
Map m = supplier.get(); |
59 |
Object head = nil; |
60 |
|
61 |
for (int j=0; j<mapSize; j++) { |
62 |
Object newHead; |
63 |
do { |
64 |
newHead = new Integer(rnd.nextInt()); |
65 |
} while (m.containsKey(newHead) || newHead.equals(nil)); |
66 |
m.put(newHead, head); |
67 |
head = newHead; |
68 |
} |
69 |
if (m.size() != mapSize) |
70 |
fail("Size not as expected."); |
71 |
|
72 |
{ |
73 |
HashMap hm = new HashMap(m); |
74 |
if (! (hm.hashCode() == m.hashCode() && |
75 |
hm.entrySet().hashCode() == m.entrySet().hashCode() && |
76 |
hm.keySet().hashCode() == m.keySet().hashCode())) |
77 |
fail("Incorrect hashCode computation."); |
78 |
|
79 |
if (! (hm.equals(m) && |
80 |
hm.entrySet().equals(m.entrySet()) && |
81 |
hm.keySet().equals(m.keySet()) && |
82 |
m.equals(hm) && |
83 |
m.entrySet().equals(hm.entrySet()) && |
84 |
m.keySet().equals(hm.keySet()))) |
85 |
fail("Incorrect equals computation."); |
86 |
} |
87 |
|
88 |
Map m2 = supplier.get(); m2.putAll(m); |
89 |
m2.values().removeAll(m.keySet()); |
90 |
if (m2.size()!= 1 || !m2.containsValue(nil)) |
91 |
fail("Collection views test failed."); |
92 |
|
93 |
int j=0; |
94 |
while (head != nil) { |
95 |
if (!m.containsKey(head)) |
96 |
fail("Linked list doesn't contain a link."); |
97 |
Object newHead = m.get(head); |
98 |
if (newHead == null) |
99 |
fail("Could not retrieve a link."); |
100 |
m.remove(head); |
101 |
head = newHead; |
102 |
j++; |
103 |
} |
104 |
if (!m.isEmpty()) |
105 |
fail("Map nonempty after removing all links."); |
106 |
if (j != mapSize) |
107 |
fail("Linked list size not as expected."); |
108 |
} |
109 |
|
110 |
@Test(dataProvider = "Supplier<Map<Integer,Integer>>") |
111 |
public static void testCheckedMap2(String description, Supplier<Map<Integer,Integer>> supplier) { |
112 |
Map m = supplier.get(); |
113 |
for (int i=0; i<mapSize; i++) |
114 |
if (m.put(new Integer(i), new Integer(2*i)) != null) |
115 |
fail("put returns a non-null value erroneously."); |
116 |
for (int i=0; i<2*mapSize; i++) |
117 |
if (m.containsValue(new Integer(i)) != (i%2==0)) |
118 |
fail("contains value "+i); |
119 |
if (m.put(nil, nil) == null) |
120 |
fail("put returns a null value erroneously."); |
121 |
Map m2 = supplier.get(); m2.putAll(m); |
122 |
if (!m.equals(m2)) |
123 |
fail("Clone not equal to original. (1)"); |
124 |
if (!m2.equals(m)) |
125 |
fail("Clone not equal to original. (2)"); |
126 |
Set s = m.entrySet(), s2 = m2.entrySet(); |
127 |
if (!s.equals(s2)) |
128 |
fail("Clone not equal to original. (3)"); |
129 |
if (!s2.equals(s)) |
130 |
fail("Clone not equal to original. (4)"); |
131 |
if (!s.containsAll(s2)) |
132 |
fail("Original doesn't contain clone!"); |
133 |
if (!s2.containsAll(s)) |
134 |
fail("Clone doesn't contain original!"); |
135 |
|
136 |
s2.removeAll(s); |
137 |
if (!m2.isEmpty()) |
138 |
fail("entrySet().removeAll failed."); |
139 |
|
140 |
m2.putAll(m); |
141 |
m2.clear(); |
142 |
if (!m2.isEmpty()) |
143 |
fail("clear failed."); |
144 |
|
145 |
Iterator i = m.entrySet().iterator(); |
146 |
while (i.hasNext()) { |
147 |
i.next(); |
148 |
i.remove(); |
149 |
} |
150 |
if (!m.isEmpty()) |
151 |
fail("Iterator.remove() failed"); |
152 |
} |
153 |
|
154 |
@DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true) |
155 |
public static Iterator<Object[]> bashNavigableMapProvider() { |
156 |
ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps()); |
157 |
iters.ensureCapacity(numItr * iters.size()); |
158 |
for (int each=1; each < numItr; each++) { |
159 |
iters.addAll(makeCheckedMaps()); |
160 |
} |
161 |
return iters.iterator(); |
162 |
} |
163 |
|
164 |
@DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true) |
165 |
public static Iterator<Object[]> navigableMapProvider() { |
166 |
return makeCheckedMaps().iterator(); |
167 |
} |
168 |
|
169 |
public static Collection<Object[]> makeCheckedMaps() { |
170 |
Object[][] params = { |
171 |
{"Collections.checkedMap(HashMap)", |
172 |
(Supplier) () -> Collections.checkedMap(new HashMap(), Integer.class, Integer.class)}, |
173 |
{"Collections.checkedMap(TreeMap(reverseOrder))", |
174 |
(Supplier) () -> Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)}, |
175 |
{"Collections.checkedMap(TreeMap.descendingMap())", |
176 |
(Supplier) () -> Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class)}, |
177 |
{"Collections.checkedNavigableMap(TreeMap)", |
178 |
(Supplier) () -> Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class)}, |
179 |
{"Collections.checkedNavigableMap(TreeMap(reverseOrder))", |
180 |
(Supplier) () -> Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class)}, |
181 |
{"Collections.checkedNavigableMap(TreeMap.descendingMap())", |
182 |
(Supplier) () -> Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class)}, |
183 |
}; |
184 |
return Arrays.asList(params); |
185 |
} |
186 |
} |