CheckedMapBash.java revision 9330:8b1f1c2a400f
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 */
31
32import java.util.*;
33import java.util.function.Supplier;
34import org.testng.annotations.Test;
35import org.testng.annotations.DataProvider;
36
37import static org.testng.Assert.fail;
38import static org.testng.Assert.assertTrue;
39
40public class CheckedMapBash {
41    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            } while (m.containsKey(newHead));
56            m.put(newHead, head);
57            head = newHead;
58        }
59        if (m.size() != mapSize)
60            fail("Size not as expected.");
61
62        {
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        }
77
78        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    public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) {
102        Map m = supplier.get();
103        for (int i=0; i<mapSize; i++)
104            if (m.put(new Integer(i), new Integer(2*i)) != null)
105                fail("put returns a non-null value erroenously.");
106        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            fail("put returns a null value erroenously.");
111        Map m2 = supplier.get(); m2.putAll(m);
112        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        while(i.hasNext()) {
137            i.next();
138            i.remove();
139        }
140        if (!m.isEmpty())
141            fail("Iterator.remove() failed");
142    }
143
144
145    @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    }
159
160    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            new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)",
165                (Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
166            new Object[]{"Collections.checkedMap(TreeSet).descendingSet()",
167                (Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},
168            new Object[]{"Collections.checkedNavigableMap(TreeSet)",
169                (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},
170            new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)",
171                (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
172            new Object[]{"Collections.checkedNavigableMap().descendingSet()",
173                (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}
174            );
175    }
176}
177