📄 abstracttestlist.java
字号:
try {
list.get(Integer.MIN_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.get(-1);
fail("List.get should throw IndexOutOfBoundsException [-1]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.get(getFullElements().length);
fail("List.get should throw IndexOutOfBoundsException [size]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.get(Integer.MAX_VALUE);
fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
/**
* Tests {@link List#indexOf}.
*/
public void testListIndexOf() {
resetFull();
List list1 = getList();
List list2 = getConfirmedList();
Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
assertEquals("indexOf should return correct result",
list1.indexOf(element), list2.indexOf(element));
verify();
}
Object[] other = getOtherElements();
for (int i = 0; i < other.length; i++) {
assertEquals("indexOf should return -1 for nonexistent element",
list1.indexOf(other[i]), -1);
verify();
}
}
/**
* Tests {@link List#lastIndexOf}.
*/
public void testListLastIndexOf() {
resetFull();
List list1 = getList();
List list2 = getConfirmedList();
Iterator iterator = list2.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
assertEquals("lastIndexOf should return correct result",
list1.lastIndexOf(element), list2.lastIndexOf(element));
verify();
}
Object[] other = getOtherElements();
for (int i = 0; i < other.length; i++) {
assertEquals("lastIndexOf should return -1 for nonexistent " +
"element", list1.lastIndexOf(other[i]), -1);
verify();
}
}
/**
* Tests bounds checking for {@link List#set(int,Object)} on an
* empty list.
*/
public void testListSetByIndexBoundsChecking() {
if (!isSetSupported()) {
return;
}
List list = makeEmptyList();
Object element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.set(-1, element);
fail("List.set should throw IndexOutOfBoundsException [-1]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.set(0, element);
fail("List.set should throw IndexOutOfBoundsException [0]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.set(1, element);
fail("List.set should throw IndexOutOfBoundsException [1]");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
list.set(Integer.MAX_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
/**
* Tests bounds checking for {@link List#set(int,Object)} on a
* full list.
*/
public void testListSetByIndexBoundsChecking2() {
if (!isSetSupported()) return;
List list = makeFullList();
Object element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException " +
"[Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.set(-1, element);
fail("List.set should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.set(getFullElements().length, element);
fail("List.set should throw IndexOutOfBoundsException [size]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.set(Integer.MAX_VALUE, element);
fail("List.set should throw IndexOutOfBoundsException " +
"[Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
}
/**
* Test {@link List#set(int,Object)}.
*/
public void testListSetByIndex() {
if (!isSetSupported()) return;
resetFull();
Object[] elements = getFullElements();
Object[] other = getOtherElements();
for (int i = 0; i < elements.length; i++) {
Object n = other[i % other.length];
Object v = ((List)collection).set(i, n);
assertEquals("Set should return correct element", elements[i], v);
((List)confirmed).set(i, n);
verify();
}
}
/**
* If {@link #isSetSupported()} returns false, tests that set operation
* raises <Code>UnsupportedOperationException.
*/
public void testUnsupportedSet() {
if (isSetSupported()) return;
resetFull();
try {
((List) collection).set(0, new Object());
fail("Emtpy collection should not support set.");
} catch (UnsupportedOperationException e) {
// expected
}
// make sure things didn't change even if the expected exception was
// thrown.
verify();
}
/**
* Tests bounds checking for {@link List#remove(int)} on an
* empty list.
*/
public void testListRemoveByIndexBoundsChecking() {
if (!isRemoveSupported()) return;
List list = makeEmptyList();
try {
list.remove(Integer.MIN_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(0);
fail("List.remove should throw IndexOutOfBoundsException [0]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(1);
fail("List.remove should throw IndexOutOfBoundsException [1]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(Integer.MAX_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
}
/**
* Tests bounds checking for {@link List#remove(int)} on a
* full list.
*/
public void testListRemoveByIndexBoundsChecking2() {
if (!isRemoveSupported()) return;
List list = makeFullList();
try {
list.remove(Integer.MIN_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(getFullElements().length);
fail("List.remove should throw IndexOutOfBoundsException [size]");
} catch(IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(Integer.MAX_VALUE);
fail("List.remove should throw IndexOutOfBoundsException " +
"[Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
}
/**
* Tests {@link List#remove(int)}.
*/
public void testListRemoveByIndex() {
if (!isRemoveSupported()) return;
int max = getFullElements().length;
for (int i = 0; i < max; i++) {
resetFull();
Object o1 = ((List)collection).remove(i);
Object o2 = ((List)confirmed).remove(i);
assertEquals("remove should return correct element", o1, o2);
verify();
}
}
/**
* Tests the read-only bits of {@link List#listIterator()}.
*/
public void testListListIterator() {
resetFull();
forwardTest(getList().listIterator(), 0);
backwardTest(getList().listIterator(), 0);
}
/**
* Tests the read-only bits of {@link List#listIterator(int)}.
*/
public void testListListIteratorByIndex() {
resetFull();
try {
getList().listIterator(-1);
} catch (IndexOutOfBoundsException ex) {}
resetFull();
try {
getList().listIterator(getList().size() + 1);
} catch (IndexOutOfBoundsException ex) {}
resetFull();
for (int i = 0; i <= confirmed.size(); i++) {
forwardTest(getList().listIterator(i), i);
backwardTest(getList().listIterator(i), i);
}
resetFull();
for (int i = 0; i <= confirmed.size(); i++) {
backwardTest(getList().listIterator(i), i);
}
}
//-----------------------------------------------------------------------
/**
* Tests remove on list iterator is correct.
*/
public void testListListIteratorPreviousRemoveNext() {
if (isRemoveSupported() == false) return;
resetFull();
if (collection.size() < 4) return;
ListIterator it = getList().listIterator();
Object zero = it.next();
Object one = it.next();
Object two = it.next();
Object two2 = it.previous();
Object one2 = it.previous();
assertEquals(one, one2);
assertEquals(two, two2);
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
assertEquals(two, getList().get(2));
it.remove(); // removed element at index 1 (one)
assertEquals(zero, getList().get(0));
assertEquals(two, getList().get(1));
Object two3 = it.next(); // do next after remove
assertEquals(two, two3);
assertEquals(collection.size() > 2, it.hasNext());
assertEquals(true, it.hasPrevious());
}
/**
* Tests remove on list iterator is correct.
*/
public void testListListIteratorPreviousRemovePrevious() {
if (isRemoveSupported() == false) return;
resetFull();
if (collection.size() < 4) return;
ListIterator it = getList().listIterator();
Object zero = it.next();
Object one = it.next();
Object two = it.next();
Object two2 = it.previous();
Object one2 = it.previous();
assertEquals(one, one2);
assertEquals(two, two2);
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
assertEquals(two, getList().get(2));
it.remove(); // removed element at index 1 (one)
assertEquals(zero, getList().get(0));
assertEquals(two, getList().get(1));
Object zero3 = it.previous(); // do previous after remove
assertEquals(zero, zero3);
assertEquals(false, it.hasPrevious());
assertEquals(collection.size() > 2, it.hasNext());
}
/**
* Tests remove on list iterator is correct.
*/
public void testListListIteratorNextRemoveNext() {
if (isRemoveSupported() == false) return;
resetFull();
if (collection.size() < 4) return;
ListIterator it = getList().listIterator();
Object zero = it.next();
Object one = it.next();
Object two = it.next();
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
assertEquals(two, getList().get(2));
Object three = getList().get(3);
it.remove(); // removed element at index 2 (two)
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
Object three2 = it.next(); // do next after remove
assertEquals(three, three2);
assertEquals(collection.size() > 3, it.hasNext());
assertEquals(true, it.hasPrevious());
}
/**
* Tests remove on list iterator is correct.
*/
public void testListListIteratorNextRemovePrevious() {
if (isRemoveSupported() == false) return;
resetFull();
if (collection.size() < 4) return;
ListIterator it = getList().listIterator();
Object zero = it.next();
Object one = it.next();
Object two = it.next();
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
assertEquals(two, getList().get(2));
it.remove(); // removed element at index 2 (two)
assertEquals(zero, getList().get(0));
assertEquals(one, getList().get(1));
Object one2 = it.previous(); // do previous after remove
assertEquals(one, one2);
assertEquals(true, it.hasNext());
assertEquals(true, it.hasPrevious());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -