📄 testcollection.java
字号:
public void testCollectionAddAll() {
if (!isAddSupported()) return;
resetEmpty();
Object[] elements = getFullElements();
boolean r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Empty collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
assertTrue("Collection should contain added element",
collection.contains(elements[i]));
}
resetFull();
int size = collection.size();
elements = getOtherElements();
r = collection.addAll(Arrays.asList(elements));
confirmed.addAll(Arrays.asList(elements));
verify();
assertTrue("Full collection should change after addAll", r);
for (int i = 0; i < elements.length; i++) {
assertTrue("Full collection should contain added element",
collection.contains(elements[i]));
}
assertEquals("Size should increase after addAll",
size + elements.length, collection.size());
resetFull();
size = collection.size();
r = collection.addAll(Arrays.asList(getFullElements()));
confirmed.addAll(Arrays.asList(getFullElements()));
verify();
if (r) {
assertTrue("Size should increase if addAll returns true",
size < collection.size());
} else {
assertEquals("Size should not change if addAll returns false",
size, collection.size());
}
}
/**
* If {@link #isAddSupported()} returns false, tests that add operations
* raise <Code>UnsupportedOperationException.
*/
public void testUnsupportedAdd() {
if (isAddSupported()) return;
resetEmpty();
try {
collection.add(new Object());
fail("Emtpy collection should not support add.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
collection.addAll(Arrays.asList(getFullElements()));
fail("Emtpy collection should not support addAll.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
resetFull();
try {
collection.add(new Object());
fail("Full collection should not support add.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
try {
collection.addAll(Arrays.asList(getOtherElements()));
fail("Full collection should not support addAll.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
}
/**
* Test {@link Collection#clear()}.
*/
public void testCollectionClear() {
if (!isRemoveSupported()) return;
resetEmpty();
collection.clear(); // just to make sure it doesn't raise anything
verify();
resetFull();
collection.clear();
confirmed.clear();
verify();
}
/**
* Tests {@link Collection#contains(Object)}.
*/
public void testCollectionContains() {
Object[] elements;
resetEmpty();
elements = getFullElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Empty collection shouldn'y contain element",
!collection.contains(elements[i]));
}
// make sure calls to "contains" don't change anything
verify();
elements = getOtherElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Empty collection shouldn'y contain element",
!collection.contains(elements[i]));
}
// make sure calls to "contains" don't change anything
verify();
resetFull();
elements = getFullElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Full collection should contain element.",
collection.contains(elements[i]));
}
// make sure calls to "contains" don't change anything
verify();
resetFull();
elements = getOtherElements();
for(int i = 0; i < elements.length; i++) {
assertTrue("Full collection shouldn't contain element",
!collection.contains(elements[i]));
}
}
/**
* Tests {@link Collection#containsAll(Collection)}.
*/
public void testCollectionContainsAll() {
resetEmpty();
Collection col = new HashSet();
assertTrue("Every Collection should contain all elements of an " +
"empty Collection.", collection.containsAll(col));
col.addAll(Arrays.asList(getOtherElements()));
assertTrue("Empty Collection shouldn't contain all elements of " +
"a non-empty Collection.", !collection.containsAll(col));
// make sure calls to "containsAll" don't change anything
verify();
resetFull();
assertTrue("Full collection shouldn't contain other elements",
!collection.containsAll(col));
col.clear();
col.addAll(Arrays.asList(getFullElements()));
assertTrue("Full collection should containAll full elements",
collection.containsAll(col));
// make sure calls to "containsAll" don't change anything
verify();
col = Arrays.asList(getFullElements()).subList(2, 5);
assertTrue("Full collection should containAll partial full " +
"elements", collection.containsAll(col));
assertTrue("Full collection should containAll itself",
collection.containsAll(collection));
// make sure calls to "containsAll" don't change anything
verify();
col = new ArrayList();
col.addAll(Arrays.asList(getFullElements()));
col.addAll(Arrays.asList(getFullElements()));
assertTrue("Full collection should containAll duplicate full " +
"elements", collection.containsAll(col));
// make sure calls to "containsAll" don't change anything
verify();
}
/**
* Tests {@link Collection#isEmpty()}.
*/
public void testCollectionIsEmpty() {
resetEmpty();
assertEquals("New Collection should be empty.",
true, collection.isEmpty());
// make sure calls to "isEmpty() don't change anything
verify();
resetFull();
assertEquals("Full collection shouldn't be empty",
false, collection.isEmpty());
// make sure calls to "isEmpty() don't change anything
verify();
}
/**
* Tests the read-only functionality of {@link Collection#iterator()}.
*/
public void testCollectionIterator() {
resetEmpty();
Iterator it1 = collection.iterator();
assertEquals("Iterator for empty Collection shouldn't have next.",
false, it1.hasNext());
try {
it1.next();
fail("Iterator at end of Collection should throw " +
"NoSuchElementException when next is called.");
} catch(NoSuchElementException e) {
// expected
}
// make sure nothing has changed after non-modification
verify();
resetFull();
it1 = collection.iterator();
for (int i = 0; i < collection.size(); i++) {
assertTrue("Iterator for full collection should haveNext",
it1.hasNext());
it1.next();
}
assertTrue("Iterator should be finished", !it1.hasNext());
ArrayList list = new ArrayList();
it1 = collection.iterator();
for (int i = 0; i < collection.size(); i++) {
Object next = it1.next();
assertTrue("Collection should contain element returned by " +
"its iterator", collection.contains(next));
list.add(next);
}
try {
it1.next();
fail("iterator.next() should raise NoSuchElementException " +
"after it finishes");
} catch (NoSuchElementException e) {
// expected
}
// make sure nothing has changed after non-modification
verify();
}
/**
* Tests removals from {@link Collection#iterator()}.
*/
public void testCollectionIteratorRemove() {
if (!isRemoveSupported()) return;
resetEmpty();
try {
collection.iterator().remove();
fail("New iterator.remove should raise IllegalState");
} catch (IllegalStateException e) {
// expected
}
verify();
try {
Iterator iter = collection.iterator();
iter.hasNext();
iter.remove();
fail("New iterator.remove should raise IllegalState " +
"even after hasNext");
} catch (IllegalStateException e) {
// expected
}
verify();
resetFull();
int size = collection.size();
Iterator iter = collection.iterator();
while (iter.hasNext()) {
Object o = iter.next();
iter.remove();
// if the elements aren't distinguishable, we can just remove a
// matching element from the confirmed collection and verify
// contents are still the same. Otherwise, we don't have the
// ability to distinguish the elements and determine which to
// remove from the confirmed collection (in which case, we don't
// verify because we don't know how).
//
// see areEqualElementsDistinguishable()
if(!areEqualElementsDistinguishable()) {
confirmed.remove(o);
verify();
}
size--;
assertEquals("Collection should shrink by one after " +
"iterator.remove", size, collection.size());
}
assertTrue("Collection should be empty after iterator purge",
collection.isEmpty());
resetFull();
iter = collection.iterator();
iter.next();
iter.remove();
try {
iter.remove();
fail("Second iter.remove should raise IllegalState");
} catch (IllegalStateException e) {
// expected
}
}
/**
* Tests {@link Collection#remove(Object)}.
*/
public void testCollectionRemove() {
if (!isRemoveSupported()) return;
resetEmpty();
Object[] elements = getFullElements();
for (int i = 0; i < elements.length; i++) {
assertTrue("Shouldn't remove nonexistent element",
!collection.remove(elements[i]));
verify();
}
Object[] other = getOtherElements();
resetFull();
for (int i = 0; i < other.length; i++) {
assertTrue("Shouldn't remove nonexistent other element",
!collection.remove(other[i]));
verify();
}
int size = collection.size();
for (int i = 0; i < elements.length; i++) {
resetFull();
assertTrue("Collection should remove extant element",
collection.remove(elements[i]));
// if the elements aren't distinguishable, we can just remove a
// matching element from the confirmed collection and verify
// contents are still the same. Otherwise, we don't have the
// ability to distinguish the elements and determine which to
// remove from the confirmed collection (in which case, we don't
// verify because we don't know how).
//
// see areEqualElementsDistinguishable()
if(!areEqualElementsDistinguishable()) {
confirmed.remove(elements[i]);
verify();
}
assertEquals("Collection should shrink after remove",
size - 1, collection.size());
}
}
/**
* Tests {@link Collection#removeAll(Collection)}.
*/
public void testCollectionRemoveAll() {
if (!isRemoveSupported()) return;
resetEmpty();
assertTrue("Emtpy collection removeAll should return false for " +
"empty input",
!collection.removeAll(Collections.EMPTY_SET));
verify();
assertTrue("Emtpy collection removeAll should return false for " +
"nonempty input",
!collection.removeAll(new ArrayList(collection)));
verify();
resetFull();
assertTrue("Full collection removeAll should return false for " +
"empty input",
!collection.removeAll(Collections.EMPTY_SET));
verify();
assertTrue("Full collection removeAll should return false for " +
"other elements",
!collection.removeAll(Arrays.asList(getOtherElements())));
verify();
assertTrue("Full collection removeAll should return true for " +
"full elements",
collection.removeAll(new HashSet(collection)));
confirmed.removeAll(new HashSet(confirmed));
verify();
resetFull();
int size = collection.size();
Collection all = Arrays.asList(getFullElements()).subList(2, 5);
assertTrue("Full collection removeAll should work",
collection.removeAll(all));
confirmed.removeAll(all);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -