5311N/A/*
5311N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
5311N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5311N/A *
5311N/A * This code is free software; you can redistribute it and/or modify it
5311N/A * under the terms of the GNU General Public License version 2 only, as
5311N/A * published by the Free Software Foundation.
5311N/A *
5311N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5311N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5311N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5311N/A * version 2 for more details (a copy is included in the LICENSE file that
5311N/A * accompanied this code).
5311N/A *
5311N/A * You should have received a copy of the GNU General Public License version
5311N/A * 2 along with this work; if not, write to the Free Software Foundation,
5311N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5311N/A *
5311N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5311N/A * or visit www.oracle.com if you need additional information or have any
5311N/A * questions.
5311N/A */
5311N/A
5311N/A/*
5311N/A * @test
5311N/A * @bug 7121314
5311N/A * @summary AbstractCollection.toArray(T[]) doesn't return the given array
5311N/A * in concurrent modification.
5311N/A * @author Ulf Zibis, David Holmes
5311N/A */
5311N/A
5311N/Aimport java.util.AbstractCollection;
5311N/Aimport java.util.Arrays;
5311N/Aimport java.util.Iterator;
5311N/A
5311N/Apublic class ToArrayTest {
5311N/A
5311N/A static class TestCollection<E> extends AbstractCollection<E> {
5311N/A private final E[] elements;
5311N/A private int[] sizes;
5311N/A private int nextSize;
5311N/A
5311N/A public TestCollection(E[] elements) {
5311N/A this.elements = elements;
5311N/A setSizeSequence(new int[] { elements.length });
5311N/A }
5311N/A
5311N/A /*
5311N/A * Sets the values that size() will return on each use. The next
5311N/A * call to size will return sizes[0], then sizes[1] etc. This allows us
5311N/A * to emulate a concurrent change to the contents of the collection
5311N/A * without having to perform concurrent changes. If sizes[n+1] contains
5311N/A * a larger value, the collection will appear to have shrunk when
5311N/A * iterated; if a smaller value then the collection will appear to have
5311N/A * grown when iterated.
5311N/A */
5311N/A void setSizeSequence(int... sizes) {
5311N/A this.sizes = sizes;
5311N/A nextSize = 0;
5311N/A }
5311N/A
5311N/A /* can change collection's size after each invocation */
5311N/A @Override
5311N/A public int size() {
5311N/A return sizes[nextSize == sizes.length - 1 ? nextSize : nextSize++];
5311N/A }
5311N/A
5311N/A @Override
5311N/A public Iterator<E> iterator() {
5311N/A return new Iterator<E>() {
5311N/A int pos = 0;
5311N/A
5311N/A public boolean hasNext() {
5311N/A return pos < sizes[nextSize];
5311N/A }
5311N/A public E next() {
5311N/A return elements[pos++];
5311N/A }
5311N/A public void remove() {
5311N/A throw new UnsupportedOperationException(
5311N/A "Not supported yet.");
5311N/A }
5311N/A };
5311N/A }
5311N/A }
5311N/A
5311N/A static final Object[] OBJECTS = { new Object(), new Object(), new Object() };
5311N/A static final TestCollection<?> CANDIDATE = new TestCollection<Object>(OBJECTS);
5311N/A static final int CAP = OBJECTS.length; // capacity of the CANDIDATE
5311N/A static final int LAST = CAP - 1; // last possible array index
5311N/A Object[] a;
5311N/A Object[] res;
5311N/A
5311N/A int last() {
5311N/A return a.length - 1;
5311N/A }
5311N/A
5311N/A protected void test() throws Throwable {
5311N/A // Check array type conversion
5311N/A res = new TestCollection<>(new Object[] { "1", "2" }).toArray(new String[0]);
5311N/A check(res instanceof String[]);
5311N/A check(res.length == 2);
5311N/A check(res[1] == "2");
5311N/A
5311N/A // Check incompatible type of target array
5311N/A try {
5311N/A res = CANDIDATE.toArray(new String[CAP]);
5311N/A check(false);
5311N/A } catch (Throwable t) {
5311N/A check(t instanceof ArrayStoreException);
5311N/A }
5311N/A
5311N/A // Check more elements than a.length
5311N/A a = new Object[CAP - 1]; // appears too small
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res != a);
5311N/A check(res[LAST] != null);
5311N/A
5311N/A // Check equal elements as a.length
5311N/A a = new Object[CAP]; // appears to match
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] != null);
5311N/A
5311N/A // Check equal elements as a.length
5311N/A a = new Object[CAP + 1]; // appears too big
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] == null);
5311N/A
5311N/A // Check less elements than expected, but more than a.length
5311N/A a = new Object[CAP - 2]; // appears too small
5311N/A CANDIDATE.setSizeSequence(CAP, CAP - 1);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res != a);
5311N/A check(res.length == CAP - 1);
5311N/A check(res[LAST - 1] != null);
5311N/A
5311N/A // Check less elements than expected, but equal as a.length
5311N/A a = Arrays.copyOf(OBJECTS, CAP); // appears to match
5311N/A CANDIDATE.setSizeSequence(CAP, CAP - 1);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] == null);
5311N/A
5311N/A // Check more elements than expected and more than a.length
5311N/A a = new Object[CAP - 1]; // appears to match
5311N/A CANDIDATE.setSizeSequence(CAP - 1, CAP);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res != a);
5311N/A check(res[LAST] != null);
5311N/A
5311N/A // Check more elements than expected, but equal as a.length
5311N/A a = new Object[CAP - 1]; // appears to match
5311N/A CANDIDATE.setSizeSequence(CAP - 2, CAP - 1);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] != null);
5311N/A
5311N/A // Check more elements than expected, but less than a.length
5311N/A a = Arrays.copyOf(OBJECTS, CAP); // appears to match
5311N/A CANDIDATE.setSizeSequence(CAP - 2, CAP - 1);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] == null);
5311N/A
5311N/A test_7121314();
5311N/A }
5311N/A
5311N/A /*
5311N/A * Major target of this testcase, bug 7121314.
5311N/A */
5311N/A protected void test_7121314() throws Throwable {
5311N/A // Check equal elements as a.length, but less than expected
5311N/A a = new Object[CAP - 1]; // appears too small
5311N/A CANDIDATE.setSizeSequence(CAP, CAP - 1);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] != null);
5311N/A
5311N/A // Check less elements than a.length and less than expected
5311N/A a = Arrays.copyOf(OBJECTS, CAP - 1); // appears too small
5311N/A CANDIDATE.setSizeSequence(CAP, CAP - 2);
5311N/A res = CANDIDATE.toArray(a);
5311N/A check(res == a);
5311N/A check(res[last()] == null);
5311N/A
5311N/A }
5311N/A
5311N/A public static void main(String[] args) throws Throwable {
5311N/A ToArrayTest testcase = new ToArrayTest();
5311N/A try {
5311N/A testcase.test();
5311N/A } catch (Throwable t) {
5311N/A unexpected(t);
5311N/A }
5311N/A
5311N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
5311N/A if (failed > 0) throw new Exception("Some tests failed");
5311N/A }
5311N/A
5311N/A //--------------------- Infrastructure ---------------------------
5311N/A static volatile int passed = 0, failed = 0;
5311N/A static void pass() { passed++; }
5311N/A static void fail() { failed++; Thread.dumpStack(); }
5311N/A static void fail(String msg) { System.out.println(msg); fail(); }
5311N/A static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
5311N/A static void check(boolean cond) { if (cond) pass(); else fail(); }
5311N/A static void equal(Object x, Object y) {
5311N/A if (x == null ? y == null : x.equals(y)) pass();
5311N/A else {System.out.println(x + " not equal to " + y); fail(); }
5311N/A }
5311N/A}
5311N/A
5311N/A