Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/List/LockStep.java
38811 views
/*1* Copyright (c) 2007, 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 635997926* @summary Compare List implementations for identical behavior27* @author Martin Buchholz28* @key randomness29*/3031import java.io.*;32import java.util.*;33import java.util.concurrent.*;34import static java.util.Collections.*;3536@SuppressWarnings("unchecked")37public class LockStep {38final int DEFAULT_SIZE = 5;39int size; // Running time is O(size**2)4041int intArg(String[] args, int i, int defaultValue) {42return args.length > i ? Integer.parseInt(args[i]) : defaultValue;43}4445boolean maybe(int n) { return rnd.nextInt(n) == 0; }4647void test(String[] args) {48size = intArg(args, 0, DEFAULT_SIZE);4950lockSteps(new ArrayList(),51new LinkedList(),52new Vector());53}5455void equalLists(List... lists) {56for (List list : lists)57equalLists(list, lists[0]);58}5960void equalLists(List x, List y) {61equal(x, y);62equal(y, x);63equal(x.size(), y.size());64equal(x.isEmpty(), y.isEmpty());65equal(x.hashCode(), y.hashCode());66equal(x.toString(), y.toString());67equal(x.toArray(), y.toArray());68}6970void lockSteps(List... lists) {71for (int i = 0; i < lists.length; i++)72if (maybe(4)) lists[i] = serialClone(lists[i]);73for (final List list : lists)74testEmptyList(list);75for (int i = 0; i < size; i++) {76ListFrobber adder = randomAdder();77for (final List list : lists) {78adder.frob(list);79equal(list.size(), i+1);80}81equalLists(lists);82}83{84final ListFrobber adder = randomAdder();85final ListFrobber remover = randomRemover();86for (final List list : lists) {8788THROWS(ConcurrentModificationException.class,89new F(){void f(){90Iterator it = list.iterator();91adder.frob(list);92it.next();}},93new F(){void f(){94Iterator it = asSubList(list).iterator();95remover.frob(list);96it.next();}},97new F(){void f(){98Iterator it = asSubList(asSubList(list)).iterator();99adder.frob(list);100it.next();}},101new F(){void f(){102List subList = asSubList(list);103remover.frob(list);104subList.get(0);}},105new F(){void f(){106List sl = asSubList(list);107List ssl = asSubList(sl);108adder.frob(sl);109ssl.get(0);}},110new F(){void f(){111List sl = asSubList(list);112List ssl = asSubList(sl);113remover.frob(sl);114ssl.get(0);}});115}116}117118for (final List l : lists) {119final List sl = asSubList(l);120final List ssl = asSubList(sl);121ssl.add(0, 42);122equal(ssl.get(0), 42);123equal(sl.get(0), 42);124equal(l.get(0), 42);125final int s = l.size();126final int rndIndex = rnd.nextInt(l.size());127THROWS(IndexOutOfBoundsException.class,128new F(){void f(){l.subList(rndIndex, rndIndex).get(0);}},129new F(){void f(){l.subList(s/2, s).get(s/2 + 1);}},130new F(){void f(){l.subList(s/2, s).get(-1);}}131);132THROWS(IllegalArgumentException.class,133new F(){void f(){ l.subList(1, 0);}},134new F(){void f(){ sl.subList(1, 0);}},135new F(){void f(){ssl.subList(1, 0);}});136}137138equalLists(lists);139140for (final List list : lists) {141equalLists(list, asSubList(list));142equalLists(list, asSubList(asSubList(list)));143}144for (final List list : lists)145System.out.println(list);146147for (int i = lists[0].size(); i > 0; i--) {148ListFrobber remover = randomRemover();149for (final List list : lists)150remover.frob(list);151equalLists(lists);152}153}154155<T> List<T> asSubList(List<T> list) {156return list.subList(0, list.size());157}158159void testEmptyCollection(Collection<?> c) {160check(c.isEmpty());161equal(c.size(), 0);162equal(c.toString(),"[]");163equal(c.toArray().length, 0);164equal(c.toArray(new Object[0]).length, 0);165166Object[] a = new Object[1]; a[0] = Boolean.TRUE;167equal(c.toArray(a), a);168equal(a[0], null);169}170171void testEmptyList(List list) {172testEmptyCollection(list);173equal(list.hashCode(), 1);174equal(list, Collections.emptyList());175}176177final Random rnd = new Random();178179abstract class ListFrobber { abstract void frob(List l); }180181ListFrobber randomAdder() {182final Integer e = rnd.nextInt(1024);183final int subListCount = rnd.nextInt(3);184final boolean atBeginning = rnd.nextBoolean();185final boolean useIterator = rnd.nextBoolean();186final boolean simpleIterator = rnd.nextBoolean();187return new ListFrobber() {void frob(List l) {188final int s = l.size();189List ll = l;190for (int i = 0; i < subListCount; i++)191ll = asSubList(ll);192if (! useIterator) {193if (atBeginning) {194switch (rnd.nextInt(3)) {195case 0: ll.add(0, e); break;196case 1: ll.subList(0, rnd.nextInt(s+1)).add(0, e); break;197case 2: ll.subList(0, rnd.nextInt(s+1)).subList(0,0).add(0,e); break;198default: throw new Error();199}200} else {201switch (rnd.nextInt(3)) {202case 0: check(ll.add(e)); break;203case 1: ll.subList(s/2, s).add(s - s/2, e); break;204case 2: ll.subList(s, s).subList(0, 0).add(0, e); break;205default: throw new Error();206}207}208} else {209if (atBeginning) {210ListIterator it = ll.listIterator();211equal(it.nextIndex(), 0);212check(! it.hasPrevious());213it.add(e);214equal(it.previousIndex(), 0);215equal(it.nextIndex(), 1);216check(it.hasPrevious());217} else {218final int siz = ll.size();219ListIterator it = ll.listIterator(siz);220equal(it.previousIndex(), siz-1);221check(! it.hasNext());222it.add(e);223equal(it.previousIndex(), siz);224equal(it.nextIndex(), siz+1);225check(! it.hasNext());226check(it.hasPrevious());227}228}}};229}230231ListFrobber randomRemover() {232final int position = rnd.nextInt(3);233final int subListCount = rnd.nextInt(3);234return new ListFrobber() {void frob(List l) {235final int s = l.size();236List ll = l;237for (int i = 0; i < subListCount; i++)238ll = asSubList(ll);239switch (position) {240case 0: // beginning241switch (rnd.nextInt(3)) {242case 0: ll.remove(0); break;243case 1: {244final Iterator it = ll.iterator();245check(it.hasNext());246THROWS(IllegalStateException.class,247new F(){void f(){it.remove();}});248it.next();249it.remove();250THROWS(IllegalStateException.class,251new F(){void f(){it.remove();}});252break;}253case 2: {254final ListIterator it = ll.listIterator();255check(it.hasNext());256THROWS(IllegalStateException.class,257new F(){void f(){it.remove();}});258it.next();259it.remove();260THROWS(IllegalStateException.class,261new F(){void f(){it.remove();}});262break;}263default: throw new Error();264}265break;266case 1: // midpoint267switch (rnd.nextInt(3)) {268case 0: ll.remove(s/2); break;269case 1: {270final ListIterator it = ll.listIterator(s/2);271it.next();272it.remove();273break;274}275case 2: {276final ListIterator it = ll.listIterator(s/2+1);277it.previous();278it.remove();279break;280}281default: throw new Error();282}283break;284case 2: // end285switch (rnd.nextInt(3)) {286case 0: ll.remove(s-1); break;287case 1: ll.subList(s-1, s).clear(); break;288case 2:289final ListIterator it = ll.listIterator(s);290check(! it.hasNext());291check(it.hasPrevious());292THROWS(IllegalStateException.class,293new F(){void f(){it.remove();}});294it.previous();295equal(it.nextIndex(), s-1);296check(it.hasNext());297it.remove();298equal(it.nextIndex(), s-1);299check(! it.hasNext());300THROWS(IllegalStateException.class,301new F(){void f(){it.remove();}});302break;303default: throw new Error();304}305break;306default: throw new Error();307}}};308}309310//--------------------- Infrastructure ---------------------------311volatile int passed = 0, failed = 0;312void pass() {passed++;}313void fail() {failed++; Thread.dumpStack();}314void fail(String msg) {System.err.println(msg); fail();}315void unexpected(Throwable t) {failed++; t.printStackTrace();}316void check(boolean cond) {if (cond) pass(); else fail();}317void equal(Object x, Object y) {318if (x == null ? y == null : x.equals(y)) pass();319else fail(x + " not equal to " + y);}320<T> void equal(T[] x, T[] y) {check(Arrays.equals(x,y));}321public static void main(String[] args) throws Throwable {322new LockStep().instanceMain(args);}323void instanceMain(String[] args) throws Throwable {324try {test(args);} catch (Throwable t) {unexpected(t);}325System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);326if (failed > 0) throw new AssertionError("Some tests failed");}327abstract class F {abstract void f() throws Throwable;}328void THROWS(Class<? extends Throwable> k, F... fs) {329for (F f : fs)330try {f.f(); fail("Expected " + k.getName() + " not thrown");}331catch (Throwable t) {332if (k.isAssignableFrom(t.getClass())) pass();333else unexpected(t);}}334static byte[] serializedForm(Object obj) {335try {336ByteArrayOutputStream baos = new ByteArrayOutputStream();337new ObjectOutputStream(baos).writeObject(obj);338return baos.toByteArray();339} catch (IOException e) { throw new RuntimeException(e); }}340static Object readObject(byte[] bytes)341throws IOException, ClassNotFoundException {342InputStream is = new ByteArrayInputStream(bytes);343return new ObjectInputStream(is).readObject();}344@SuppressWarnings("unchecked")345static <T> T serialClone(T obj) {346try { return (T) readObject(serializedForm(obj)); }347catch (Exception e) { throw new RuntimeException(e); }}348}349350351