author | martin |
Wed, 02 Sep 2015 14:11:50 -0700 | |
changeset 32427 | c22b7e41adf3 |
parent 14856 | 92a1bcf46888 |
permissions | -rw-r--r-- |
12422
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
1 |
/* |
14856 | 2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
12422
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
4 |
* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
8 |
* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
13 |
* accompanied this code). |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
14 |
* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
18 |
* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
21 |
* questions. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
22 |
*/ |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
23 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
24 |
/* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
25 |
* @test |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
26 |
* @bug 7121314 |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
27 |
* @summary AbstractCollection.toArray(T[]) doesn't return the given array |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
28 |
* in concurrent modification. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
29 |
* @author Ulf Zibis, David Holmes |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
30 |
*/ |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
31 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
32 |
import java.util.AbstractCollection; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
33 |
import java.util.Arrays; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
34 |
import java.util.Iterator; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
35 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
36 |
public class ToArrayTest { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
37 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
38 |
static class TestCollection<E> extends AbstractCollection<E> { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
39 |
private final E[] elements; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
40 |
private int[] sizes; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
41 |
private int nextSize; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
42 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
43 |
public TestCollection(E[] elements) { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
44 |
this.elements = elements; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
45 |
setSizeSequence(new int[] { elements.length }); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
46 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
47 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
48 |
/* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
49 |
* Sets the values that size() will return on each use. The next |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
50 |
* call to size will return sizes[0], then sizes[1] etc. This allows us |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
51 |
* to emulate a concurrent change to the contents of the collection |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
52 |
* without having to perform concurrent changes. If sizes[n+1] contains |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
53 |
* a larger value, the collection will appear to have shrunk when |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
54 |
* iterated; if a smaller value then the collection will appear to have |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
55 |
* grown when iterated. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
56 |
*/ |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
57 |
void setSizeSequence(int... sizes) { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
58 |
this.sizes = sizes; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
59 |
nextSize = 0; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
60 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
61 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
62 |
/* can change collection's size after each invocation */ |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
63 |
@Override |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
64 |
public int size() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
65 |
return sizes[nextSize == sizes.length - 1 ? nextSize : nextSize++]; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
66 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
67 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
68 |
@Override |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
69 |
public Iterator<E> iterator() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
70 |
return new Iterator<E>() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
71 |
int pos = 0; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
72 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
73 |
public boolean hasNext() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
74 |
return pos < sizes[nextSize]; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
75 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
76 |
public E next() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
77 |
return elements[pos++]; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
78 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
79 |
public void remove() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
80 |
throw new UnsupportedOperationException( |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
81 |
"Not supported yet."); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
82 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
83 |
}; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
84 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
85 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
86 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
87 |
static final Object[] OBJECTS = { new Object(), new Object(), new Object() }; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
88 |
static final TestCollection<?> CANDIDATE = new TestCollection<Object>(OBJECTS); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
89 |
static final int CAP = OBJECTS.length; // capacity of the CANDIDATE |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
90 |
static final int LAST = CAP - 1; // last possible array index |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
91 |
Object[] a; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
92 |
Object[] res; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
93 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
94 |
int last() { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
95 |
return a.length - 1; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
96 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
97 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
98 |
protected void test() throws Throwable { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
99 |
// Check array type conversion |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
100 |
res = new TestCollection<>(new Object[] { "1", "2" }).toArray(new String[0]); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
101 |
check(res instanceof String[]); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
102 |
check(res.length == 2); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
103 |
check(res[1] == "2"); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
104 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
105 |
// Check incompatible type of target array |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
106 |
try { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
107 |
res = CANDIDATE.toArray(new String[CAP]); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
108 |
check(false); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
109 |
} catch (Throwable t) { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
110 |
check(t instanceof ArrayStoreException); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
111 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
112 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
113 |
// Check more elements than a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
114 |
a = new Object[CAP - 1]; // appears too small |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
115 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
116 |
check(res != a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
117 |
check(res[LAST] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
118 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
119 |
// Check equal elements as a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
120 |
a = new Object[CAP]; // appears to match |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
121 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
122 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
123 |
check(res[last()] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
124 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
125 |
// Check equal elements as a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
126 |
a = new Object[CAP + 1]; // appears too big |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
127 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
128 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
129 |
check(res[last()] == null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
130 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
131 |
// Check less elements than expected, but more than a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
132 |
a = new Object[CAP - 2]; // appears too small |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
133 |
CANDIDATE.setSizeSequence(CAP, CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
134 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
135 |
check(res != a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
136 |
check(res.length == CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
137 |
check(res[LAST - 1] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
138 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
139 |
// Check less elements than expected, but equal as a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
140 |
a = Arrays.copyOf(OBJECTS, CAP); // appears to match |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
141 |
CANDIDATE.setSizeSequence(CAP, CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
142 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
143 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
144 |
check(res[last()] == null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
145 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
146 |
// Check more elements than expected and more than a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
147 |
a = new Object[CAP - 1]; // appears to match |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
148 |
CANDIDATE.setSizeSequence(CAP - 1, CAP); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
149 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
150 |
check(res != a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
151 |
check(res[LAST] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
152 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
153 |
// Check more elements than expected, but equal as a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
154 |
a = new Object[CAP - 1]; // appears to match |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
155 |
CANDIDATE.setSizeSequence(CAP - 2, CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
156 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
157 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
158 |
check(res[last()] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
159 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
160 |
// Check more elements than expected, but less than a.length |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
161 |
a = Arrays.copyOf(OBJECTS, CAP); // appears to match |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
162 |
CANDIDATE.setSizeSequence(CAP - 2, CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
163 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
164 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
165 |
check(res[last()] == null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
166 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
167 |
test_7121314(); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
168 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
169 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
170 |
/* |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
171 |
* Major target of this testcase, bug 7121314. |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
172 |
*/ |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
173 |
protected void test_7121314() throws Throwable { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
174 |
// Check equal elements as a.length, but less than expected |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
175 |
a = new Object[CAP - 1]; // appears too small |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
176 |
CANDIDATE.setSizeSequence(CAP, CAP - 1); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
177 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
178 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
179 |
check(res[last()] != null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
180 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
181 |
// Check less elements than a.length and less than expected |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
182 |
a = Arrays.copyOf(OBJECTS, CAP - 1); // appears too small |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
183 |
CANDIDATE.setSizeSequence(CAP, CAP - 2); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
184 |
res = CANDIDATE.toArray(a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
185 |
check(res == a); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
186 |
check(res[last()] == null); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
187 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
188 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
189 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
190 |
public static void main(String[] args) throws Throwable { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
191 |
ToArrayTest testcase = new ToArrayTest(); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
192 |
try { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
193 |
testcase.test(); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
194 |
} catch (Throwable t) { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
195 |
unexpected(t); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
196 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
197 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
198 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
199 |
if (failed > 0) throw new Exception("Some tests failed"); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
200 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
201 |
|
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
202 |
//--------------------- Infrastructure --------------------------- |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
203 |
static volatile int passed = 0, failed = 0; |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
204 |
static void pass() { passed++; } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
205 |
static void fail() { failed++; Thread.dumpStack(); } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
206 |
static void fail(String msg) { System.out.println(msg); fail(); } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
207 |
static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
208 |
static void check(boolean cond) { if (cond) pass(); else fail(); } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
209 |
static void equal(Object x, Object y) { |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
210 |
if (x == null ? y == null : x.equals(y)) pass(); |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
211 |
else {System.out.println(x + " not equal to " + y); fail(); } |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
212 |
} |
7058f568e4cc
7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents:
diff
changeset
|
213 |
} |