ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedMapBash.java
Revision: 1.7
Committed: Sun Oct 18 06:58:01 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +5 -5 lines
Log Message:
fix paramterized test description strings

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.5 * @bug 4904067 5023830 7129185
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     import static org.testng.Assert.assertTrue;
40 jsr166 1.1
41     public class CheckedMapBash {
42 jsr166 1.5 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 jsr166 1.1
63 jsr166 1.5 {
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 jsr166 1.1 }
78    
79 jsr166 1.5 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 jsr166 1.1 for (int i=0; i<mapSize; i++)
105     if (m.put(new Integer(i), new Integer(2*i)) != null)
106 jsr166 1.5 fail("put returns a non-null value erroenously.");
107 jsr166 1.1 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 jsr166 1.5 fail("put returns a null value erroenously.");
112     Map m2 = supplier.get(); m2.putAll(m);
113 jsr166 1.1 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 jsr166 1.2 while (i.hasNext()) {
138 jsr166 1.1 i.next();
139     i.remove();
140     }
141     if (!m.isEmpty())
142     fail("Iterator.remove() failed");
143     }
144    
145 jsr166 1.5 @DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)
146     public static Iterator<Object[]> bashNavigableMapProvider() {
147     ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());
148     iters.ensureCapacity(numItr * iters.size());
149     for (int each=1; each < numItr; each++) {
150     iters.addAll( makeCheckedMaps());
151     }
152     return iters.iterator();
153     }
154    
155     @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
156     public static Iterator<Object[]> navigableMapProvider() {
157     return makeCheckedMaps().iterator();
158 jsr166 1.1 }
159    
160 jsr166 1.5 public static Collection<Object[]> makeCheckedMaps() {
161     return Arrays.asList(
162     new Object[]{"Collections.checkedMap(HashMap)",
163     (Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}},
164 jsr166 1.7 new Object[]{"Collections.checkedMap(TreeMap(reverseOrder))",
165 jsr166 1.5 (Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
166 jsr166 1.7 new Object[]{"Collections.checkedMap(TreeMap.descendingMap())",
167 jsr166 1.5 (Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},
168 jsr166 1.7 new Object[]{"Collections.checkedNavigableMap(TreeMap)",
169 jsr166 1.5 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},
170 jsr166 1.7 new Object[]{"Collections.checkedNavigableMap(TreeMap(reverseOrder))",
171 jsr166 1.5 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
172 jsr166 1.7 new Object[]{"Collections.checkedNavigableMap(TreeMap.descendingMap())",
173 jsr166 1.5 (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}
174     );
175 jsr166 1.1 }
176     }