--- jsr166/src/test/loops/MapCheck.java 2009/10/29 23:09:07 1.6 +++ jsr166/src/test/loops/MapCheck.java 2016/10/23 03:03:23 1.19 @@ -1,7 +1,7 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ /** * @test @@ -9,17 +9,17 @@ * * When run with "s" second arg, this requires file "testwords", which * is best used with real words. We can't check in this file, but you - * can create one from a real dictonary (1 line per word) and then run + * can create one from a real dictionary (1 line per word) and then run * linux "shuf" to randomize entries. */ -import java.util.*; import java.io.*; +import java.util.*; public class MapCheck { static final Object MISSING = new Object(); static TestTimer timer = new TestTimer(); - static Class eclass; - static Class mapClass = java.util.concurrent.ConcurrentHashMap.class; + static Class eclass; + static Class mapClass = java.util.concurrent.ConcurrentHashMap.class; static final LoopHelpers.SimpleRandom srng = new LoopHelpers.SimpleRandom(); static final Random rng = new Random(3152688); @@ -40,7 +40,7 @@ public class MapCheck { if (args.length > 0) { try { mapClass = Class.forName(args[0]); - } catch(ClassNotFoundException e) { + } catch (ClassNotFoundException e) { throw new RuntimeException("Class " + args[0] + " not found."); } } @@ -79,7 +79,7 @@ public class MapCheck { Object[] absent = new Object[size]; initializeKeys(key, absent, size); - precheck(size, key, absent); + precheck(size, key, absent); for (int rep = 0; rep < numTests; ++rep) { mainTest(newMap(), key, absent); @@ -99,8 +99,8 @@ public class MapCheck { static Map newMap() { try { - return (Map)mapClass.newInstance(); - } catch(Exception e) { + return (Map) mapClass.getConstructor().newInstance(); + } catch (Exception e) { throw new RuntimeException("Can't instantiate " + mapClass + ": " + e); } } @@ -134,7 +134,7 @@ public class MapCheck { try { m.put(null, x); v = m.get(null); - } catch(NullPointerException npe) { + } catch (NullPointerException npe) { System.out.println("Map does not allow null keys"); return; } @@ -143,7 +143,6 @@ public class MapCheck { if (m.get(null) != null) throw new Error(); } - static void getTest(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; timer.start(nm, n); @@ -153,21 +152,20 @@ public class MapCheck { ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } - // unused static void getTestBoxed(String nm, int n, Map s, Object[] key, int expect) { int sum = 0; Map intMap = (Map)s; timer.start(nm, n); for (int i = 0; i < n; i++) { - if ((Integer)(intMap.get(i)) != i) ++sum; + if (intMap.get(i) != i) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); } static void remTest(String nm, int n, Map s, Object[] key, int expect) { @@ -177,7 +175,7 @@ public class MapCheck { if (s.remove(key[i]) != null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } @@ -186,7 +184,7 @@ public class MapCheck { timer.start(nm, n); s.clear(); timer.finish(); - reallyAssert (s.isEmpty()); + reallyAssert(s.isEmpty()); } static void putTest(String nm, int n, Map s, Object[] key, int expect) { @@ -198,7 +196,7 @@ public class MapCheck { if (v == null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } @@ -209,7 +207,7 @@ public class MapCheck { if (s.containsKey(key[i])) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } @@ -219,7 +217,7 @@ public class MapCheck { for (int i = 0; i < n; i++) { if (s.containsKey(key[i])) ++sum; } - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } @@ -230,7 +228,7 @@ public class MapCheck { if (s.remove(key[i]) != null) ++sum; } timer.finish(); - reallyAssert (sum == expect); + reallyAssert(sum == expect); checkSum += sum; } @@ -240,11 +238,10 @@ public class MapCheck { timer.start("Traverse key or value ", size); if (s.containsValue(MISSING)) ++sum; timer.finish(); - reallyAssert (sum == 0); + reallyAssert(sum == 0); checkSum += sum; } - static Object kitTest(Map s, int size) { Object last = null; int sum = 0; @@ -256,7 +253,7 @@ public class MapCheck { last = x; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); checkSum += sum; return last; } @@ -272,7 +269,7 @@ public class MapCheck { last = x; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); checkSum += sum; return last; } @@ -289,13 +286,13 @@ public class MapCheck { ++sum; } timer.finish(); - reallyAssert (sum == size); + reallyAssert(sum == size); checkSum += sum; } static void itRemTest(Map s, int size) { int sz = s.size(); - reallyAssert (sz == size); + reallyAssert(sz == size); timer.start("Remove Present ", size); int sum = 0; for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { @@ -304,13 +301,13 @@ public class MapCheck { ++sum; } timer.finish(); - reallyAssert (sum == sz); + reallyAssert(sum == sz); checkSum += sum; } static void itHalfRemTest(Map s, int size) { int sz = s.size(); - reallyAssert (sz == size); + reallyAssert(sz == size); timer.start("Remove Present ", size); int sum = 0; for (Iterator it = s.keySet().iterator(); it.hasNext(); ) { @@ -321,7 +318,7 @@ public class MapCheck { ++sum; } timer.finish(); - reallyAssert (sum == sz / 2); + reallyAssert(sum == sz / 2); checkSum += sum; } @@ -329,7 +326,7 @@ public class MapCheck { timer.start(nm, n); dst.putAll(src); timer.finish(); - reallyAssert (src.size() == dst.size()); + reallyAssert(src.size() == dst.size()); } static void serTest(Map s, int size) throws Exception { @@ -350,7 +347,7 @@ public class MapCheck { FileInputStream is = new FileInputStream("MapCheck.dat"); ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is)); - Map m = (Map)in.readObject(); + Map m = (Map) in.readObject(); long endTime = System.currentTimeMillis(); long time = endTime - startTime; @@ -358,7 +355,7 @@ public class MapCheck { System.out.print(time + "ms"); if (s instanceof IdentityHashMap) return; - reallyAssert (s.equals(m)); + reallyAssert(s.equals(m)); } static void mainTest(Map s, Object[] key, Object[] absent) { @@ -426,34 +423,34 @@ public class MapCheck { timer.start("Traverse entry ", size * 12); // 12 until finish int sh1 = s.hashCode() - s2.hashCode(); - reallyAssert (sh1 == 0); + reallyAssert(sh1 == 0); boolean eq1 = s2.equals(s); boolean eq2 = s.equals(s2); - reallyAssert (eq1 && eq2); + reallyAssert(eq1 && eq2); Set es2 = s2.entrySet(); for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Object entry = it.next(); if (es2.contains(entry)) ++sum; } - reallyAssert (sum == size); + reallyAssert(sum == size); s2.put(lastkey, MISSING); int sh2 = s.hashCode() - s2.hashCode(); - reallyAssert (sh2 != 0); + reallyAssert(sh2 != 0); eq1 = s2.equals(s); eq2 = s.equals(s2); - reallyAssert (!eq1 && !eq2); + reallyAssert(!eq1 && !eq2); sum = 0; for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry)it.next(); e.setValue(absent[sum++]); } - reallyAssert (sum == size); + reallyAssert(sum == size); for (Iterator it = s2.entrySet().iterator(); it.hasNext(); ) { Map.Entry e = (Map.Entry)it.next(); e.setValue(s.get(e.getKey())); @@ -473,12 +470,12 @@ public class MapCheck { reallyAssert(rmiss == 0); clrTest(size, s2); - reallyAssert (s2.isEmpty() && s.isEmpty()); + reallyAssert(s2.isEmpty() && s.isEmpty()); } static void itTest4(Map s, int size, int pos) { IdentityHashMap seen = new IdentityHashMap(size); - reallyAssert (s.size() == size); + reallyAssert(s.size() == size); int sum = 0; timer.start("Iter XEntry ", size); Iterator it = s.entrySet().iterator(); @@ -492,9 +489,9 @@ public class MapCheck { if (x != MISSING) ++sum; } - reallyAssert (s.containsKey(k)); + reallyAssert(s.containsKey(k)); it.remove(); - reallyAssert (!s.containsKey(k)); + reallyAssert(!s.containsKey(k)); while (it.hasNext()) { Map.Entry x = (Map.Entry)(it.next()); Object k2 = x.getKey(); @@ -503,16 +500,14 @@ public class MapCheck { ++sum; } - reallyAssert (s.size() == size-1); + reallyAssert(s.size() == size-1); s.put(k, v); - reallyAssert (seen.size() == size); + reallyAssert(seen.size() == size); timer.finish(); - reallyAssert (sum == size); - reallyAssert (s.size() == size); + reallyAssert(sum == size); + reallyAssert(s.size() == size); } - - static void initializeKeys(Object[] key, Object[] absent, int size) { if (eclass == Object.class) { for (int i = 0; i < size; ++i) key[i] = new Object(); @@ -610,7 +605,7 @@ public class MapCheck { abs[ai++] = s; break; } - sb.append((char)c); + sb.append((char) c); } } in.close(); @@ -621,7 +616,6 @@ public class MapCheck { } } - static void randomWords(Object[] ws, int origin, int size) { for (int i = origin; i < size; ++i) { int k = 0; @@ -629,15 +623,15 @@ public class MapCheck { char[] c = new char[len * 4 + 1]; for (int j = 1; j < len; ++j) { int r = srng.next(); - c[k++] = (char)(' ' + (r & 0x7f)); + c[k++] = (char) (' ' + (r & 0x7f)); r >>>= 8; - c[k++] = (char)(' ' + (r & 0x7f)); + c[k++] = (char) (' ' + (r & 0x7f)); r >>>= 8; - c[k++] = (char)(' ' + (r & 0x7f)); + c[k++] = (char) (' ' + (r & 0x7f)); r >>>= 8; - c[k++] = (char)(' ' + (r & 0x7f)); + c[k++] = (char) (' ' + (r & 0x7f)); } - c[k++] = (char)((i & 31) | 1); // never == to any testword + c[k++] = (char) ((i & 31) | 1); // never == to any testword ws[i] = new String(c); } } @@ -651,8 +645,8 @@ public class MapCheck { static void printStats() { for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) { - Map.Entry e = (Map.Entry)(it.next()); - Stats stats = ((Stats)(e.getValue())); + Map.Entry e = (Map.Entry) it.next(); + Stats stats = (Stats) e.getValue(); System.out.print(e.getKey() + ": "); long s; long n = stats.number; @@ -663,7 +657,7 @@ public class MapCheck { else s = stats.sum; - double t = ((double)s) / n; + double t = ((double) s) / n; long nano = Math.round(t); System.out.printf("%6d", + nano); System.out.println(); @@ -682,7 +676,7 @@ public class MapCheck { if (st == null) accum.put(name, new Stats(elapsed, numOps)); else - ((Stats)st).addTime(elapsed, numOps); + ((Stats) st).addTime(elapsed, numOps); } } @@ -702,10 +696,9 @@ public class MapCheck { } } - static void shuffle(Object[] keys) { int size = keys.length; - for (int i= size; i>1; i--) { + for (int i = size; i > 1; i--) { int r = rng.nextInt(i); Object t = keys[i-1]; keys[i-1] = keys[r]; @@ -715,7 +708,7 @@ public class MapCheck { static void shuffle(ArrayList keys) { int size = keys.size(); - for (int i= size; i>1; i--) { + for (int i = size; i > 1; i--) { int r = rng.nextInt(i); Object t = keys.get(i-1); keys.set(i-1, keys.get(r));