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) appends e to the end of the list.
  • remove(e) deletes the first occurrence of e, keeps the remaining elements in order, and returns true, or returns false if e is not in the list.
  • indexOf(e) returns the index of the first occurrence of e, or -1 if it is not present.
  • get(i) throws IndexOutOfBoundsException if i < 0 or i >= size().
Solution
  • The empty list.
  • A single element.
  • Duplicate values.
  • The index boundaries: 0 and size() - 1 are valid, -1 and size() 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));
}