📄 testmap.java
字号:
/**
* Utility methods to create an array of Map.Entry objects
* out of the given key and value arrays.<P>
*
* @param keys the array of keys
* @param values the array of values
* @return an array of Map.Entry of those keys to those values
*/
private Map.Entry[] makeEntryArray(Object[] keys, Object[] values) {
Map.Entry[] result = new Map.Entry[keys.length];
for (int i = 0; i < keys.length; i++) {
result[i] = new DefaultMapEntry(keys[i], values[i]);
}
return result;
}
/**
* Bulk test {@link Map#entrySet()}. This method runs through all of
* the tests in {@link TestSet}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the map and the other collection views are still valid.
*
* @return a {@link TestSet} instance for testing the map's entry set
*/
public BulkTest bulkTestMapEntrySet() {
return new TestMapEntrySet();
}
class TestMapEntrySet extends TestSet {
public TestMapEntrySet() {
super("");
}
// Have to implement manually; entrySet doesn't support addAll
protected Object[] getFullElements() {
Object[] k = getSampleKeys();
Object[] v = getSampleValues();
return makeEntryArray(k, v);
}
// Have to implement manually; entrySet doesn't support addAll
protected Object[] getOtherElements() {
Object[] k = getOtherKeys();
Object[] v = getOtherValues();
return makeEntryArray(k, v);
}
protected Set makeEmptySet() {
return makeEmptyMap().entrySet();
}
protected Set makeFullSet() {
return makeFullMap().entrySet();
}
protected boolean isAddSupported() {
// Collection views don't support add operations.
return false;
}
protected boolean isRemoveSupported() {
// Entry set should only support remove if map does
return isAddRemoveModifiable();
}
protected void resetFull() {
TestMap.this.resetFull();
collection = map.entrySet();
TestMapEntrySet.this.confirmed = TestMap.this.confirmed.entrySet();
}
protected void resetEmpty() {
TestMap.this.resetEmpty();
collection = map.entrySet();
TestMapEntrySet.this.confirmed = TestMap.this.confirmed.entrySet();
}
protected void verify() {
super.verify();
TestMap.this.verify();
}
}
/**
* Bulk test {@link Map#keySet()}. This method runs through all of
* the tests in {@link TestSet}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the map and the other collection views are still valid.
*
* @return a {@link TestSet} instance for testing the map's key set
*/
public BulkTest bulkTestMapKeySet() {
return new TestMapKeySet();
}
class TestMapKeySet extends TestSet {
public TestMapKeySet() {
super("");
}
protected Object[] getFullElements() {
return getSampleKeys();
}
protected Object[] getOtherElements() {
return getOtherKeys();
}
protected Set makeEmptySet() {
return makeEmptyMap().keySet();
}
protected Set makeFullSet() {
return makeFullMap().keySet();
}
protected boolean isAddSupported() {
return false;
}
protected boolean isRemoveSupported() {
return isAddRemoveModifiable();
}
protected void resetEmpty() {
TestMap.this.resetEmpty();
collection = map.keySet();
TestMapKeySet.this.confirmed = TestMap.this.confirmed.keySet();
}
protected void resetFull() {
TestMap.this.resetFull();
collection = map.keySet();
TestMapKeySet.this.confirmed = TestMap.this.confirmed.keySet();
}
protected void verify() {
super.verify();
TestMap.this.verify();
}
}
/**
* Bulk test {@link Map#values()}. This method runs through all of
* the tests in {@link TestCollection}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the map and the other collection views are still valid.
*
* @return a {@link TestCollection} instance for testing the map's
* values collection
*/
public BulkTest bulkTestMapValues() {
return new TestMapValues();
}
class TestMapValues extends TestCollection {
public TestMapValues() {
super("");
}
protected Object[] getFullElements() {
return getSampleValues();
}
protected Object[] getOtherElements() {
return getOtherValues();
}
protected Collection makeCollection() {
return makeEmptyMap().values();
}
protected Collection makeFullCollection() {
return makeFullMap().values();
}
protected boolean isAddSupported() {
return false;
}
protected boolean isRemoveSupported() {
return isAddRemoveModifiable();
}
protected boolean areEqualElementsDistinguishable() {
// equal values are associated with different keys, so they are
// distinguishable.
return true;
}
protected Collection makeConfirmedCollection() {
// never gets called, reset methods are overridden
return null;
}
protected Collection makeConfirmedFullCollection() {
// never gets called, reset methods are overridden
return null;
}
protected void resetFull() {
TestMap.this.resetFull();
collection = map.values();
TestMapValues.this.confirmed = TestMap.this.confirmed.values();
}
protected void resetEmpty() {
TestMap.this.resetEmpty();
collection = map.values();
TestMapValues.this.confirmed = TestMap.this.confirmed.values();
}
protected void verify() {
super.verify();
TestMap.this.verify();
}
// TODO: should test that a remove on the values collection view
// removes the proper mapping and not just any mapping that may have
// the value equal to the value returned from the values iterator.
}
/**
* Resets the {@link #map}, {@link #entrySet}, {@link #keySet},
* {@link #values} and {@link #confirmed} fields to empty.
*/
protected void resetEmpty() {
this.map = makeEmptyMap();
views();
this.confirmed = new HashMap();
}
/**
* Resets the {@link #map}, {@link #entrySet}, {@link #keySet},
* {@link #values} and {@link #confirmed} fields to full.
*/
protected void resetFull() {
this.map = makeFullMap();
views();
this.confirmed = new HashMap();
Object[] k = getSampleKeys();
Object[] v = getSampleValues();
for (int i = 0; i < k.length; i++) {
confirmed.put(k[i], v[i]);
}
}
/**
* Resets the collection view fields.
*/
private void views() {
this.keySet = map.keySet();
this.values = map.values();
this.entrySet = map.entrySet();
}
/**
* Verifies that {@link #map} is still equal to {@link #confirmed}.
* This method checks that the map is equal to the HashMap,
* <I>and</I> that the map's collection views are still equal to
* the HashMap's collection views. An <Code>equals</Code> test
* is done on the maps and their collection views; their size and
* <Code>isEmpty</Code> results are compared; their hashCodes are
* compared; and <Code>containsAll</Code> tests are run on the
* collection views.
*/
protected void verify() {
verifyMap();
verifyEntrySet();
verifyKeySet();
verifyValues();
}
protected void verifyMap() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("Map should be same size as HashMap",
size, map.size());
assertEquals("Map should be empty if HashMap is",
empty, map.isEmpty());
assertEquals("hashCodes should be the same",
confirmed.hashCode(), map.hashCode());
// this fails for LRUMap because confirmed.equals() somehow modifies
// map, causing concurrent modification exceptions.
//assertEquals("Map should still equal HashMap", confirmed, map);
// this works though and performs the same verification:
assertTrue("Map should still equal HashMap", map.equals(confirmed));
// TODO: this should really be rexamined to figure out why LRU map
// behaves like it does (the equals shouldn't modify since all accesses
// by the confirmed collection should be through an iterator, thus not
// causing LRUMap to change).
}
protected void verifyEntrySet() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("entrySet should be same size as HashMap's",
size, entrySet.size());
assertEquals("entrySet should be empty if HashMap is",
empty, entrySet.isEmpty());
assertTrue("entrySet should contain all HashMap's elements",
entrySet.containsAll(confirmed.entrySet()));
assertEquals("entrySet hashCodes should be the same",
confirmed.entrySet().hashCode(), entrySet.hashCode());
assertEquals("Map's entry set should still equal HashMap's",
confirmed.entrySet(), entrySet);
}
protected void verifyKeySet() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("keySet should be same size as HashMap's",
size, keySet.size());
assertEquals("keySet should be empty if HashMap is",
empty, keySet.isEmpty());
assertTrue("keySet should contain all HashMap's elements",
keySet.containsAll(confirmed.keySet()));
assertEquals("keySet hashCodes should be the same",
confirmed.keySet().hashCode(), keySet.hashCode());
assertEquals("Map's key set should still equal HashMap's",
confirmed.keySet(), keySet);
}
protected void verifyValues() {
Bag bag1 = new HashBag(confirmed.values());
Bag bag2 = new HashBag(values);
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("values should be same size as HashMap's",
size, values.size());
assertEquals("values should be empty if HashMap is",
empty, values.isEmpty());
assertTrue("values should contain all HashMap's elements",
values.containsAll(confirmed.values()));
assertEquals("Map's values should still equal HashMap's",
bag1, bag2);
}
/**
* Erases any leftover instance variables by setting them to null.
*/
protected void tearDown() {
map = null;
keySet = null;
entrySet = null;
values = null;
confirmed = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -