• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2008, 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 6612102
27  * @summary Test Map implementations for mutual compatibility
28  * @key randomness
29  */
30 package test.java.util.Map;
31 
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Hashtable;
35 import java.util.IdentityHashMap;
36 import java.util.Iterator;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Random;
41 import java.util.TreeMap;
42 import java.util.WeakHashMap;
43 import java.util.concurrent.ConcurrentHashMap;
44 import java.util.concurrent.ConcurrentSkipListMap;
45 
46 import org.testng.annotations.Test;
47 import org.testng.Assert;
48 
49 /**
50  * Based on the strange scenario required to reproduce
51  * (coll) IdentityHashMap.iterator().remove() might decrement size twice
52  *
53  * It would be good to add more "Lockstep-style" tests to this file.
54  */
55 public class LockStep {
mapsEqual(Map m1, Map m2)56     void mapsEqual(Map m1, Map m2) {
57         Assert.assertEquals(m1, m2);
58         Assert.assertEquals(m2, m1);
59         Assert.assertEquals(m1.size(), m2.size());
60         Assert.assertEquals(m1.isEmpty(), m2.isEmpty());
61         Assert.assertEquals(m1.keySet(), m2.keySet());
62         Assert.assertEquals(m2.keySet(), m1.keySet());
63     }
64 
mapsEqual(List<Map> maps)65     void mapsEqual(List<Map> maps) {
66         Map first = maps.get(0);
67         for (Map map : maps)
68             mapsEqual(first, map);
69     }
70 
put(List<Map> maps, Object key, Object val)71     void put(List<Map> maps, Object key, Object val) {
72         for (Map map : maps)
73             map.put(key, val);
74         mapsEqual(maps);
75     }
76 
removeLastTwo(List<Map> maps)77     void removeLastTwo(List<Map> maps) {
78         Map first = maps.get(0);
79         int size = first.size();
80         Iterator fit = first.keySet().iterator();
81         for (int j = 0; j < size - 2; j++)
82             fit.next();
83         Object x1 = fit.next();
84         Object x2 = fit.next();
85 
86         for (Map map : maps) {
87             Iterator it = map.keySet().iterator();
88             while (it.hasNext()) {
89                 Object x = it.next();
90                 if (x == x1 || x == x2)
91                     it.remove();
92             }
93         }
94         mapsEqual(maps);
95     }
96 
97     @Test
testLockStep()98     public void testLockStep() throws Throwable {
99         final int iterations = 100;
100         final Random r = new Random();
101 
102         for (int i = 0; i < iterations; i++) {
103             List<Map> maps = List.of(
104                     new IdentityHashMap(11),
105                     new HashMap(16),
106                     new LinkedHashMap(16),
107                     new WeakHashMap(16),
108                     new Hashtable(16),
109                     new TreeMap(),
110                     new ConcurrentHashMap(16),
111                     new ConcurrentSkipListMap(),
112                     Collections.checkedMap(new HashMap(16), Integer.class, Integer.class),
113                     Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class),
114                     Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class),
115                     Collections.synchronizedMap(new HashMap(16)),
116                     Collections.synchronizedSortedMap(new TreeMap()),
117                     Collections.synchronizedNavigableMap(new TreeMap()));
118 
119             for (int j = 0; j < 10; j++)
120                 put(maps, r.nextInt(100), r.nextInt(100));
121             removeLastTwo(maps);
122         }
123     }
124 }