concurrenthashmaptest.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 552 行 · 第 1/2 页
JAVA
552 行
/*
* 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
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
import junit.framework.*;
import edu.emory.mathcs.backport.java.util.*;
import edu.emory.mathcs.backport.java.util.concurrent.*;
import java.util.*;
import java.io.*;
public class ConcurrentHashMapTest extends JSR166TestCase{
public static void main(String[] args) {
junit.textui.TestRunner.run (suite());
}
public static Test suite() {
return new TestSuite(ConcurrentHashMapTest.class);
}
/**
* Create a map from Integers 1-5 to Strings "A"-"E".
*/
private static ConcurrentHashMap map5() {
ConcurrentHashMap map = new ConcurrentHashMap(5);
assertTrue(map.isEmpty());
map.put(one, "A");
map.put(two, "B");
map.put(three, "C");
map.put(four, "D");
map.put(five, "E");
assertFalse(map.isEmpty());
assertEquals(5, map.size());
return map;
}
/**
* clear removes all pairs
*/
public void testClear() {
ConcurrentHashMap map = map5();
map.clear();
assertEquals(map.size(), 0);
}
/**
* Maps with same contents are equal
*/
public void testEquals() {
ConcurrentHashMap map1 = map5();
ConcurrentHashMap map2 = map5();
assertEquals(map1, map2);
assertEquals(map2, map1);
map1.clear();
assertFalse(map1.equals(map2));
assertFalse(map2.equals(map1));
}
/**
* contains returns true for contained value
*/
public void testContains() {
ConcurrentHashMap map = map5();
assertTrue(map.contains("A"));
assertFalse(map.contains("Z"));
}
/**
* containsKey returns true for contained key
*/
public void testContainsKey() {
ConcurrentHashMap map = map5();
assertTrue(map.containsKey(one));
assertFalse(map.containsKey(zero));
}
/**
* containsValue returns true for held values
*/
public void testContainsValue() {
ConcurrentHashMap map = map5();
assertTrue(map.containsValue("A"));
assertFalse(map.containsValue("Z"));
}
/**
* enumeration returns an enumeration containing the correct
* elements
*/
public void testEnumeration() {
ConcurrentHashMap map = map5();
Enumeration e = map.elements();
int count = 0;
while(e.hasMoreElements()){
count++;
e.nextElement();
}
assertEquals(5, count);
}
/**
* get returns the correct element at the given key,
* or null if not present
*/
public void testGet() {
ConcurrentHashMap map = map5();
assertEquals("A", (String)map.get(one));
ConcurrentHashMap empty = new ConcurrentHashMap();
assertNull(map.get("anything"));
}
/**
* isEmpty is true of empty map and false for non-empty
*/
public void testIsEmpty() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
assertTrue(empty.isEmpty());
assertFalse(map.isEmpty());
}
/**
* keys returns an enumeration containing all the keys from the map
*/
public void testKeys() {
ConcurrentHashMap map = map5();
Enumeration e = map.keys();
int count = 0;
while(e.hasMoreElements()){
count++;
e.nextElement();
}
assertEquals(5, count);
}
/**
* keySet returns a Set containing all the keys
*/
public void testKeySet() {
ConcurrentHashMap map = map5();
Set s = map.keySet();
assertEquals(5, s.size());
assertTrue(s.contains(one));
assertTrue(s.contains(two));
assertTrue(s.contains(three));
assertTrue(s.contains(four));
assertTrue(s.contains(five));
}
/**
* values collection contains all values
*/
public void testValues() {
ConcurrentHashMap map = map5();
Collection s = map.values();
assertEquals(5, s.size());
assertTrue(s.contains("A"));
assertTrue(s.contains("B"));
assertTrue(s.contains("C"));
assertTrue(s.contains("D"));
assertTrue(s.contains("E"));
}
/**
* entrySet contains all pairs
*/
public void testEntrySet() {
ConcurrentHashMap map = map5();
Set s = map.entrySet();
assertEquals(5, s.size());
Iterator it = s.iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
assertTrue(
(e.getKey().equals(one) && e.getValue().equals("A")) ||
(e.getKey().equals(two) && e.getValue().equals("B")) ||
(e.getKey().equals(three) && e.getValue().equals("C")) ||
(e.getKey().equals(four) && e.getValue().equals("D")) ||
(e.getKey().equals(five) && e.getValue().equals("E")));
}
}
/**
* putAll adds all key-value pairs from the given map
*/
public void testPutAll() {
ConcurrentHashMap empty = new ConcurrentHashMap();
ConcurrentHashMap map = map5();
empty.putAll(map);
assertEquals(5, empty.size());
assertTrue(empty.containsKey(one));
assertTrue(empty.containsKey(two));
assertTrue(empty.containsKey(three));
assertTrue(empty.containsKey(four));
assertTrue(empty.containsKey(five));
}
/**
* putIfAbsent works when the given key is not present
*/
public void testPutIfAbsent() {
ConcurrentHashMap map = map5();
map.putIfAbsent(six, "Z");
assertTrue(map.containsKey(six));
}
/**
* putIfAbsent does not add the pair if the key is already present
*/
public void testPutIfAbsent2() {
ConcurrentHashMap map = map5();
assertEquals("A", map.putIfAbsent(one, "Z"));
}
/**
* replace fails when the given key is not present
*/
public void testReplace() {
ConcurrentHashMap map = map5();
assertNull(map.replace(six, "Z"));
assertFalse(map.containsKey(six));
}
/**
* replace succeeds if the key is already present
*/
public void testReplace2() {
ConcurrentHashMap map = map5();
assertNotNull(map.replace(one, "Z"));
assertEquals("Z", map.get(one));
}
/**
* replace value fails when the given key not mapped to expected value
*/
public void testReplaceValue() {
ConcurrentHashMap map = map5();
assertEquals("A", map.get(one));
assertFalse(map.replace(one, "Z", "Z"));
assertEquals("A", map.get(one));
}
/**
* replace value succeeds when the given key mapped to expected value
*/
public void testReplaceValue2() {
ConcurrentHashMap map = map5();
assertEquals("A", map.get(one));
assertTrue(map.replace(one, "A", "Z"));
assertEquals("Z", map.get(one));
}
/**
* remove removes the correct key-value pair from the map
*/
public void testRemove() {
ConcurrentHashMap map = map5();
map.remove(five);
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
}
/**
* remove(key,value) removes only if pair present
*/
public void testRemove2() {
ConcurrentHashMap map = map5();
map.remove(five, "E");
assertEquals(4, map.size());
assertFalse(map.containsKey(five));
map.remove(four, "A");
assertEquals(4, map.size());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?