ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedMapBash.java
Revision: 1.11
Committed: Sat Oct 21 06:47:09 2017 UTC (6 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +0 -1 lines
Log Message:
fix unused import

File Contents

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