test/java/util/Collection/MOAT.java
Print this page
@@ -171,10 +171,13 @@
Map<Integer,Integer> singletonMap = singletonMap(1,2);
equal(singletonMap.size(), 1);
testMap(singletonMap);
testImmutableMap(singletonMap);
+
+ // Protected ArrayList#removeRange
+ NewArrayList.testRemoveRange();
}
private static void checkContainsSelf(Collection<Integer> c) {
check(c.containsAll(c));
check(c.containsAll(Arrays.asList(c.toArray())));
@@ -1391,7 +1394,46 @@
}
public int size() {
return set.size();
}
}
+ private static class NewArrayList extends ArrayList<Integer> {
+ // ArrayList#removeRange is protected, so is tested
+ // from within a derived class
+ public static void testRemoveRange() {
+ final int MAX_ITEM = 8;
+ final int START_INDEX = -2;
+ final int END_INDEX = MAX_ITEM + 2;
+
+ final NewArrayList list = new NewArrayList();
+ try { list.removeRange(list.size(), list.size()); }
+ catch (Throwable t) { unexpected(t); }
+ for (int i = 0; i <= MAX_ITEM; ++i) {
+ list.add(i);
+ }
+
+ for (int fromIndex = START_INDEX; fromIndex <= END_INDEX; ++fromIndex) {
+ for (int toIndex = START_INDEX; toIndex <= END_INDEX; ++toIndex) {
+ if (fromIndex < 0
+ || toIndex > list.size()
+ || toIndex < fromIndex) {
+ final int FROM_INDEX = fromIndex;
+ final int TO_INDEX = toIndex;
+ THROWS(IndexOutOfBoundsException.class,
+ new Fun() { void f() { list.removeRange(FROM_INDEX, TO_INDEX); }});
+ } else {
+ try {
+ NewArrayList list1 = (NewArrayList)list.clone();
+ list1.removeRange(fromIndex, toIndex);
+ for (int i = 0, j = 0; i < list1.size(); ++i, ++j) {
+ if (j == fromIndex)
+ j = toIndex;
+ check(list1.get(i) == j);
+ }
+ } catch (Throwable t) { unexpected(t); }
+ }
+ }
+ }
+ }
+ }
}