Path: blob/master/test/jdk/java/util/List/SubList.java
41152 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 807913626* @library /test/lib27* @build jdk.test.lib.RandomFactory28* @run testng SubList29* @summary Basic functionality of sublists30* @key randomness31*/3233import java.util.AbstractList;34import java.util.Arrays;35import java.util.ArrayList;36import java.util.Collections;37import java.util.ConcurrentModificationException;38import java.util.Iterator;39import java.util.LinkedList;40import java.util.List;41import java.util.ListIterator;42import java.util.Random;43import java.util.Vector;4445import org.testng.annotations.Test;46import org.testng.annotations.DataProvider;4748import jdk.test.lib.RandomFactory;495051public class SubList extends org.testng.Assert {5253final Random rnd = RandomFactory.getRandom();5455@Test(dataProvider = "modifiable")56public void testAdd(List<Integer> list, int from, int to) {57List<Integer> subList = list.subList(from, to);58Integer e = rnd.nextInt();59subList.add(e);60assertEquals(list.get(to), e);61assertEquals(subList.size(), to - from + 1);62}6364@Test(dataProvider = "modifiable",65expectedExceptions = ConcurrentModificationException.class)66public void testModAdd(List<Integer> list, int from, int to) {67List<Integer> subList = list.subList(from, to);68list.add(42);69subList.add(42);70}7172@Test(dataProvider = "unresizable",73expectedExceptions = UnsupportedOperationException.class)74public void testUnmodAdd(List<Integer> list, int from, int to) {75List<Integer> subList = list.subList(from, to);76subList.add(42);77}7879@Test(dataProvider = "modifiable")80public void testAddAtPos(List<Integer> list, int from, int to) {81List<Integer> subList = list.subList(from, to);82int i = rnd.nextInt(1 + to - from);83Integer e = rnd.nextInt();84subList.add(i, e);85assertEquals(list.get(from + i), e);86assertEquals(subList.size(), to - from + 1);87}8889@Test(dataProvider = "modifiable",90expectedExceptions = ConcurrentModificationException.class)91public void testModAddAtPos(List<Integer> list, int from, int to) {92List<Integer> subList = list.subList(from, to);93list.add(42);94int i = rnd.nextInt(1 + to - from);95subList.add(i, 42);96}9798@Test(dataProvider = "unresizable",99expectedExceptions = UnsupportedOperationException.class)100public void testUnmodAddAtPos(List<Integer> list, int from, int to) {101List<Integer> subList = list.subList(from, to);102int i = rnd.nextInt(1 + to - from);103subList.add(i, 42);104}105106@Test(dataProvider = "modifiable")107public void testClear(List<Integer> list, int from, int to) {108List<Integer> subList = list.subList(from, to);109subList.clear();110assertTrue(subList.isEmpty());111assertEquals(subList.size(), 0);112}113114@Test(dataProvider = "modifiable",115expectedExceptions = ConcurrentModificationException.class)116public void testModClear(List<Integer> list, int from, int to) {117List<Integer> subList = list.subList(from, to);118list.add(42);119subList.clear();120}121122@Test(dataProvider = "unresizable",123expectedExceptions = UnsupportedOperationException.class)124public void testUnmodClear(List<Integer> list, int from, int to) {125List<Integer> subList = list.subList(from, to);126subList.clear();127}128129@Test(dataProvider = "all")130public void testEquals(List<Integer> list, int from, int to) {131List<Integer> subList1 = list.subList(from, to);132List<Integer> subList2 = list.subList(from, to);133assertTrue(subList1.equals(subList2));134assertEquals(subList1.hashCode(), subList2.hashCode());135for (int i = 0; i != 16; ++i) {136int from3 = rnd.nextInt(1 + list.size());137int to3 = from3 + rnd.nextInt(1 + list.size() - from3);138boolean equal = (to - from) == (to3 - from3);139for (int j = 0; j < to - from && j < to3 - from3; ++j)140equal &= list.get(from + j) == list.get(from3 + j);141List<Integer> subList3 = list.subList(from3, to3);142assertEquals(subList1.equals(subList3), equal);143}144}145146// @Test(dataProvider = "modifiable",147// expectedExceptions = ConcurrentModificationException.class)148// public void testModEquals(List<Integer> list, int from, int to) {149// List<Integer> subList = list.subList(from, to);150// list.add(42);151// subList.equals(subList);152// }153154@Test(dataProvider = "modifiable",155expectedExceptions = ConcurrentModificationException.class)156public void testModHashCode(List<Integer> list, int from, int to) {157List<Integer> subList = list.subList(from, to);158list.add(42);159subList.hashCode();160}161162@Test(dataProvider = "all")163public void testGet(List<Integer> list, int from, int to) {164List<Integer> subList = list.subList(from, to);165for (int i = 0; i < to - from; ++i)166assertEquals(list.get(from + i), subList.get(i));167}168169@Test(dataProvider = "modifiable",170expectedExceptions = ConcurrentModificationException.class)171public void testModGet(List<Integer> list, int from, int to) {172List<Integer> subList = list.subList(from, to);173list.add(42);174subList.get(from);175}176177@Test(dataProvider = "all")178public void testIndexOf(List<Integer> list, int from, int to) {179List<Integer> subList = list.subList(from, to);180if (from < to) {181Integer e = list.get(from);182int j = subList.indexOf(e);183assertTrue(j == 0);184}185for (int i = 0; i < list.size(); ++i) {186Integer e = list.get(i);187int j = subList.indexOf(e);188if (i < from || i >= to) {189assertTrue(j == -1 || subList.get(j) == e);190} else {191assertTrue(j >= 0);192assertTrue(j <= i - from);193assertEquals(subList.get(j), e);194}195}196for (int i = 0; i < 16; ++i) {197Integer r = rnd.nextInt();198if (list.contains(r)) continue;199int j = subList.indexOf(r);200assertTrue(j == -1);201}202}203204@Test(dataProvider = "modifiable",205expectedExceptions = ConcurrentModificationException.class)206public void testModIndexOf(List<Integer> list, int from, int to) {207List<Integer> subList = list.subList(from, to);208list.add(42);209subList.indexOf(from);210}211212@Test(dataProvider = "all")213public void testIterator(List<Integer> list, int from, int to) {214List<Integer> subList = list.subList(from, to);215Iterator<Integer> it = subList.iterator();216for (int i = from; i < to; ++i) {217assertTrue(it.hasNext());218assertEquals(list.get(i), it.next());219}220assertFalse(it.hasNext());221}222223@Test(dataProvider = "modifiable",224expectedExceptions = ConcurrentModificationException.class)225public void testModIteratorNext(List<Integer> list, int from, int to) {226List<Integer> subList = list.subList(from, to);227Iterator<Integer> it = subList.iterator();228list.add(42);229it.next();230}231232@Test(dataProvider = "modifiable")233public void testIteratorRemove(List<Integer> list, int from, int to) {234List<Integer> subList = list.subList(from, to);235Iterator<Integer> it = subList.iterator();236for (int i = from; i < to; ++i) {237assertTrue(it.hasNext());238assertEquals(list.get(from), it.next());239it.remove();240}241assertFalse(it.hasNext());242assertTrue(subList.isEmpty());243}244245@Test(dataProvider = "modifiable",246expectedExceptions = ConcurrentModificationException.class)247public void testModIteratorRemove(List<Integer> list, int from, int to) {248List<Integer> subList = list.subList(from, to);249Iterator<Integer> it = subList.iterator();250it.next();251list.add(42);252it.remove();253}254255@Test(dataProvider = "unresizable",256expectedExceptions = UnsupportedOperationException.class)257public void testUnmodIteratorRemove(List<Integer> list, int from, int to) {258List<Integer> subList = list.subList(from, to);259Iterator<Integer> it = subList.iterator();260it.next();261it.remove();262}263264@Test(dataProvider = "all")265public void testIteratorForEachRemaining(List<Integer> list, int from, int to) {266List<Integer> subList = list.subList(from, to);267for (int k = 0; k < 16; ++k) {268int r = from + rnd.nextInt(1 + to - from);269Iterator<Integer> it = subList.iterator();270for (int i = from; i < to; ++i) {271assertTrue(it.hasNext());272if (i == r) {273Iterator<Integer> jt = list.listIterator(r);274it.forEachRemaining(x ->275assertTrue(jt.hasNext() && x == jt.next()));276break;277}278assertEquals(list.get(i), it.next());279}280it.forEachRemaining(x -> fail());281}282}283284@Test(dataProvider = "all")285public void testLastIndexOf(List<Integer> list, int from, int to) {286List<Integer> subList = list.subList(from, to);287if (from < to) {288Integer e = list.get(to - 1);289int j = subList.lastIndexOf(e);290assertTrue(j == to - from - 1);291}292for (int i = 0; i < list.size(); ++i) {293Integer e = list.get(i);294int j = subList.lastIndexOf(e);295if (i < from || i >= to) {296assertTrue(j == -1 || subList.get(j) == e);297} else {298assertTrue(j >= 0 && j >= i - from);299assertEquals(subList.get(j), e);300}301}302for (int i = 0; i < 16; ++i) {303Integer r = rnd.nextInt();304if (list.contains(r)) continue;305int j = subList.lastIndexOf(r);306assertTrue(j == -1);307}308}309310@Test(dataProvider = "modifiable",311expectedExceptions = ConcurrentModificationException.class)312public void testModLastIndexOf(List<Integer> list, int from, int to) {313List<Integer> subList = list.subList(from, to);314list.add(42);315subList.lastIndexOf(42);316}317318@Test(dataProvider = "unresizable")319public void testListIterator(List<Integer> list, int from, int to) {320List<Integer> subList = list.subList(from, to);321ListIterator<Integer> it = subList.listIterator();322for (int i = from; i < to; ++i) {323assertTrue(it.hasNext());324assertTrue(it.nextIndex() == i - from);325assertEquals(list.get(i), it.next());326}327assertFalse(it.hasNext());328}329330@Test(dataProvider = "modifiable",331expectedExceptions = ConcurrentModificationException.class)332public void testModListIteratorNext(List<Integer> list, int from, int to) {333List<Integer> subList = list.subList(from, to);334ListIterator<Integer> it = subList.listIterator();335list.add(42);336it.next();337}338339@Test(dataProvider = "modifiable")340public void testListIteratorSet(List<Integer> list, int from, int to) {341List<Integer> subList = list.subList(from, to);342ListIterator<Integer> it = subList.listIterator();343for (int i = from; i < to; ++i) {344assertTrue(it.hasNext());345assertTrue(it.nextIndex() == i - from);346assertEquals(list.get(i), it.next());347Integer e = rnd.nextInt();348it.set(e);349assertEquals(list.get(i), e);350}351assertFalse(it.hasNext());352}353354@Test(dataProvider = "modifiable",355expectedExceptions = ConcurrentModificationException.class)356public void testModListIteratorSet(List<Integer> list, int from, int to) {357List<Integer> subList = list.subList(from, to);358ListIterator<Integer> it = subList.listIterator();359it.next();360list.add(42);361it.set(42);362}363364@Test(dataProvider = "unsettable",365expectedExceptions = UnsupportedOperationException.class)366public void testUnmodListIteratorSet(List<Integer> list, int from, int to) {367List<Integer> subList = list.subList(from, to);368ListIterator<Integer> it = subList.listIterator();369it.next();370it.set(42);371}372373@Test(dataProvider = "unresizable")374public void testListIteratorPrevious(List<Integer> list, int from, int to) {375List<Integer> subList = list.subList(from, to);376ListIterator<Integer> it = subList.listIterator(subList.size());377for (int i = to - 1; i >= from; --i) {378assertTrue(it.hasPrevious());379assertTrue(it.previousIndex() == i - from);380assertEquals(list.get(i), it.previous());381}382assertFalse(it.hasPrevious());383}384385@Test(dataProvider = "modifiable",386expectedExceptions = ConcurrentModificationException.class)387public void testModListIteratorPrevious(List<Integer> list, int from, int to) {388List<Integer> subList = list.subList(from, to);389ListIterator<Integer> it = subList.listIterator(to - from);390list.add(42);391it.previous();392}393394@Test(dataProvider = "modifiable")395public void testListIteratorSetPrevious(List<Integer> list, int from, int to) {396List<Integer> subList = list.subList(from, to);397ListIterator<Integer> it = subList.listIterator(subList.size());398for (int i = to - 1; i >= from; --i) {399assertTrue(it.hasPrevious());400assertTrue(it.previousIndex() == i - from);401assertEquals(list.get(i), it.previous());402Integer e = rnd.nextInt();403it.set(e);404assertEquals(list.get(i), e);405}406assertFalse(it.hasPrevious());407}408409@Test(dataProvider = "unsettable",410expectedExceptions = UnsupportedOperationException.class)411public void testUnmodListIteratorSetPrevious(List<Integer> list, int from, int to) {412List<Integer> subList = list.subList(from, to);413ListIterator<Integer> it = subList.listIterator(to - from);414it.previous();415it.set(42);416}417418@Test(dataProvider = "modifiable")419public void testListIteratorAdd(List<Integer> list, int from, int to) {420List<Integer> subList = list.subList(from, to);421for (int i = 0; i < 16; ++i) {422int r = rnd.nextInt(1 + subList.size());423ListIterator<Integer> it = subList.listIterator(r);424Integer e = rnd.nextInt();425it.add(e);426assertEquals(it.previous(), e);427assertEquals(list.get(from + r), e);428}429}430431@Test(dataProvider = "unresizable",432expectedExceptions = UnsupportedOperationException.class)433public void testUnmodListIteratorAdd(List<Integer> list, int from, int to) {434List<Integer> subList = list.subList(from, to);435int r = rnd.nextInt(1 + subList.size());436ListIterator<Integer> it = subList.listIterator(r);437it.add(42);438}439440@Test(dataProvider = "modifiable",441expectedExceptions = ConcurrentModificationException.class)442public void testModListIteratorAdd(List<Integer> list, int from, int to) {443List<Integer> subList = list.subList(from, to);444ListIterator<Integer> it = subList.listIterator();445it.next();446list.add(42);447it.add(42);448}449450@Test(dataProvider = "modifiable")451public void testListIteratorRemoveNext(List<Integer> list, int from, int to) {452List<Integer> subList = list.subList(from, to);453ListIterator<Integer> it = subList.listIterator();454for (int i = from; i < to; ++i) {455assertTrue(it.hasNext());456assertTrue(it.nextIndex() == 0);457assertEquals(list.get(from), it.next());458it.remove();459}460assertFalse(it.hasNext());461assertTrue(subList.isEmpty());462}463464@Test(dataProvider = "unresizable",465expectedExceptions = UnsupportedOperationException.class)466public void testUnmodListIteratorRemoveNext(List<Integer> list, int from, int to) {467List<Integer> subList = list.subList(from, to);468ListIterator<Integer> it = subList.listIterator();469it.next();470it.remove();471}472473@Test(dataProvider = "modifiable",474expectedExceptions = ConcurrentModificationException.class)475public void testModListIteratorRemove(List<Integer> list, int from, int to) {476List<Integer> subList = list.subList(from, to);477ListIterator<Integer> it = subList.listIterator();478it.next();479list.add(42);480it.remove();481}482483@Test(dataProvider = "modifiable")484public void testListIteratorRemovePrevious(List<Integer> list, int from, int to) {485List<Integer> subList = list.subList(from, to);486ListIterator<Integer> it = subList.listIterator(subList.size());487for (int i = to - 1; i >= from; --i) {488assertTrue(it.hasPrevious());489assertTrue(it.previousIndex() == i - from);490assertEquals(list.get(i), it.previous());491it.remove();492}493assertFalse(it.hasPrevious());494assertTrue(subList.isEmpty());495}496497@Test(dataProvider = "unresizable",498expectedExceptions = UnsupportedOperationException.class)499public void testUnmodListIteratorRemovePrevious(List<Integer> list, int from, int to) {500List<Integer> subList = list.subList(from, to);501ListIterator<Integer> it = subList.listIterator(subList.size());502it.previous();503it.remove();504}505506@Test(dataProvider = "modifiable")507public void testRemove(List<Integer> list, int from, int to) {508List<Integer> subList = list.subList(from, to);509for (int i = 0; i < 16; ++i) {510if (subList.isEmpty()) break;511int r = rnd.nextInt(subList.size());512Integer e = list.get(from + r);513assertEquals(subList.remove(r), e);514}515}516517@Test(dataProvider = "unresizable",518expectedExceptions = UnsupportedOperationException.class)519public void testUnmodRemove(List<Integer> list, int from, int to) {520List<Integer> subList = list.subList(from, to);521int r = rnd.nextInt(subList.size());522subList.remove(r);523}524525@Test(dataProvider = "modifiable",526expectedExceptions = ConcurrentModificationException.class)527public void testModRemove(List<Integer> list, int from, int to) {528List<Integer> subList = list.subList(from, to);529list.add(42);530subList.remove(0);531}532533@Test(dataProvider = "modifiable")534public void testSet(List<Integer> list, int from, int to) {535List<Integer> subList = list.subList(from, to);536for (int i = 0; i < to - from; ++i) {537Integer e0 = list.get(from + i);538Integer e1 = rnd.nextInt();539assertEquals(subList.set(i, e1), e0);540assertEquals(list.get(from + i), e1);541}542}543544@Test(dataProvider = "modifiable",545expectedExceptions = ConcurrentModificationException.class)546public void testModSet(List<Integer> list, int from, int to) {547List<Integer> subList = list.subList(from, to);548list.add(42);549subList.set(0, 42);550}551552@Test(dataProvider = "all")553public void testSubList(List<Integer> list, int from, int to) {554List<Integer> subList = list.subList(from, to);555for (int i = 0; i < 16 && from < to; ++i) {556int from1 = rnd.nextInt(to - from);557int to1 = from1 + 1 + rnd.nextInt(to - from - from1);558List<Integer> subSubList = subList.subList(from1, to1);559for (int j = 0; j < to1 - from1; ++j)560assertEquals(list.get(from + from1 + j), subSubList.get(j));561}562}563564/**565* All kinds of lists566*/567@DataProvider568public static Object[][] all() {569Object[][] l1 = modifiable();570Object[][] l2 = unresizable();571Object[][] res = Arrays.copyOf(l1, l1.length + l2.length);572System.arraycopy(l2, 0, res, l1.length, l2.length);573return res;574}575576/**577* Lists that allow any modifications: resizing and setting values578*/579@DataProvider580public static Object[][] modifiable() {581final List<Integer> c1 = Arrays.asList(42);582final List<Integer> c9 = Arrays.asList(40, 41, 42, 43, 44, 45, -1,583Integer.MIN_VALUE, 1000500);584585return new Object[][] {586{new ArrayList<>(c1), 0, 1},587{new LinkedList<>(c1), 0, 1},588{new Vector<>(c1), 0, 1},589{new ArrayList<>(c1).subList(0, 1), 0, 1},590{new LinkedList<>(c1).subList(0, 1), 0, 1},591{new Vector<>(c1).subList(0, 1), 0, 1},592{Collections.checkedList(new ArrayList<>(c1), Integer.class), 0, 1},593{Collections.checkedList(new LinkedList<>(c1), Integer.class), 0, 1},594{Collections.checkedList(new Vector<>(c1), Integer.class), 0, 1},595{Collections.synchronizedList(new ArrayList<>(c1)), 0, 1},596{Collections.synchronizedList(new LinkedList<>(c1)), 0, 1},597{Collections.synchronizedList(new Vector<>(c1)), 0, 1},598599{new ArrayList<>(c9), 2, 5},600{new LinkedList<>(c9), 2, 5},601{new Vector<>(c9), 2, 5},602{new ArrayList<>(c9).subList(1, 8), 1, 4},603{new LinkedList<>(c9).subList(1, 8), 1, 4},604{new Vector<>(c9).subList(1, 8), 1, 4},605{Collections.checkedList(new ArrayList<>(c9), Integer.class), 2, 5},606{Collections.checkedList(new LinkedList<>(c9), Integer.class), 2, 5},607{Collections.checkedList(new Vector<>(c9), Integer.class), 2, 5},608{Collections.synchronizedList(new ArrayList<>(c9)), 2, 5},609{Collections.synchronizedList(new LinkedList<>(c9)), 2, 5},610{Collections.synchronizedList(new Vector<>(c9)), 2, 5},611};612}613614/**615* Lists that don't allow resizing, but allow setting values616*/617@DataProvider618public static Object[][] unresizable() {619final List<Integer> c1 = Arrays.asList(42);620final List<Integer> c9 = Arrays.asList(40, 41, 42, 43, 44, 45, -1,621Integer.MIN_VALUE, 1000500);622623Object[][] l1 = unsettable();624Object[][] l2 = {625{c1, 0, 1},626{c1.subList(0, 1), 0, 1},627{Collections.checkedList(c1, Integer.class), 0, 1},628{Collections.synchronizedList(c1), 0, 1},629{c9, 0, 4},630{c9, 4, 6},631{c9.subList(1, 8), 1, 4},632{c9.subList(1, 8), 0, 7},633{Collections.checkedList(c9, Integer.class), 3, 6},634{Collections.synchronizedList(c9), 3, 5},635};636Object[][] res = Arrays.copyOf(l1, l1.length + l2.length);637System.arraycopy(l2, 0, res, l1.length, l2.length);638return res;639}640641/**642* Lists that don't allow either resizing or setting values643*/644@DataProvider645public static Object[][] unsettable() {646final List<Integer> c1 = Arrays.asList(42);647final List<Integer> c9 = Arrays.asList(40, 41, 42, 43, 44, 45, -1,648Integer.MIN_VALUE, 1000500);649650return new Object[][] {651{new MyList(1), 0, 1},652{new MyList(1).subList(0, 1), 0, 1},653{Collections.singletonList(42), 0, 1},654{Collections.singletonList(42).subList(0, 1), 0, 1},655{Collections.unmodifiableList(c1), 0, 1},656{Collections.unmodifiableList(new ArrayList<>(c1)), 0, 1},657{Collections.unmodifiableList(new LinkedList<>(c1)), 0, 1},658{Collections.unmodifiableList(new Vector<>(c1)), 0, 1},659660{new MyList(9), 3, 6},661{new MyList(9).subList(2, 8), 3, 6},662{Collections.unmodifiableList(c9), 3, 6},663{Collections.unmodifiableList(new ArrayList<>(c9)), 3, 6},664{Collections.unmodifiableList(new LinkedList<>(c9)), 3, 6},665{Collections.unmodifiableList(new Vector<>(c9)), 3, 6},666};667}668669static class MyList extends AbstractList<Integer> {670private int size;671MyList(int s) { size = s; }672public Integer get(int index) { return 42; }673public int size() { return size; }674}675}676677678