Choosing Edge Cases
We are testing implementations of the List ADT, using the list field of a ListTest class that is a List<Integer> created fresh before each test. The relevant operations are:
add(e)appendseto the end of the list.remove(e)deletes the first occurrence ofe, keeps the remaining elements in order, and returns true, or returns false ifeis not in the list.indexOf(e)returns the index of the first occurrence ofe, or-1if it is not present.get(i)throwsIndexOutOfBoundsExceptionifi < 0ori >= size().
Solution
- The empty list.
- A single element.
- Duplicate values.
- The index boundaries:
0andsize() - 1are valid,-1andsize()throw. - Removal from each position: the first element, a middle element, the last element, the only element, and an element that is not present.
Solution
@Test
public void indexOfReturnsFirstOccurrence() {
list.add(4);
list.add(7);
list.add(7);
assertEquals(1, list.indexOf(7));
}
Solution
@Test
public void removeMiddleElementKeepsLaterElements() {
list.add(10);
list.add(20);
list.add(30);
list.remove(20);
assertEquals(30, list.get(1));
}