/* * Copyright (c) 2000, 2019, 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 * @bug 4327164 8229338 * @summary Basic test for new RandomAccess interface * @run testng Basic
*/
/* * Lists which implement Random Access interface
*/
@DataProvider(name = "testLists") public Object[][] testData() { var intArray = new Integer[100]; var stack = new Stack<>(); var random = new Random(); for (int i = 0; i < 100; i++) { var r = random.nextInt(100);
stack.push(r);
intArray[i] = r;
}
List<Integer> list = Arrays.asList(intArray); returnnew Object[][]{
{list, true, "Arrays.asList"},
{stack, true, "Stack"},
{new ArrayList<>(list), true, "ArrayList"},
{new LinkedList<>(list), false, "LinkedList"},
{new Vector<>(list), true, "Vector"},
{new CopyOnWriteArrayList<>(list), true, "CopyOnWriteArrayList"}
};
}
@Test(dataProvider = "testLists") publicvoid testListCopy(List<Integer> list, boolean expectedRA, String failMsg) {
ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, 0)); // Test that copy works on random & sequential access
Collections.copy(list, testCollection);
assertEquals(list, testCollection, "Copy failed: " + failMsg);
}
@Test(dataProvider = "testLists") publicvoid testListFill(List<Integer> list, boolean expectedRA, String failMsg) {
ArrayList testCollection = new ArrayList<>(Collections.nCopies(100, 0)); // Test that copy works on random & sequential access
Collections.fill(list, 0);
assertEquals(list, testCollection, "Fill failed: " + failMsg);
}
/* * Test that shuffle and binarySearch work the same on random and sequential access lists.
*/
@DataProvider(name = "testFactoryLists") public Object[][] testDataFactory() { returnnew Object[][]{
{"ArrayList -> LinkedList", supplier(ArrayList::new), copyCtor(LinkedList::new)},
{"CopyOnWriteArrayList -> Stack", supplier(CopyOnWriteArrayList::new),
copyCtor((list) -> { var s = new Stack();s.addAll(list);return s; })}
};
}
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 und die Messung sind noch experimentell.