/* * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
@Test(dataProvider = "all") publicvoid testIterator(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
Iterator<Integer> it = subList.iterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext());
assertEquals(list.get(i), it.next());
}
assertFalse(it.hasNext());
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModIteratorNext(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
Iterator<Integer> it = subList.iterator();
list.add(42);
it.next();
}
@Test(dataProvider = "modifiable") publicvoid testIteratorRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
Iterator<Integer> it = subList.iterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext());
assertEquals(list.get(from), it.next());
it.remove();
}
assertFalse(it.hasNext());
assertTrue(subList.isEmpty());
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModIteratorRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
Iterator<Integer> it = subList.iterator();
it.next();
list.add(42);
it.remove();
}
@Test(dataProvider = "unresizable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodIteratorRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
Iterator<Integer> it = subList.iterator();
it.next();
it.remove();
}
@Test(dataProvider = "all") publicvoid testIteratorForEachRemaining(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); for (int k = 0; k < 16; ++k) { int r = from + rnd.nextInt(1 + to - from);
Iterator<Integer> it = subList.iterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext()); if (i == r) {
Iterator<Integer> jt = list.listIterator(r);
it.forEachRemaining(x ->
assertTrue(jt.hasNext() && x == jt.next())); break;
}
assertEquals(list.get(i), it.next());
}
it.forEachRemaining(x -> fail());
}
}
@Test(dataProvider = "all") publicvoid testLastIndexOf(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); if (from < to) {
Integer e = list.get(to - 1); int j = subList.lastIndexOf(e);
assertTrue(j == to - from - 1);
} for (int i = 0; i < list.size(); ++i) {
Integer e = list.get(i); int j = subList.lastIndexOf(e); if (i < from || i >= to) {
assertTrue(j == -1 || subList.get(j) == e);
} else {
assertTrue(j >= 0 && j >= i - from);
assertEquals(subList.get(j), e);
}
} for (int i = 0; i < 16; ++i) {
Integer r = rnd.nextInt(); if (list.contains(r)) continue; int j = subList.lastIndexOf(r);
assertTrue(j == -1);
}
}
@Test(dataProvider = "unresizable") publicvoid testListIterator(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext());
assertTrue(it.nextIndex() == i - from);
assertEquals(list.get(i), it.next());
}
assertFalse(it.hasNext());
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModListIteratorNext(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
list.add(42);
it.next();
}
@Test(dataProvider = "modifiable") publicvoid testListIteratorSet(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext());
assertTrue(it.nextIndex() == i - from);
assertEquals(list.get(i), it.next());
Integer e = rnd.nextInt();
it.set(e);
assertEquals(list.get(i), e);
}
assertFalse(it.hasNext());
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModListIteratorSet(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
it.next();
list.add(42);
it.set(42);
}
@Test(dataProvider = "unsettable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodListIteratorSet(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
it.next();
it.set(42);
}
@Test(dataProvider = "unresizable") publicvoid testListIteratorPrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(subList.size()); for (int i = to - 1; i >= from; --i) {
assertTrue(it.hasPrevious());
assertTrue(it.previousIndex() == i - from);
assertEquals(list.get(i), it.previous());
}
assertFalse(it.hasPrevious());
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModListIteratorPrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(to - from);
list.add(42);
it.previous();
}
@Test(dataProvider = "modifiable") publicvoid testListIteratorSetPrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(subList.size()); for (int i = to - 1; i >= from; --i) {
assertTrue(it.hasPrevious());
assertTrue(it.previousIndex() == i - from);
assertEquals(list.get(i), it.previous());
Integer e = rnd.nextInt();
it.set(e);
assertEquals(list.get(i), e);
}
assertFalse(it.hasPrevious());
}
@Test(dataProvider = "unsettable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodListIteratorSetPrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(to - from);
it.previous();
it.set(42);
}
@Test(dataProvider = "modifiable") publicvoid testListIteratorAdd(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); for (int i = 0; i < 16; ++i) { int r = rnd.nextInt(1 + subList.size());
ListIterator<Integer> it = subList.listIterator(r);
Integer e = rnd.nextInt();
it.add(e);
assertEquals(it.previous(), e);
assertEquals(list.get(from + r), e);
}
}
@Test(dataProvider = "unresizable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodListIteratorAdd(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); int r = rnd.nextInt(1 + subList.size());
ListIterator<Integer> it = subList.listIterator(r);
it.add(42);
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModListIteratorAdd(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
it.next();
list.add(42);
it.add(42);
}
@Test(dataProvider = "modifiable") publicvoid testListIteratorRemoveNext(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(); for (int i = from; i < to; ++i) {
assertTrue(it.hasNext());
assertTrue(it.nextIndex() == 0);
assertEquals(list.get(from), it.next());
it.remove();
}
assertFalse(it.hasNext());
assertTrue(subList.isEmpty());
}
@Test(dataProvider = "unresizable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodListIteratorRemoveNext(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
it.next();
it.remove();
}
@Test(dataProvider = "modifiable",
expectedExceptions = ConcurrentModificationException.class) publicvoid testModListIteratorRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator();
it.next();
list.add(42);
it.remove();
}
@Test(dataProvider = "modifiable") publicvoid testListIteratorRemovePrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(subList.size()); for (int i = to - 1; i >= from; --i) {
assertTrue(it.hasPrevious());
assertTrue(it.previousIndex() == i - from);
assertEquals(list.get(i), it.previous());
it.remove();
}
assertFalse(it.hasPrevious());
assertTrue(subList.isEmpty());
}
@Test(dataProvider = "unresizable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodListIteratorRemovePrevious(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to);
ListIterator<Integer> it = subList.listIterator(subList.size());
it.previous();
it.remove();
}
@Test(dataProvider = "modifiable") publicvoid testRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); for (int i = 0; i < 16; ++i) { if (subList.isEmpty()) break; int r = rnd.nextInt(subList.size());
Integer e = list.get(from + r);
assertEquals(subList.remove(r), e);
}
}
@Test(dataProvider = "unresizable",
expectedExceptions = UnsupportedOperationException.class) publicvoid testUnmodRemove(List<Integer> list, int from, int to) {
List<Integer> subList = list.subList(from, to); int r = rnd.nextInt(subList.size());
subList.remove(r);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.