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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines