0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6359979
0N/A * @summary Compare List implementations for identical behavior
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport static java.util.Collections.*;
0N/A
0N/A@SuppressWarnings("unchecked")
0N/Apublic class LockStep {
0N/A final int DEFAULT_SIZE = 5;
0N/A int size; // Running time is O(size**2)
0N/A
0N/A int intArg(String[] args, int i, int defaultValue) {
0N/A return args.length > i ? Integer.parseInt(args[i]) : defaultValue;
0N/A }
0N/A
0N/A boolean maybe(int n) { return rnd.nextInt(n) == 0; }
0N/A
0N/A void test(String[] args) {
0N/A size = intArg(args, 0, DEFAULT_SIZE);
0N/A
0N/A lockSteps(new ArrayList(),
0N/A new LinkedList(),
0N/A new Vector());
0N/A }
0N/A
0N/A void equalLists(List... lists) {
0N/A for (List list : lists)
0N/A equalLists(list, lists[0]);
0N/A }
0N/A
0N/A void equalLists(List x, List y) {
0N/A equal(x, y);
0N/A equal(y, x);
0N/A equal(x.size(), y.size());
0N/A equal(x.isEmpty(), y.isEmpty());
0N/A equal(x.hashCode(), y.hashCode());
0N/A equal(x.toString(), y.toString());
0N/A equal(x.toArray(), y.toArray());
0N/A }
0N/A
0N/A void lockSteps(List... lists) {
0N/A for (int i = 0; i < lists.length; i++)
0N/A if (maybe(4)) lists[i] = serialClone(lists[i]);
0N/A for (final List list : lists)
0N/A testEmptyList(list);
0N/A for (int i = 0; i < size; i++) {
0N/A ListFrobber adder = randomAdder();
0N/A for (final List list : lists) {
0N/A adder.frob(list);
0N/A equal(list.size(), i+1);
0N/A }
0N/A equalLists(lists);
0N/A }
0N/A {
0N/A final ListFrobber adder = randomAdder();
0N/A final ListFrobber remover = randomRemover();
0N/A for (final List list : lists) {
0N/A
0N/A THROWS(ConcurrentModificationException.class,
0N/A new F(){void f(){
0N/A Iterator it = list.iterator();
0N/A adder.frob(list);
0N/A it.next();}},
0N/A new F(){void f(){
0N/A Iterator it = asSubList(list).iterator();
0N/A remover.frob(list);
0N/A it.next();}},
0N/A new F(){void f(){
0N/A Iterator it = asSubList(asSubList(list)).iterator();
0N/A adder.frob(list);
0N/A it.next();}},
0N/A new F(){void f(){
0N/A List subList = asSubList(list);
0N/A remover.frob(list);
0N/A subList.get(0);}},
0N/A new F(){void f(){
0N/A List sl = asSubList(list);
0N/A List ssl = asSubList(sl);
0N/A adder.frob(sl);
0N/A ssl.get(0);}},
0N/A new F(){void f(){
0N/A List sl = asSubList(list);
0N/A List ssl = asSubList(sl);
0N/A remover.frob(sl);
0N/A ssl.get(0);}});
0N/A }
0N/A }
0N/A
0N/A for (final List l : lists) {
0N/A final List sl = asSubList(l);
0N/A final List ssl = asSubList(sl);
0N/A ssl.add(0, 42);
0N/A equal(ssl.get(0), 42);
0N/A equal(sl.get(0), 42);
0N/A equal(l.get(0), 42);
0N/A final int s = l.size();
0N/A final int rndIndex = rnd.nextInt(l.size());
0N/A THROWS(IndexOutOfBoundsException.class,
0N/A new F(){void f(){l.subList(rndIndex, rndIndex).get(0);}},
0N/A new F(){void f(){l.subList(s/2, s).get(s/2 + 1);}},
0N/A new F(){void f(){l.subList(s/2, s).get(-1);}}
0N/A );
0N/A THROWS(IllegalArgumentException.class,
0N/A new F(){void f(){ l.subList(1, 0);}},
0N/A new F(){void f(){ sl.subList(1, 0);}},
0N/A new F(){void f(){ssl.subList(1, 0);}});
0N/A }
0N/A
0N/A equalLists(lists);
0N/A
0N/A for (final List list : lists) {
0N/A equalLists(list, asSubList(list));
0N/A equalLists(list, asSubList(asSubList(list)));
0N/A }
0N/A for (final List list : lists)
0N/A System.out.println(list);
0N/A
0N/A for (int i = lists[0].size(); i > 0; i--) {
0N/A ListFrobber remover = randomRemover();
0N/A for (final List list : lists)
0N/A remover.frob(list);
0N/A equalLists(lists);
0N/A }
0N/A }
0N/A
0N/A <T> List<T> asSubList(List<T> list) {
0N/A return list.subList(0, list.size());
0N/A }
0N/A
0N/A void testEmptyCollection(Collection<?> c) {
0N/A check(c.isEmpty());
0N/A equal(c.size(), 0);
0N/A equal(c.toString(),"[]");
0N/A equal(c.toArray().length, 0);
0N/A equal(c.toArray(new Object[0]).length, 0);
0N/A
0N/A Object[] a = new Object[1]; a[0] = Boolean.TRUE;
0N/A equal(c.toArray(a), a);
0N/A equal(a[0], null);
0N/A }
0N/A
0N/A void testEmptyList(List list) {
0N/A testEmptyCollection(list);
0N/A equal(list.hashCode(), 1);
0N/A equal(list, Collections.emptyList());
0N/A }
0N/A
0N/A final Random rnd = new Random();
0N/A
0N/A abstract class ListFrobber { abstract void frob(List l); }
0N/A
0N/A ListFrobber randomAdder() {
0N/A final Integer e = rnd.nextInt(1024);
0N/A final int subListCount = rnd.nextInt(3);
0N/A final boolean atBeginning = rnd.nextBoolean();
0N/A final boolean useIterator = rnd.nextBoolean();
0N/A final boolean simpleIterator = rnd.nextBoolean();
0N/A return new ListFrobber() {void frob(List l) {
0N/A final int s = l.size();
0N/A List ll = l;
0N/A for (int i = 0; i < subListCount; i++)
0N/A ll = asSubList(ll);
0N/A if (! useIterator) {
0N/A if (atBeginning) {
0N/A switch (rnd.nextInt(3)) {
0N/A case 0: ll.add(0, e); break;
0N/A case 1: ll.subList(0, rnd.nextInt(s+1)).add(0, e); break;
0N/A case 2: ll.subList(0, rnd.nextInt(s+1)).subList(0,0).add(0,e); break;
0N/A default: throw new Error();
0N/A }
0N/A } else {
0N/A switch (rnd.nextInt(3)) {
0N/A case 0: check(ll.add(e)); break;
0N/A case 1: ll.subList(s/2, s).add(s - s/2, e); break;
0N/A case 2: ll.subList(s, s).subList(0, 0).add(0, e); break;
0N/A default: throw new Error();
0N/A }
0N/A }
0N/A } else {
0N/A if (atBeginning) {
0N/A ListIterator it = ll.listIterator();
0N/A equal(it.nextIndex(), 0);
0N/A check(! it.hasPrevious());
0N/A it.add(e);
0N/A equal(it.previousIndex(), 0);
0N/A equal(it.nextIndex(), 1);
0N/A check(it.hasPrevious());
0N/A } else {
0N/A final int siz = ll.size();
0N/A ListIterator it = ll.listIterator(siz);
0N/A equal(it.previousIndex(), siz-1);
0N/A check(! it.hasNext());
0N/A it.add(e);
0N/A equal(it.previousIndex(), siz);
0N/A equal(it.nextIndex(), siz+1);
0N/A check(! it.hasNext());
0N/A check(it.hasPrevious());
0N/A }
0N/A }}};
0N/A }
0N/A
0N/A ListFrobber randomRemover() {
0N/A final int position = rnd.nextInt(3);
0N/A final int subListCount = rnd.nextInt(3);
0N/A return new ListFrobber() {void frob(List l) {
0N/A final int s = l.size();
0N/A List ll = l;
0N/A for (int i = 0; i < subListCount; i++)
0N/A ll = asSubList(ll);
0N/A switch (position) {
0N/A case 0: // beginning
0N/A switch (rnd.nextInt(3)) {
0N/A case 0: ll.remove(0); break;
0N/A case 1: {
0N/A final Iterator it = ll.iterator();
0N/A check(it.hasNext());
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A it.next();
0N/A it.remove();
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A break;}
0N/A case 2: {
0N/A final ListIterator it = ll.listIterator();
0N/A check(it.hasNext());
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A it.next();
0N/A it.remove();
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A break;}
0N/A default: throw new Error();
0N/A }
0N/A break;
0N/A case 1: // midpoint
0N/A switch (rnd.nextInt(3)) {
0N/A case 0: ll.remove(s/2); break;
0N/A case 1: {
0N/A final ListIterator it = ll.listIterator(s/2);
0N/A it.next();
0N/A it.remove();
0N/A break;
0N/A }
0N/A case 2: {
0N/A final ListIterator it = ll.listIterator(s/2+1);
0N/A it.previous();
0N/A it.remove();
0N/A break;
0N/A }
0N/A default: throw new Error();
0N/A }
0N/A break;
0N/A case 2: // end
0N/A switch (rnd.nextInt(3)) {
0N/A case 0: ll.remove(s-1); break;
0N/A case 1: ll.subList(s-1, s).clear(); break;
0N/A case 2:
0N/A final ListIterator it = ll.listIterator(s);
0N/A check(! it.hasNext());
0N/A check(it.hasPrevious());
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A it.previous();
0N/A equal(it.nextIndex(), s-1);
0N/A check(it.hasNext());
0N/A it.remove();
0N/A equal(it.nextIndex(), s-1);
0N/A check(! it.hasNext());
0N/A THROWS(IllegalStateException.class,
0N/A new F(){void f(){it.remove();}});
0N/A break;
0N/A default: throw new Error();
0N/A }
0N/A break;
0N/A default: throw new Error();
0N/A }}};
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A volatile int passed = 0, failed = 0;
0N/A void pass() {passed++;}
0N/A void fail() {failed++; Thread.dumpStack();}
0N/A void fail(String msg) {System.err.println(msg); fail();}
0N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A void check(boolean cond) {if (cond) pass(); else fail();}
0N/A void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A <T> void equal(T[] x, T[] y) {check(Arrays.equals(x,y));}
0N/A public static void main(String[] args) throws Throwable {
0N/A new LockStep().instanceMain(args);}
0N/A void instanceMain(String[] args) throws Throwable {
0N/A try {test(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A abstract class F {abstract void f() throws Throwable;}
0N/A void THROWS(Class<? extends Throwable> k, F... fs) {
0N/A for (F f : fs)
0N/A try {f.f(); fail("Expected " + k.getName() + " not thrown");}
0N/A catch (Throwable t) {
0N/A if (k.isAssignableFrom(t.getClass())) pass();
0N/A else unexpected(t);}}
0N/A static byte[] serializedForm(Object obj) {
0N/A try {
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A new ObjectOutputStream(baos).writeObject(obj);
0N/A return baos.toByteArray();
0N/A } catch (IOException e) { throw new RuntimeException(e); }}
0N/A static Object readObject(byte[] bytes)
0N/A throws IOException, ClassNotFoundException {
0N/A InputStream is = new ByteArrayInputStream(bytes);
0N/A return new ObjectInputStream(is).readObject();}
0N/A @SuppressWarnings("unchecked")
0N/A static <T> T serialClone(T obj) {
0N/A try { return (T) readObject(serializedForm(obj)); }
0N/A catch (Exception e) { throw new RuntimeException(e); }}
0N/A}