ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedMapBash.java
Revision: 1.5
Committed: Wed Sep 9 21:47:59 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +100 -72 lines
Log Message:
merge upstream changes

File Contents

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