undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,182 @@
/*
* Copyright (c) 2013, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import org.testng.annotations.Test;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* @test
* @bug 8028564
* @run testng/timeout=1200 ConcurrentAssociateTest
* @summary Test that association operations, such as put and compute,
* place entries in the map
* @modules java.management
*/
@Test
public class ConcurrentAssociateTest {
/** Maximum time (in seconds) to wait for a test method to complete. */
private static final int TIMEOUT = Integer.getInteger("timeout", 200);
/** The number of entries for each thread to place in a map. */
private static final int N = Integer.getInteger("n", 128);
/** The number of iterations of the test. */
private static final int I = Integer.getInteger("i", 64);
/** Objects to be placed in the concurrent map. */
static class X {
// Limit the hash code to trigger collisions
final int hc = ThreadLocalRandom.current().nextInt(1, 9);
public int hashCode() { return hc; }
}
@Test
public void testPut() throws Throwable {
test("CHM.put", (m, o) -> m.put(o, o));
}
@Test
public void testCompute() throws Throwable {
test("CHM.compute", (m, o) -> m.compute(o, (k, v) -> o));
}
@Test
public void testComputeIfAbsent() throws Throwable {
test("CHM.computeIfAbsent", (m, o) -> m.computeIfAbsent(o, (k) -> o));
}
@Test
public void testMerge() throws Throwable {
test("CHM.merge", (m, o) -> m.merge(o, o, (v1, v2) -> v1));
}
@Test
public void testPutAll() throws Throwable {
test("CHM.putAll", (m, o) -> {
Map<Object, Object> hm = new HashMap<>();
hm.put(o, o);
m.putAll(hm);
});
}
private static void test(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) throws Throwable {
for (int i = 0; i < I; i++) {
testOnce(desc, associator);
}
}
static class AssociationFailure extends RuntimeException {
AssociationFailure(String message) {
super(message);
}
}
private static void testOnce(String desc, BiConsumer<ConcurrentMap<Object, Object>, Object> associator) throws Throwable {
ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>();
CountDownLatch s = new CountDownLatch(1);
Supplier<Runnable> sr = () -> () -> {
try {
if (!s.await(TIMEOUT, TimeUnit.SECONDS)) {
dumpTestThreads();
throw new AssertionError("timed out");
}
}
catch (InterruptedException e) {
}
for (int i = 0; i < N; i++) {
Object o = new X();
associator.accept(m, o);
if (!m.containsKey(o)) {
throw new AssociationFailure(desc + " failed: entry does not exist");
}
}
};
// Bound concurrency to avoid degenerate performance
int ps = Math.min(Runtime.getRuntime().availableProcessors(), 8);
Stream<CompletableFuture> runners = IntStream.range(0, ps)
.mapToObj(i -> sr.get())
.map(CompletableFuture::runAsync);
CompletableFuture all = CompletableFuture.allOf(
runners.toArray(CompletableFuture[]::new));
// Trigger the runners to start associating
s.countDown();
try {
all.get(TIMEOUT, TimeUnit.SECONDS);
} catch (TimeoutException e) {
dumpTestThreads();
throw e;
} catch (Throwable e) {
dumpTestThreads();
Throwable cause = e.getCause();
if (cause instanceof AssociationFailure) {
throw cause;
}
throw e;
}
}
/**
* A debugging tool to print stack traces of most threads, as jstack does.
* Uninteresting threads are filtered out.
*/
static void dumpTestThreads() {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
System.err.println("------ stacktrace dump start ------");
for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
final String name = info.getThreadName();
String lockName;
if ("Signal Dispatcher".equals(name))
continue;
if ("Reference Handler".equals(name))
continue;
if ("Finalizer".equals(name))
continue;
System.err.print(info);
}
System.err.println("------ stacktrace dump end ------");
}
}

View file

@ -0,0 +1,136 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import org.testng.annotations.Test;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.function.Supplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* @test
* @bug 8028564
* @run testng ConcurrentContainsKeyTest
* @summary Test that entries are always present in the map,
* when entries are held within one bin that is a tree
*/
@Test
public class ConcurrentContainsKeyTest {
// The number of entries for each thread to place in a map
// Should be > ConcurrentHashMap.TREEIFY_THRESHOLD but small
// enough to allow for enough iteration overlap by multiple threads
private static final int N = Integer.getInteger("n", 16);
// The number of rounds each thread performs per entry
private static final int R = Integer.getInteger("r", 32);
// The number of iterations of the test
private static final int I = Integer.getInteger("i", 256);
// Object to be placed in the concurrent map
static class X implements Comparable<X> {
private final int a;
X(int a) {
this.a = a;
}
public int compareTo(X o) {
return this.a - o.a;
}
public int hashCode() {
// Return the same hash code to guarantee collisions
return 0;
}
}
@Test
public void testContainsKey() {
X[] content = IntStream.range(0, N).mapToObj(i -> new X(i)).toArray(X[]::new);
// Create map with an initial size >= ConcurrentHashMap.TREEIFY_THRESHOLD
// ensuring tree'ification will occur for a small number of entries
// with the same hash code
ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<>(64);
Stream.of(content).forEach(x -> m.put(x, x));
test(content, m);
}
private static void test(X[] content, ConcurrentHashMap<Object, Object> m) {
for (int i = 0; i < I; i++) {
testOnce(content, m);
}
}
static class AssociationFailure extends RuntimeException {
AssociationFailure(String message) {
super(message);
}
}
private static void testOnce(Object[] content, ConcurrentHashMap<Object, Object> m) {
CountDownLatch s = new CountDownLatch(1);
Supplier<Runnable> sr = () -> () -> {
try {
s.await();
}
catch (InterruptedException e) {
}
for (int i = 0; i < R * N; i++) {
Object o = content[i % content.length];
if (!m.containsKey(o)) {
throw new AssociationFailure("CHM.containsKey failed: entry does not exist");
}
}
};
int ps = Runtime.getRuntime().availableProcessors();
Stream<CompletableFuture> runners = IntStream.range(0, ps)
.mapToObj(i -> sr.get())
.map(CompletableFuture::runAsync);
CompletableFuture all = CompletableFuture.allOf(
runners.toArray(CompletableFuture[]::new));
// Trigger the runners to start checking key membership
s.countDown();
try {
all.join();
}
catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
}
else {
throw e;
}
}
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* Portions Copyright (c) 2011 IBM Corporation
*/
/*
* @test
* @bug 6312706
* @summary Sets from Map.entrySet() return distinct objects for each Entry
* @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
*/
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class DistinctEntrySetElements {
public static void main(String[] args) throws Exception {
final ConcurrentHashMap<String, String> concurrentHashMap =
new ConcurrentHashMap<>();
concurrentHashMap.put("One", "Un");
concurrentHashMap.put("Two", "Deux");
concurrentHashMap.put("Three", "Trois");
Set<Map.Entry<String, String>> entrySet = concurrentHashMap.entrySet();
HashSet<Map.Entry<String, String>> hashSet = new HashSet<>(entrySet);
if (false == hashSet.equals(entrySet)) {
throw new RuntimeException("Test FAILED: Sets are not equal.");
}
if (hashSet.hashCode() != entrySet.hashCode()) {
throw new RuntimeException("Test FAILED: Set's hashcodes are not equal.");
}
}
}

View file

@ -0,0 +1,103 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* 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/publicdomain/zero/1.0/
*/
/**
* Misc utilities in JSR166 performance tests
*/
class LoopHelpers {
// Some mindless computation to do between synchronizations...
/**
* generates 32 bit pseudo-random numbers.
* Adapted from http://www.snippets.org
*/
public static int compute1(int x) {
int lo = 16807 * (x & 0xFFFF);
int hi = 16807 * (x >>> 16);
lo += (hi & 0x7FFF) << 16;
if ((lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
lo += hi >>> 15;
if (lo == 0 || (lo & 0x80000000) != 0) {
lo &= 0x7fffffff;
++lo;
}
return lo;
}
/**
* Computes a linear congruential random number a random number
* of times.
*/
public static int compute2(int x) {
int loops = (x >>> 4) & 7;
while (loops-- > 0) {
x = (x * 2147483647) % 16807;
}
return x;
}
public static class BarrierTimer implements Runnable {
public volatile long startTime;
public volatile long endTime;
public void run() {
long t = System.nanoTime();
if (startTime == 0)
startTime = t;
else
endTime = t;
}
public void clear() {
startTime = 0;
endTime = 0;
}
public long getTime() {
return endTime - startTime;
}
}
public static String rightJustify(long n) {
// There's probably a better way to do this...
String field = " ";
String num = Long.toString(n);
if (num.length() >= field.length())
return num;
StringBuilder b = new StringBuilder(field);
b.replace(b.length()-num.length(), b.length(), num);
return b.toString();
}
}

View file

@ -0,0 +1,622 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* 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/publicdomain/zero/1.0/
*/
/*
* @test
* @bug 4486658
* @run main/timeout=240 MapCheck
* @summary Times and checks basic map operations
*/
import java.io.Serializable;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class MapCheck {
static final int absentSize = 1 << 17;
static final int absentMask = absentSize - 1;
static Object[] absent = new Object[absentSize];
static final Object MISSING = new Object();
static TestTimer timer = new TestTimer();
static void reallyAssert(boolean b) {
if (!b) throw new Error("Failed Assertion");
}
public static void main(String[] args) throws Exception {
Class mapClass = java.util.concurrent.ConcurrentHashMap.class;
int numTests = 8;
int size = 50000;
if (args.length > 0) {
try {
mapClass = Class.forName(args[0]);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class " + args[0] + " not found.");
}
}
if (args.length > 1)
numTests = Integer.parseInt(args[1]);
if (args.length > 2)
size = Integer.parseInt(args[2]);
boolean doSerializeTest = args.length > 3;
System.out.println("Testing " + mapClass.getName() + " trials: " + numTests + " size: " + size);
for (int i = 0; i < absentSize; ++i) absent[i] = new Object();
Object[] key = new Object[size];
for (int i = 0; i < size; ++i) key[i] = new Object();
forceMem(size * 8);
for (int rep = 0; rep < numTests; ++rep) {
runTest(newMap(mapClass), key);
}
TestTimer.printStats();
if (doSerializeTest)
stest(newMap(mapClass), size);
}
static Map newMap(Class cl) {
try {
return (Map)cl.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Can't instantiate " + cl + ": " + e);
}
}
static void runTest(Map s, Object[] key) {
shuffle(key);
int size = key.length;
long startTime = System.currentTimeMillis();
test(s, key);
long time = System.currentTimeMillis() - startTime;
}
static void forceMem(int n) {
// force enough memory
Long[] junk = new Long[n];
for (int i = 0; i < junk.length; ++i) junk[i] = new Long(i);
int sum = 0;
for (int i = 0; i < junk.length; ++i)
sum += (int)(junk[i].longValue() + i);
if (sum == 0) System.out.println("Useless number = " + sum);
junk = null;
// System.gc();
}
static void t1(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
int iters = 4;
timer.start(nm, n * iters);
for (int j = 0; j < iters; ++j) {
for (int i = 0; i < n; i++) {
if (s.get(key[i]) != null) ++sum;
}
}
timer.finish();
reallyAssert(sum == expect * iters);
}
static void t2(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
timer.start(nm, n);
for (int i = 0; i < n; i++) {
if (s.remove(key[i]) != null) ++sum;
}
timer.finish();
reallyAssert(sum == expect);
}
static void t3(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
timer.start(nm, n);
for (int i = 0; i < n; i++) {
if (s.put(key[i], absent[i & absentMask]) == null) ++sum;
}
timer.finish();
reallyAssert(sum == expect);
}
static void t4(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
timer.start(nm, n);
for (int i = 0; i < n; i++) {
if (s.containsKey(key[i])) ++sum;
}
timer.finish();
reallyAssert(sum == expect);
}
static void t5(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
timer.start(nm, n/2);
for (int i = n-2; i >= 0; i-=2) {
if (s.remove(key[i]) != null) ++sum;
}
timer.finish();
reallyAssert(sum == expect);
}
static void t6(String nm, int n, Map s, Object[] k1, Object[] k2) {
int sum = 0;
timer.start(nm, n * 2);
for (int i = 0; i < n; i++) {
if (s.get(k1[i]) != null) ++sum;
if (s.get(k2[i & absentMask]) != null) ++sum;
}
timer.finish();
reallyAssert(sum == n);
}
static void t7(String nm, int n, Map s, Object[] k1, Object[] k2) {
int sum = 0;
timer.start(nm, n * 2);
for (int i = 0; i < n; i++) {
if (s.containsKey(k1[i])) ++sum;
if (s.containsKey(k2[i & absentMask])) ++sum;
}
timer.finish();
reallyAssert(sum == n);
}
static void t8(String nm, int n, Map s, Object[] key, int expect) {
int sum = 0;
timer.start(nm, n);
for (int i = 0; i < n; i++) {
if (s.get(key[i]) != null) ++sum;
}
timer.finish();
reallyAssert(sum == expect);
}
static void t9(Map s) {
int sum = 0;
int iters = 20;
timer.start("ContainsValue (/n) ", iters * s.size());
int step = absentSize / iters;
for (int i = 0; i < absentSize; i += step)
if (s.containsValue(absent[i])) ++sum;
timer.finish();
reallyAssert(sum != 0);
}
static void ktest(Map s, int size, Object[] key) {
timer.start("ContainsKey ", size);
Set ks = s.keySet();
int sum = 0;
for (int i = 0; i < size; i++) {
if (ks.contains(key[i])) ++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void ittest1(Map s, int size) {
int sum = 0;
timer.start("Iter Key ", size);
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
if (it.next() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void ittest2(Map s, int size) {
int sum = 0;
timer.start("Iter Value ", size);
for (Iterator it = s.values().iterator(); it.hasNext(); ) {
if (it.next() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void ittest3(Map s, int size) {
int sum = 0;
timer.start("Iter Entry ", size);
for (Iterator it = s.entrySet().iterator(); it.hasNext(); ) {
if (it.next() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void ittest4(Map s, int size, int pos) {
IdentityHashMap seen = new IdentityHashMap(size);
reallyAssert(s.size() == size);
int sum = 0;
timer.start("Iter XEntry ", size);
Iterator it = s.entrySet().iterator();
Object k = null;
Object v = null;
for (int i = 0; i < size-pos; ++i) {
Map.Entry x = (Map.Entry)(it.next());
k = x.getKey();
v = x.getValue();
seen.put(k, k);
if (x != MISSING)
++sum;
}
reallyAssert(s.containsKey(k));
it.remove();
reallyAssert(!s.containsKey(k));
while (it.hasNext()) {
Map.Entry x = (Map.Entry)(it.next());
Object k2 = x.getKey();
seen.put(k2, k2);
if (x != MISSING)
++sum;
}
reallyAssert(s.size() == size-1);
s.put(k, v);
reallyAssert(seen.size() == size);
timer.finish();
reallyAssert(sum == size);
reallyAssert(s.size() == size);
}
static void ittest(Map s, int size) {
ittest1(s, size);
ittest2(s, size);
ittest3(s, size);
// for (int i = 0; i < size-1; ++i)
// ittest4(s, size, i);
}
static void entest1(Hashtable ht, int size) {
int sum = 0;
timer.start("Iter Enumeration Key ", size);
for (Enumeration en = ht.keys(); en.hasMoreElements(); ) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void entest2(Hashtable ht, int size) {
int sum = 0;
timer.start("Iter Enumeration Value ", size);
for (Enumeration en = ht.elements(); en.hasMoreElements(); ) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void entest3(Hashtable ht, int size) {
int sum = 0;
timer.start("Iterf Enumeration Key ", size);
Enumeration en = ht.keys();
for (int i = 0; i < size; ++i) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void entest4(Hashtable ht, int size) {
int sum = 0;
timer.start("Iterf Enumeration Value", size);
Enumeration en = ht.elements();
for (int i = 0; i < size; ++i) {
if (en.nextElement() != MISSING)
++sum;
}
timer.finish();
reallyAssert(sum == size);
}
static void entest(Map s, int size) {
if (s instanceof Hashtable) {
Hashtable ht = (Hashtable)s;
// entest3(ht, size);
// entest4(ht, size);
entest1(ht, size);
entest2(ht, size);
entest1(ht, size);
entest2(ht, size);
entest1(ht, size);
entest2(ht, size);
}
}
static void rtest(Map s, int size) {
timer.start("Remove (iterator) ", size);
for (Iterator it = s.keySet().iterator(); it.hasNext(); ) {
it.next();
it.remove();
}
timer.finish();
}
static void rvtest(Map s, int size) {
timer.start("Remove (iterator) ", size);
for (Iterator it = s.values().iterator(); it.hasNext(); ) {
it.next();
it.remove();
}
timer.finish();
}
static void dtest(Map s, int size, Object[] key) {
timer.start("Put (putAll) ", size * 2);
Map s2 = null;
try {
s2 = (Map) s.getClass().getDeclaredConstructor().newInstance();
s2.putAll(s);
}
catch (Exception e) { e.printStackTrace(); return; }
timer.finish();
timer.start("Iter Equals ", size * 2);
boolean eqt = s2.equals(s) && s.equals(s2);
reallyAssert(eqt);
timer.finish();
timer.start("Iter HashCode ", size * 2);
int shc = s.hashCode();
int s2hc = s2.hashCode();
reallyAssert(shc == s2hc);
timer.finish();
timer.start("Put (present) ", size);
s2.putAll(s);
timer.finish();
timer.start("Iter EntrySet contains ", size * 2);
Set es2 = s2.entrySet();
int sum = 0;
for (Iterator i1 = s.entrySet().iterator(); i1.hasNext(); ) {
Object entry = i1.next();
if (es2.contains(entry)) ++sum;
}
timer.finish();
reallyAssert(sum == size);
t6("Get ", size, s2, key, absent);
Object hold = s2.get(key[size-1]);
s2.put(key[size-1], absent[0]);
timer.start("Iter Equals ", size * 2);
eqt = s2.equals(s) && s.equals(s2);
reallyAssert(!eqt);
timer.finish();
timer.start("Iter HashCode ", size * 2);
int s1h = s.hashCode();
int s2h = s2.hashCode();
reallyAssert(s1h != s2h);
timer.finish();
s2.put(key[size-1], hold);
timer.start("Remove (iterator) ", size * 2);
Iterator s2i = s2.entrySet().iterator();
Set es = s.entrySet();
while (s2i.hasNext())
es.remove(s2i.next());
timer.finish();
reallyAssert(s.isEmpty());
timer.start("Clear ", size);
s2.clear();
timer.finish();
reallyAssert(s2.isEmpty() && s.isEmpty());
}
static void stest(Map s, int size) throws Exception {
if (!(s instanceof Serializable))
return;
System.out.print("Serialize : ");
for (int i = 0; i < size; i++) {
s.put(new Integer(i), Boolean.TRUE);
}
long startTime = System.currentTimeMillis();
FileOutputStream fs = new FileOutputStream("MapCheck.dat");
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fs));
out.writeObject(s);
out.close();
FileInputStream is = new FileInputStream("MapCheck.dat");
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(is));
Map m = (Map)in.readObject();
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
System.out.print(time + "ms");
if (s instanceof IdentityHashMap) return;
reallyAssert(s.equals(m));
}
static void test(Map s, Object[] key) {
int size = key.length;
t3("Put (absent) ", size, s, key, size);
t3("Put (present) ", size, s, key, 0);
t7("ContainsKey ", size, s, key, absent);
t4("ContainsKey ", size, s, key, size);
ktest(s, size, key);
t4("ContainsKey ", absentSize, s, absent, 0);
t6("Get ", size, s, key, absent);
t1("Get (present) ", size, s, key, size);
t1("Get (absent) ", absentSize, s, absent, 0);
t2("Remove (absent) ", absentSize, s, absent, 0);
t5("Remove (present) ", size, s, key, size / 2);
t3("Put (half present) ", size, s, key, size / 2);
ittest(s, size);
entest(s, size);
t9(s);
rtest(s, size);
t4("ContainsKey ", size, s, key, 0);
t2("Remove (absent) ", size, s, key, 0);
t3("Put (presized) ", size, s, key, size);
dtest(s, size, key);
}
static class TestTimer {
private String name;
private long numOps;
private long startTime;
private String cname;
static final java.util.TreeMap accum = new java.util.TreeMap();
static void printStats() {
for (Iterator it = accum.entrySet().iterator(); it.hasNext(); ) {
Map.Entry e = (Map.Entry)(it.next());
Stats stats = (Stats)(e.getValue());
int n = stats.number;
double t;
if (n > 0)
t = stats.sum / n;
else
t = stats.least;
long nano = Math.round(1000000.0 * t);
System.out.println(e.getKey() + ": " + nano);
}
}
void start(String name, long numOps) {
this.name = name;
this.cname = classify();
this.numOps = numOps;
startTime = System.currentTimeMillis();
}
String classify() {
if (name.startsWith("Get"))
return "Get ";
else if (name.startsWith("Put"))
return "Put ";
else if (name.startsWith("Remove"))
return "Remove ";
else if (name.startsWith("Iter"))
return "Iter ";
else
return null;
}
void finish() {
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
double timePerOp = ((double)time)/numOps;
Object st = accum.get(name);
if (st == null)
accum.put(name, new Stats(timePerOp));
else {
Stats stats = (Stats) st;
stats.sum += timePerOp;
stats.number++;
if (timePerOp < stats.least) stats.least = timePerOp;
}
if (cname != null) {
st = accum.get(cname);
if (st == null)
accum.put(cname, new Stats(timePerOp));
else {
Stats stats = (Stats) st;
stats.sum += timePerOp;
stats.number++;
if (timePerOp < stats.least) stats.least = timePerOp;
}
}
}
}
static class Stats {
double sum = 0;
double least;
int number = 0;
Stats(double t) { least = t; }
}
static void shuffle(Object[] keys) {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int size = keys.length;
for (int i=size; i>1; i--) {
int r = rnd.nextInt(i);
Object t = keys[i-1];
keys[i-1] = keys[r];
keys[r] = t;
}
}
}

View file

@ -0,0 +1,249 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* 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/publicdomain/zero/1.0/
*/
/*
* @test
* @bug 4486658
* @summary Exercise multithreaded maps, by default ConcurrentHashMap.
* Multithreaded hash table test. Each thread does a random walk
* though elements of "key" array. On each iteration, it checks if
* table includes key. If absent, with probability pinsert it
* inserts it, and if present, with probability premove it removes
* it. (pinsert and premove are expressed as percentages to simplify
* parsing from command line.)
* @library /test/lib
* @run main/timeout=1600 MapLoops
*/
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.List;
import java.util.Map;
import java.util.SplittableRandom;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import jdk.test.lib.Utils;
public class MapLoops {
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
static int nkeys = 1000; // 10_000
static int pinsert = 60;
static int premove = 2;
static int maxThreads = 100;
static int nops = 10000; // 100_000
static int removesPerMaxRandom;
static int insertsPerMaxRandom;
static final ExecutorService pool = Executors.newCachedThreadPool();
static final List<Throwable> throwables = new CopyOnWriteArrayList<>();
public static void main(String[] args) throws Exception {
Class mapClass = null;
if (args.length > 0) {
try {
mapClass = Class.forName(args[0]);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Class " + args[0] + " not found.");
}
}
else
mapClass = java.util.concurrent.ConcurrentHashMap.class;
if (args.length > 1)
maxThreads = Integer.parseInt(args[1]);
if (args.length > 2)
nkeys = Integer.parseInt(args[2]);
if (args.length > 3)
pinsert = Integer.parseInt(args[3]);
if (args.length > 4)
premove = Integer.parseInt(args[4]);
if (args.length > 5)
nops = Integer.parseInt(args[5]);
// normalize probabilities wrt random number generator
removesPerMaxRandom = (int)((double)premove/100.0 * 0x7FFFFFFFL);
insertsPerMaxRandom = (int)((double)pinsert/100.0 * 0x7FFFFFFFL);
System.out.print("Class: " + mapClass.getName());
System.out.print(" threads: " + maxThreads);
System.out.print(" size: " + nkeys);
System.out.print(" ins: " + pinsert);
System.out.print(" rem: " + premove);
System.out.print(" ops: " + nops);
System.out.println();
int k = 1;
int warmups = 2;
for (int i = 1; i <= maxThreads;) {
test(i, nkeys, mapClass);
if (warmups > 0)
--warmups;
else if (i == k) {
k = i << 1;
i = i + (i >>> 1);
}
else if (i == 1 && k == 2) {
i = k;
warmups = 1;
}
else
i = k;
}
pool.shutdown();
if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
throw new Error();
if (! throwables.isEmpty())
throw new Error
(throwables.size() + " thread(s) terminated abruptly.");
}
static Integer[] makeKeys(int n) {
SplittableRandom rnd = new SplittableRandom();
Integer[] key = new Integer[n];
for (int i = 0; i < key.length; ++i)
key[i] = new Integer(rnd.nextInt());
return key;
}
static void shuffleKeys(Integer[] key) {
SplittableRandom rnd = new SplittableRandom();
for (int i = key.length; i > 1; --i) {
int j = rnd.nextInt(i);
Integer tmp = key[j];
key[j] = key[i-1];
key[i-1] = tmp;
}
}
static void test(int i, int nkeys, Class mapClass) throws Exception {
System.out.print("Threads: " + i + "\t:");
Map<Integer, Integer> map = (Map<Integer, Integer>)
mapClass.getDeclaredConstructor().newInstance();
Integer[] key = makeKeys(nkeys);
// Uncomment to start with a non-empty table
// for (int j = 0; j < nkeys; j += 4) // start 1/4 occupied
// map.put(key[j], key[j]);
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
CyclicBarrier barrier = new CyclicBarrier(i+1, timer);
SplittableRandom rnd = new SplittableRandom();
for (int t = 0; t < i; ++t)
pool.execute(new Runner(map, key, barrier, rnd.split()));
barrier.await();
barrier.await();
long time = timer.getTime();
long tpo = time / (i * (long)nops);
System.out.print(LoopHelpers.rightJustify(tpo) + " ns per op");
double secs = (double)time / 1000000000.0;
System.out.println("\t " + secs + "s run time");
map.clear();
}
static class Runner implements Runnable {
final Map<Integer,Integer> map;
final Integer[] key;
final CyclicBarrier barrier;
final SplittableRandom rnd;
int position;
int total;
Runner(Map<Integer,Integer> map,
Integer[] key,
CyclicBarrier barrier,
SplittableRandom rnd) {
this.map = map;
this.key = key;
this.barrier = barrier;
this.rnd = rnd;
position = key.length / 2;
}
int step() {
// random-walk around key positions, bunching accesses
int r = rnd.nextInt(Integer.MAX_VALUE);
position += (r & 7) - 3;
while (position >= key.length) position -= key.length;
while (position < 0) position += key.length;
Integer k = key[position];
Integer x = map.get(k);
if (x != null) {
if (x.intValue() != k.intValue())
throw new Error("bad mapping: " + x + " to " + k);
if (r < removesPerMaxRandom) {
if (map.remove(k) != null) {
position = total % key.length; // move from position
return 2;
}
}
}
else if (r < insertsPerMaxRandom) {
++position;
map.put(k, k);
return 2;
}
// Uncomment to add a little computation between accesses
// total += LoopHelpers.compute1(k.intValue());
total += r;
return 1;
}
public void run() {
try {
barrier.await();
int ops = nops;
while (ops > 0)
ops -= step();
barrier.await();
}
catch (Throwable throwable) {
synchronized (System.err) {
System.err.println("--------------------------------");
throwable.printStackTrace();
}
throwables.add(throwable);
}
}
}
}

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4486658 8010293
* @summary thread safety of toArray methods of collection views
* @author Martin Buchholz
*/
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ToArray {
public static void main(String[] args) throws Throwable {
final int runsPerTest = Integer.getInteger("jsr166.runsPerTest", 1);
final int reps = 10 * runsPerTest;
for (int i = reps; i--> 0; )
executeTest();
}
static void executeTest() throws Throwable {
try (var executor = Executors.newCachedThreadPool()) {
final ConcurrentHashMap<Integer, Integer> m = new ConcurrentHashMap<>();
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final int nCPU = Runtime.getRuntime().availableProcessors();
final int minWorkers = 2;
final int maxWorkers = Math.max(minWorkers, Math.min(32, nCPU));
final int nWorkers = rnd.nextInt(minWorkers, maxWorkers + 1);
final int sizePerWorker = 1024;
final int maxSize = nWorkers * sizePerWorker;
// The foreman busy-checks that the size of the arrays obtained
// from the keys and values views grows monotonically until it
// reaches the maximum size.
// NOTE: these size constraints are not specific to toArray and are
// applicable to any form of traversal of the collection views
CompletableFuture<?> foreman = CompletableFuture.runAsync(new Runnable() {
private int prevSize = 0;
private boolean checkProgress(Object[] a) {
int size = a.length;
if (size < prevSize || size > maxSize)
throw new AssertionError(
String.format("prevSize=%d size=%d maxSize=%d",
prevSize, size, maxSize));
prevSize = size;
return size == maxSize;
}
public void run() {
Integer[] empty = new Integer[0];
for (; ; )
if (checkProgress(m.values().toArray())
& checkProgress(m.keySet().toArray())
& checkProgress(m.values().toArray(empty))
& checkProgress(m.keySet().toArray(empty)))
return;
}
}, executor);
// Each worker puts globally unique keys into the map
List<CompletableFuture<?>> workers =
IntStream.range(0, nWorkers)
.mapToObj(w -> (Runnable) () -> {
for (int i = 0, o = w * sizePerWorker; i < sizePerWorker; i++)
m.put(o + i, i);
})
.map(r -> CompletableFuture.runAsync(r, executor))
.collect(Collectors.toList());
// Wait for workers and foreman to complete
workers.forEach(CompletableFuture<?>::join);
foreman.join();
}
}
}

View file

@ -0,0 +1,210 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Written by Martin Buchholz with assistance from members of JCP
* JSR-166 Expert Group and released to the public domain, as
* explained at http://creativecommons.org/publicdomain/zero/1.0/
*/
/*
* @test
* @modules java.base/java.util.concurrent:open
* @run testng WhiteBox
* @summary White box tests of implementation details
*/
import static org.testng.Assert.*;
import org.testng.annotations.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier;
@Test
public class WhiteBox {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final VarHandle TABLE, NEXTTABLE, SIZECTL;
final MethodHandle TABLE_SIZE_FOR;
WhiteBox() throws ReflectiveOperationException {
Class<?> mClass = ConcurrentHashMap.class;
String nodeClassName = mClass.getName() + "$Node";
Class<?> nodeClass = Class.forName(nodeClassName);
Class<?> nodeArrayClass = Class.forName("[L" + nodeClassName + ";");
MethodHandles.Lookup lookup
= MethodHandles.privateLookupIn(mClass, MethodHandles.lookup());
TABLE = lookup.findVarHandle(mClass, "table", nodeArrayClass);
NEXTTABLE = lookup.findVarHandle(mClass, "nextTable", nodeArrayClass);
SIZECTL = lookup.findVarHandle(mClass, "sizeCtl", int.class);
TABLE_SIZE_FOR = lookup.findStatic(
mClass, "tableSizeFor",
MethodType.methodType(int.class, int.class));
}
Object[] table(ConcurrentHashMap m) { return (Object[]) TABLE.getVolatile(m); }
Object[] nextTable(ConcurrentHashMap m) { return (Object[]) NEXTTABLE.getVolatile(m); }
int sizeCtl(ConcurrentHashMap m) { return (int) SIZECTL.getVolatile(m); }
int tableSizeFor(int n) {
try {
return (int) TABLE_SIZE_FOR.invoke(n);
} catch (Throwable t) { throw new AssertionError(t); }
}
List<Supplier<ConcurrentHashMap>> newConcurrentHashMapSuppliers(
int initialCapacity) {
return List.of(
() -> new ConcurrentHashMap(initialCapacity),
() -> new ConcurrentHashMap(initialCapacity, 0.75f),
() -> new ConcurrentHashMap(initialCapacity, 0.75f, 1));
}
ConcurrentHashMap newConcurrentHashMap(int initialCapacity) {
List<Supplier<ConcurrentHashMap>> suppliers
= newConcurrentHashMapSuppliers(initialCapacity);
return suppliers.get(rnd.nextInt(suppliers.size())).get();
}
@Test
public void defaultConstructor() {
ConcurrentHashMap m = new ConcurrentHashMap();
assertNull(table(m));
assertEquals(sizeCtl(m), 0);
assertResizeNotInProgress(m);
}
@Test
public void shouldNotResizeWhenInitialCapacityProvided() {
int initialCapacity = rnd.nextInt(1, 100);
Object[] initialTable = null;
ConcurrentHashMap m = newConcurrentHashMap(initialCapacity);
// table is lazily initialized
assertNull(table(m));
int expectedInitialTableLength = sizeCtl(m);
assertInvariants(m);
for (int i = 0; i < initialCapacity; i++) {
m.put(i * 100 + rnd.nextInt(100), i);
if (i == 0)
initialTable = table(m);
else
assertSame(table(m), initialTable);
assertInvariants(m);
}
assertEquals(initialTable.length, expectedInitialTableLength);
}
@Test
public void constructorsShouldGiveSameInitialCapacity() {
int initialCapacity = rnd.nextInt(1, 256);
long distinctTableLengths
= newConcurrentHashMapSuppliers(initialCapacity).stream()
.map(Supplier::get)
.mapToInt(map -> { map.put(42, 42); return table(map).length; })
.distinct()
.count();
assertEquals(1L, distinctTableLengths);
}
@Test
public void testTableSizeFor() {
assertEquals(1, tableSizeFor(0));
assertEquals(1, tableSizeFor(1));
assertEquals(2, tableSizeFor(2));
assertEquals(4, tableSizeFor(3));
assertEquals(16, tableSizeFor(15));
assertEquals(16, tableSizeFor(16));
assertEquals(32, tableSizeFor(17));
int maxSize = 1 << 30;
assertEquals(maxSize, tableSizeFor(maxSize - 1));
assertEquals(maxSize, tableSizeFor(maxSize));
assertEquals(maxSize, tableSizeFor(maxSize + 1));
assertEquals(maxSize, tableSizeFor(Integer.MAX_VALUE));
}
byte[] serialBytes(Object o) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
oos.close();
return bos.toByteArray();
} catch (Exception fail) {
throw new AssertionError(fail);
}
}
@SuppressWarnings("unchecked")
<T> T serialClone(T o) {
try {
ObjectInputStream ois = new ObjectInputStream
(new ByteArrayInputStream(serialBytes(o)));
T clone = (T) ois.readObject();
assertNotSame(o, clone);
assertSame(o.getClass(), clone.getClass());
return clone;
} catch (Exception fail) {
throw new AssertionError(fail);
}
}
@Test
public void testSerialization() {
assertInvariants(serialClone(new ConcurrentHashMap()));
ConcurrentHashMap m = newConcurrentHashMap(rnd.nextInt(100));
m.put(1, 1);
ConcurrentHashMap clone = serialClone(m);
assertInvariants(clone);
assertNotSame(table(m), table(clone));
assertEquals(m, clone);
assertResizeNotInProgress(m);
assertResizeNotInProgress(clone);
}
/** Checks conditions which should always be true. */
void assertInvariants(ConcurrentHashMap m) {
if (!m.isEmpty())
assertNotNull(table(m));
}
void assertResizeNotInProgress(ConcurrentHashMap m) {
assertTrue(sizeCtl(m) >= 0);
assertNull(nextTable(m));
}
}