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