ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/CheckedMapBash.java
(Generate patch)

Comparing jsr166/src/test/jtreg/util/Collections/CheckedMapBash.java (file contents):
Revision 1.2 by jsr166, Wed Sep 1 20:12:39 2010 UTC vs.
Revision 1.11 by jsr166, Sat Oct 21 06:47:09 2017 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
# Line 16 | Line 16
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 < * CA 95054 USA or visit www.sun.com if you need additional information or
21 < * have any questions.
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
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   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  
40   public class CheckedMapBash {
41 <    static Random rnd = new Random();
42 <    static Object nil = new Integer(0);
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) || newHead.equals(nil));
56 >            m.put(newHead, head);
57 >            head = newHead;
58 >        }
59 >        if (m.size() != mapSize)
60 >            fail("Size not as expected.");
61  
62 <    public static void main(String[] args) {
63 <        int numItr = 100;
64 <        int mapSize = 100;
65 <
66 <        // Linked List test
67 <        for (int i=0; i<numItr; i++) {
68 <            Map m = newMap();
69 <            Object head = nil;
70 <
71 <            for (int j=0; j<mapSize; j++) {
72 <                Object newHead;
73 <                do {
74 <                    newHead = new Integer(rnd.nextInt());
75 <                } while (m.containsKey(newHead));
51 <                m.put(newHead, head);
52 <                head = newHead;
53 <            }
54 <            if (m.size() != mapSize)
55 <                fail("Size not as expected.");
56 <
57 <            {
58 <                HashMap hm = new HashMap(m);
59 <                if (! (hm.hashCode() == m.hashCode() &&
60 <                       hm.entrySet().hashCode() == m.entrySet().hashCode() &&
61 <                       hm.keySet().hashCode() == m.keySet().hashCode()))
62 <                    fail("Incorrect hashCode computation.");
63 <
64 <                if (! (hm.equals(m) &&
65 <                       hm.entrySet().equals(m.entrySet()) &&
66 <                       hm.keySet().equals(m.keySet()) &&
67 <                       m.equals(hm) &&
68 <                       m.entrySet().equals(hm.entrySet()) &&
69 <                       m.keySet().equals(hm.keySet())))
70 <                    fail("Incorrect equals computation.");
71 <            }
72 <
73 <            Map m2 = newMap(); m2.putAll(m);
74 <            m2.values().removeAll(m.keySet());
75 <            if (m2.size()!= 1 || !m2.containsValue(nil))
76 <                fail("Collection views test failed.");
77 <
78 <            int j=0;
79 <            while (head != nil) {
80 <                if (!m.containsKey(head))
81 <                    fail("Linked list doesn't contain a link.");
82 <                Object newHead = m.get(head);
83 <                if (newHead == null)
84 <                    fail("Could not retrieve a link.");
85 <                m.remove(head);
86 <                head = newHead;
87 <                j++;
88 <            }
89 <            if (!m.isEmpty())
90 <                fail("Map nonempty after removing all links.");
91 <            if (j != mapSize)
92 <                fail("Linked list size not as expected.");
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 m = newMap();
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 testCheckedMap2(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.");
105 >                fail("put returns a non-null value erroneously.");
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 = newMap(); m2.putAll(m);
110 >            fail("put returns a null value erroneously.");
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))
# Line 134 | Line 141 | public class CheckedMapBash {
141              fail("Iterator.remove() failed");
142      }
143  
144 <    static Map newMap() {
145 <        Map m = Collections.checkedMap(new HashMap(),
146 <                                       Integer.class, Integer.class);
144 >    @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 >            iters.addAll(makeCheckedMaps());
150 >        }
151 >        return iters.iterator();
152 >    }
153  
154 <        if (!m.isEmpty())
155 <            fail("New instance non empty.");
156 <        return m;
154 >    @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
155 >    public static Iterator<Object[]> navigableMapProvider() {
156 >        return makeCheckedMaps().iterator();
157      }
158  
159 <    static void fail(String s) {
160 <        throw new RuntimeException(s);
159 >    public static Collection<Object[]> makeCheckedMaps() {
160 >        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      }
176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines