📄 testmap.java
字号:
/**
* Test to ensure that makeEmptyMap and makeFull returns a new non-null
* map with each invocation.
**/
public void testMakeMap() {
Map em = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
Map em2 = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
assertTrue("failure in test: makeEmptyMap must return a new map " +
"with each invocation.", em != em2);
Map fm = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
Map fm2 = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
assertTrue("failure in test: makeFullMap must return a new map " +
"with each invocation.", fm != fm2);
}
/**
* Tests Map.isEmpty()
**/
public void testMapIsEmpty() {
resetEmpty();
assertEquals("Map.isEmpty() should return true with an empty map",
true, map.isEmpty());
verify();
resetFull();
assertEquals("Map.isEmpty() should return false with a non-empty map",
false, map.isEmpty());
verify();
}
/**
* Tests Map.size()
**/
public void testMapSize() {
resetEmpty();
assertEquals("Map.size() should be 0 with an empty map",
0, map.size());
verify();
resetFull();
assertEquals("Map.size() should equal the number of entries " +
"in the map", getSampleKeys().length, map.size());
verify();
}
/**
* Tests {@link Map#clear()}. If the map {@link #isAddRemoveModifiable()
* can add and remove elements}, then {@link Map#size()} and {@link
* Map#isEmpty()} are used to ensure that map has no elements after a call
* to clear. If the map does not support adding and removing elements,
* this method checks to ensure clear throws an
* UnsupportedOperationException.
**/
public void testMapClear() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
map.clear();
confirmed.clear();
verify();
resetFull();
map.clear();
confirmed.clear();
verify();
}
/**
* Tests Map.containsKey(Object) by verifying it returns false for all
* sample keys on a map created using an empty map and returns true for
* all sample keys returned on a full map.
**/
public void testMapContainsKey() {
Object[] keys = getSampleKeys();
resetEmpty();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must not contain key when map is empty",
!map.containsKey(keys[i]));
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must contain key for a mapping in the map. " +
"Missing: " + keys[i], map.containsKey(keys[i]));
}
verify();
}
/**
* Tests Map.containsValue(Object) by verifying it returns false for all
* sample values on an empty map and returns true for all sample values on
* a full map.
**/
public void testMapContainsValue() {
Object[] values = getSampleValues();
resetEmpty();
for(int i = 0; i < values.length; i++) {
assertTrue("Empty map must not contain value",
!map.containsValue(values[i]));
}
verify();
resetFull();
for(int i = 0; i < values.length; i++) {
assertTrue("Map must contain value for a mapping in the map.",
map.containsValue(values[i]));
}
verify();
}
/**
* Tests Map.equals(Object)
**/
public void testMapEquals() {
resetEmpty();
assertTrue("Empty maps unequal.", map.equals(confirmed));
verify();
resetFull();
assertTrue("Full maps unequal.", map.equals(confirmed));
verify();
resetFull();
// modify the HashMap created from the full map and make sure this
// change results in map.equals() to return false.
Iterator iter = confirmed.keySet().iterator();
iter.next();
iter.remove();
assertTrue("Different maps equal.", !map.equals(confirmed));
resetFull();
assertTrue("equals(null) returned true.", !map.equals(null));
assertTrue("equals(new Object()) returned true.",
!map.equals(new Object()));
verify();
}
/**
* Tests Map.get(Object)
**/
public void testMapGet() {
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for (int i = 0; i < keys.length; i++) {
assertTrue("Empty map.get() should return null.",
map.get(keys[i]) == null);
}
verify();
resetFull();
for (int i = 0; i < keys.length; i++) {
assertEquals("Full map.get() should return value from mapping.",
values[i], map.get(keys[i]));
}
}
/**
* Tests Map.hashCode()
**/
public void testMapHashCode() {
resetEmpty();
assertTrue("Empty maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
resetFull();
assertTrue("Equal maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
}
/**
* Tests Map.toString(). Since the format of the string returned by the
* toString() method is not defined in the Map interface, there is no
* common way to test the results of the toString() method. Thereforce,
* it is encouraged that Map implementations override this test with one
* that checks the format matches any format defined in its API. This
* default implementation just verifies that the toString() method does
* not return null.
**/
public void testMapToString() {
resetEmpty();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
resetFull();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
}
/**
* Compare the current serialized form of the Map
* against the canonical version in CVS.
*/
public void testEmptyMapCompatibility() throws IOException, ClassNotFoundException {
/**
* Create canonical objects with this code
Map map = makeEmptyMap();
if (!(map instanceof Serializable)) return;
writeExternalFormToDisk((Serializable) map, getCanonicalEmptyCollectionName(map));
*/
// test to make sure the canonical form has been preserved
if (!(makeEmptyMap() instanceof Serializable)) return;
Map map = (Map) readExternalFormFromDisk(getCanonicalEmptyCollectionName(makeEmptyMap()));
assertTrue("Map is empty",map.isEmpty() == true);
}
/**
* Compare the current serialized form of the Map
* against the canonical version in CVS.
*/
public void testFullMapCompatibility() throws IOException, ClassNotFoundException {
/**
* Create canonical objects with this code
Map map = makeFullMap();
if (!(map instanceof Serializable)) return;
writeExternalFormToDisk((Serializable) map, getCanonicalFullCollectionName(map));
*/
// test to make sure the canonical form has been preserved
if (!(makeFullMap() instanceof Serializable)) return;
Map map = (Map) readExternalFormFromDisk(getCanonicalFullCollectionName(makeFullMap()));
assertEquals("Map is the right size",map.size(), getSampleKeys().length);
}
/**
* Tests Map.put(Object, Object)
**/
public void testMapPut() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Object[] newValues = getNewSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], values[i]);
confirmed.put(keys[i], values[i]);
verify();
assertTrue("First map.put should return null", o == null);
assertTrue("Map should contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain value after put",
map.containsValue(values[i]));
}
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], newValues[i]);
confirmed.put(keys[i], newValues[i]);
verify();
assertEquals("Second map.put should return previous value",
values[i], o);
assertTrue("Map should still contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain new value after put",
map.containsValue(newValues[i]));
// if duplicates are allowed, we're not guarunteed that the value
// no longer exists, so don't try checking that.
if(!useDuplicateValues()) {
assertTrue("Map should not contain old value after second put",
!map.containsValue(values[i]));
}
}
}
/**
* Tests Map.putAll(Collection)
**/
public void testMapPutAll() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Map m2 = makeFullMap();
map.putAll(m2);
confirmed.putAll(m2);
verify();
resetEmpty();
m2 = new HashMap();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
m2.put(keys[i], values[i]);
}
map.putAll(m2);
confirmed.putAll(m2);
verify();
}
/**
* Tests Map.remove(Object)
**/
public void testMapRemove() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
assertTrue("First map.remove should return null", o == null);
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
confirmed.remove(keys[i]);
verify();
assertEquals("map.remove with valid key should return value",
values[i], o);
}
Object[] other = getOtherKeys();
resetFull();
int size = map.size();
for (int i = 0; i < other.length; i++) {
Object o = map.remove(other[i]);
assertEquals("map.remove for nonexistent key should return null",
o, null);
assertEquals("map.remove for nonexistent key should not " +
"shrink map", size, map.size());
}
verify();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -