Path: blob/master/test/jdk/java/util/RandomAccess/Basic.java
41149 views
/*1* Copyright (c) 2000, 2019, 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 4327164 822933826* @summary Basic test for new RandomAccess interface27* @run testng Basic28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;3233import static org.testng.Assert.assertEquals;3435import java.util.*;36import java.util.concurrent.CopyOnWriteArrayList;37import java.util.function.Function;38import java.util.function.Supplier;3940public class Basic {4142/*43* Lists which implement Random Access interface44*/45@DataProvider(name = "testLists")46public Object[][] testData() {47var intArray = new Integer[100];48var stack = new Stack<>();49var random = new Random();50for (int i = 0; i < 100; i++) {51var r = random.nextInt(100);52stack.push(r);53intArray[i] = r;54}55List<Integer> list = Arrays.asList(intArray);56return new Object[][]{57{list, true, "Arrays.asList"},58{stack, true, "Stack"},59{new ArrayList<>(list), true, "ArrayList"},60{new LinkedList<>(list), false, "LinkedList"},61{new Vector<>(list), true, "Vector"},62{new CopyOnWriteArrayList<>(list), true, "CopyOnWriteArrayList"}63};64}6566@Test(dataProvider = "testLists")67public void testRandomAccess(List<Integer> list, boolean expectedRA, String failMsg) {6869var actualRA = list instanceof RandomAccess;70assertEquals(actualRA, expectedRA, failMsg);7172List<Integer> unmodList = Collections.unmodifiableList(list);73List<Integer> syncList = Collections.synchronizedList(list);74assertEquals((unmodList instanceof RandomAccess), actualRA,75"Unmodifiable fails to preserve RandomAccess");76assertEquals((syncList instanceof RandomAccess), actualRA,77"Synchronized fails to preserve RandomAccess");7879while (list.size() > 0) {80list = list.subList(0, list.size() - 1);81assertEquals((list instanceof RandomAccess), actualRA,82"SubList fails to preserve RandomAccess: " + list.size());8384unmodList = unmodList.subList(0, unmodList.size() - 1);85assertEquals((unmodList instanceof RandomAccess), actualRA,86"SubList(unmodifiable) fails to preserve RandomAccess: "87+ unmodList.size());8889syncList = syncList.subList(0, syncList.size() - 1);90assertEquals((syncList instanceof RandomAccess), actualRA,91"SubList(synchronized) fails to preserve RandomAccess: "92+ syncList.size());93}94}9596@Test(dataProvider = "testLists")97public void testListCopy(List<Integer> list, boolean expectedRA, String failMsg) {98ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, 0));99// Test that copy works on random & sequential access100Collections.copy(list, testCollection);101assertEquals(list, testCollection, "Copy failed: " + failMsg);102}103104@Test(dataProvider = "testLists")105public void testListFill(List<Integer> list, boolean expectedRA, String failMsg) {106ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, 0));107// Test that copy works on random & sequential access108Collections.fill(list, 0);109assertEquals(list, testCollection, "Fill failed: " + failMsg);110}111112/*113* Test that shuffle and binarySearch work the same on random and sequential access lists.114*/115@DataProvider(name = "testFactoryLists")116public Object[][] testDataFactory() {117return new Object[][]{118{"ArrayList -> LinkedList", supplier(ArrayList::new), copyCtor(LinkedList::new)},119{"CopyOnWriteArrayList -> Stack", supplier(CopyOnWriteArrayList::new),120copyCtor((list) -> { var s = new Stack();s.addAll(list);return s; })}121};122}123124private Supplier<List<Integer>> supplier(Supplier<List<Integer>> supplier) {125return supplier;126}127128private Function<List<Integer>, List<Integer>> copyCtor(Function<List<Integer>, List<Integer>> ctor) {129return ctor;130}131132@Test(dataProvider = "testFactoryLists")133public void testListShuffle(String description, Supplier<List<Integer>> randomAccessListSupplier,134Function<List<Integer>, List<Integer>> otherListFactory) {135136//e.g: ArrayList<Integer> al = new ArrayList<>();137List<Integer> l1 = randomAccessListSupplier.get();138for (int j = 0; j < 100; j++) {139l1.add(Integer.valueOf(2 * j));140}141// e.g: List<Integer> ll = new LinkedList<>(al);142List<Integer> l2 = otherListFactory.apply(l1);143for (int i = 0; i < 100; i++) {144Collections.shuffle(l1, new Random(666));145Collections.shuffle(l2, new Random(666));146assertEquals(l1, l2, "Shuffle failed: " + description);147}148}149150@Test(dataProvider = "testFactoryLists")151public void testListBinarySearch(String description, Supplier<List<Integer>> randomAccessListSupplier,152Function<List<Integer>, List<Integer>> otherListFactory) {153154//e.g: ArrayList<Integer> al = new ArrayList<>();155List<Integer> l1 = randomAccessListSupplier.get();156for (int i = 0; i < 10000; i++) {157l1.add(Integer.valueOf(2 * i));158}159// e.g: List<Integer> ll = new LinkedList<>(al);160List<Integer> l2 = otherListFactory.apply(l1);161for (int i = 0; i < 500; i++) {162Integer key = Integer.valueOf(new Random(666).nextInt(20000));163assertEquals(Collections.binarySearch(l1, key), Collections164.binarySearch(l2, key), "Binary search failed: " + description);165}166}167}168169170