< prev index next >

test/jdk/java/util/Map/LockStep.java

Print this page

  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 
 31 import java.util.Collections;
 32 import java.util.HashMap;
 33 import java.util.Hashtable;
 34 import java.util.IdentityHashMap;
 35 import java.util.Iterator;
 36 import java.util.LinkedHashMap;
 37 import java.util.List;
 38 import java.util.Map;
 39 import java.util.Random;
 40 import java.util.TreeMap;
 41 import java.util.WeakHashMap;
 42 import java.util.concurrent.ConcurrentHashMap;
 43 import java.util.concurrent.ConcurrentSkipListMap;
 44 
 45 /**
 46  * Based on the strange scenario required to reproduce
 47  * (coll) IdentityHashMap.iterator().remove() might decrement size twice
 48  *
 49  * It would be good to add more "Lockstep-style" tests to this file.
 50  */
 51 public class LockStep {

















 52     void mapsEqual(Map m1, Map m2) {
 53         equal(m1, m2);
 54         equal(m2, m1);
 55         equal(m1.size(), m2.size());
 56         equal(m1.isEmpty(), m2.isEmpty());
 57         equal(m1.keySet(), m2.keySet());
 58         equal(m2.keySet(), m1.keySet());
 59     }
 60 
 61     void mapsEqual(List<Map> maps) {
 62         Map first = maps.get(0);
 63         for (Map map : maps)
 64             mapsEqual(first, map);
 65     }
 66 
 67     void put(List<Map> maps, Object key, Object val) {
 68         for (Map map : maps)
 69             map.put(key, val);
 70         mapsEqual(maps);
 71     }

 95         it.remove();
 96         if (m.size() != size-1)
 97             throw new Error(String.format("Incorrect size!%nmap=%s, size=%d%n",
 98                                           m.toString(), m.size()));
 99     }
100 
101     void test(String[] args) throws Throwable {
102         final int iterations = 100;
103         final Random r = new Random();
104 
105         for (int i = 0; i < iterations; i++) {
106             List<Map> maps = List.of(
107                 new IdentityHashMap(11),
108                 new HashMap(16),
109                 new LinkedHashMap(16),
110                 new WeakHashMap(16),
111                 new Hashtable(16),
112                 new TreeMap(),
113                 new ConcurrentHashMap(16),
114                 new ConcurrentSkipListMap(),
115                 Collections.checkedMap(new HashMap(16), Integer.class, Integer.class),
116                 Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class),
117                 Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class),
118                 Collections.synchronizedMap(new HashMap(16)),
119                 Collections.synchronizedSortedMap(new TreeMap()),
120                 Collections.synchronizedNavigableMap(new TreeMap()));
121 
122             for (int j = 0; j < 10; j++)
123                 put(maps, r.nextInt(100), r.nextInt(100));
124             removeLastTwo(maps);
125         }
126     }
127 
128     //--------------------- Infrastructure ---------------------------
129     volatile int passed = 0, failed = 0;
130     void pass() {passed++;}
131     void fail() {failed++; Thread.dumpStack();}
132     void fail(String msg) {System.err.println(msg); fail();}
133     void unexpected(Throwable t) {failed++; t.printStackTrace();}
134     void check(boolean cond) {if (cond) pass(); else fail();}
135     void equal(Object x, Object y) {
136         if (x == null ? y == null : x.equals(y)) pass();
137         else fail(x + " not equal to " + y);}
138     public static void main(String[] args) throws Throwable {
139         new LockStep().instanceMain(args);}
140     void instanceMain(String[] args) throws Throwable {
141         try {test(args);} catch (Throwable t) {unexpected(t);}
142         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
143         if (failed > 0) throw new AssertionError("Some tests failed");}

  1 /*
  2  * Copyright (c) 2008, 2024, 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 8336669
 27  * @summary Test Map implementations for mutual compatibility
 28  * @key randomness
 29  */
 30 
 31 import java.util.Collections;
 32 import java.util.HashMap;
 33 import java.util.Hashtable;
 34 import java.util.IdentityHashMap;
 35 import java.util.Iterator;
 36 import java.util.LinkedHashMap;
 37 import java.util.List;
 38 import java.util.Map;
 39 import java.util.Random;
 40 import java.util.TreeMap;
 41 import java.util.WeakHashMap;
 42 import java.util.concurrent.ConcurrentHashMap;
 43 import java.util.concurrent.ConcurrentSkipListMap;
 44 
 45 /**
 46  * Based on the strange scenario required to reproduce
 47  * (coll) IdentityHashMap.iterator().remove() might decrement size twice
 48  *
 49  * It would be good to add more "Lockstep-style" tests to this file.
 50  */
 51 public class LockStep {
 52     // An interned identity class holding an int (like non-Preview Integer)
 53     // interned for IdentityHashMap
 54     // identity for WeakHashMap
 55     private record Int(int intValue) implements Comparable<Int> {
 56         private static final Map<Integer, Int> interned = new HashMap<>(100);
 57 
 58         // Return a unique Ini for each int.
 59         static Int intern(int intValue) {
 60             return interned.computeIfAbsent(intValue, (i) -> new Int(i));
 61         }
 62 
 63         @Override
 64         public int compareTo(Int o) {
 65             return Integer.compare(intValue, o.intValue);
 66         }
 67     }
 68 
 69     void mapsEqual(Map m1, Map m2) {
 70         equal(m1, m2);
 71         equal(m2, m1);
 72         equal(m1.size(), m2.size());
 73         equal(m1.isEmpty(), m2.isEmpty());
 74         equal(m1.keySet(), m2.keySet());
 75         equal(m2.keySet(), m1.keySet());
 76     }
 77 
 78     void mapsEqual(List<Map> maps) {
 79         Map first = maps.get(0);
 80         for (Map map : maps)
 81             mapsEqual(first, map);
 82     }
 83 
 84     void put(List<Map> maps, Object key, Object val) {
 85         for (Map map : maps)
 86             map.put(key, val);
 87         mapsEqual(maps);
 88     }

112         it.remove();
113         if (m.size() != size-1)
114             throw new Error(String.format("Incorrect size!%nmap=%s, size=%d%n",
115                                           m.toString(), m.size()));
116     }
117 
118     void test(String[] args) throws Throwable {
119         final int iterations = 100;
120         final Random r = new Random();
121 
122         for (int i = 0; i < iterations; i++) {
123             List<Map> maps = List.of(
124                 new IdentityHashMap(11),
125                 new HashMap(16),
126                 new LinkedHashMap(16),
127                 new WeakHashMap(16),
128                 new Hashtable(16),
129                 new TreeMap(),
130                 new ConcurrentHashMap(16),
131                 new ConcurrentSkipListMap(),
132                 Collections.checkedMap(new HashMap(16), Int.class, Integer.class),
133                 Collections.checkedSortedMap(new TreeMap(), Int.class, Integer.class),
134                 Collections.checkedNavigableMap(new TreeMap(), Int.class, Integer.class),
135                 Collections.synchronizedMap(new HashMap(16)),
136                 Collections.synchronizedSortedMap(new TreeMap()),
137                 Collections.synchronizedNavigableMap(new TreeMap()));
138 
139             for (int j = 0; j < 10; j++)
140                 put(maps, Int.intern(r.nextInt(100)), r.nextInt(100));
141             removeLastTwo(maps);
142         }
143     }
144 
145     //--------------------- Infrastructure ---------------------------
146     volatile int passed = 0, failed = 0;
147     void pass() {passed++;}
148     void fail() {failed++; Thread.dumpStack();}
149     void fail(String msg) {System.err.println(msg); fail();}
150     void unexpected(Throwable t) {failed++; t.printStackTrace();}
151     void check(boolean cond) {if (cond) pass(); else fail();}
152     void equal(Object x, Object y) {
153         if (x == null ? y == null : x.equals(y)) pass();
154         else fail(x + " not equal to " + y);}
155     public static void main(String[] args) throws Throwable {
156         new LockStep().instanceMain(args);}
157     void instanceMain(String[] args) throws Throwable {
158         try {test(args);} catch (Throwable t) {unexpected(t);}
159         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
160         if (failed > 0) throw new AssertionError("Some tests failed");}
< prev index next >