1 | /*
|
---|
2 | * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
---|
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
---|
4 | *
|
---|
5 | * This code is free software; you can redistribute it and/or modify it
|
---|
6 | * under the terms of the GNU General Public License version 2 only, as
|
---|
7 | * published by the Free Software Foundation.
|
---|
8 | *
|
---|
9 | * This code is distributed in the hope that it will be useful, but WITHOUT
|
---|
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
---|
12 | * version 2 for more details (a copy is included in the LICENSE file that
|
---|
13 | * accompanied this code).
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License version
|
---|
16 | * 2 along with this work; if not, write to the Free Software Foundation,
|
---|
17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
---|
18 | *
|
---|
19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
---|
20 | * or visit www.oracle.com if you need additional information or have any
|
---|
21 | * questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * @test
|
---|
26 | * @summary com.sun.tools.util.List.toArray violates Collection spec
|
---|
27 | */
|
---|
28 |
|
---|
29 | import com.sun.tools.javac.util.List;
|
---|
30 |
|
---|
31 | public class T6238612 {
|
---|
32 | public static void main(String... args) {
|
---|
33 | new T6238612().test();
|
---|
34 | }
|
---|
35 |
|
---|
36 | boolean error = false;
|
---|
37 |
|
---|
38 | // exercise the List.toArray method for a variety of lists
|
---|
39 | void test() {
|
---|
40 | test(List.<String>nil());
|
---|
41 | test(List.of("a"));
|
---|
42 | test(List.of("a", "b", "c"));
|
---|
43 | test(List.of("a", "b", "c", "d", "e", "f"));
|
---|
44 | if (error)
|
---|
45 | throw new Error("test failed");
|
---|
46 | }
|
---|
47 |
|
---|
48 | // given a list, exercise the List.toArray method for a variety of arrays
|
---|
49 | void test(List<String> list) {
|
---|
50 | int n = list.size();
|
---|
51 | if (n > 0)
|
---|
52 | test(list, new String[0]);
|
---|
53 |
|
---|
54 | if (n > 1)
|
---|
55 | test(list, new String[n - 1]);
|
---|
56 |
|
---|
57 | test(list, new String[n]);
|
---|
58 | test(list, new String[n + 1]);
|
---|
59 | test(list, new String[n + 5]);
|
---|
60 | }
|
---|
61 |
|
---|
62 | // test the List.toArray method for a particular list and array
|
---|
63 | void test(List<String> list, String[] array) {
|
---|
64 | String[] result = list.toArray(array);
|
---|
65 |
|
---|
66 | if (result.length < list.size()) {
|
---|
67 | error("returned array is too small; expected: " + list.size() + ", found: " + result.length);
|
---|
68 | return;
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (list.size() <= array.length && result != array)
|
---|
72 | error("new array wrongly created");
|
---|
73 |
|
---|
74 | int i = 0;
|
---|
75 | for (String s: list)
|
---|
76 | check(result, i++, s);
|
---|
77 |
|
---|
78 | if (i < result.length)
|
---|
79 | check(result, i, null);
|
---|
80 | }
|
---|
81 |
|
---|
82 | // check a specific element of an array
|
---|
83 | void check(String[] array, int i, String expected) {
|
---|
84 | if (!equal(array[i], expected))
|
---|
85 | error("element " + i + " incorrect; expected: " + str(expected) + ", found: " + str(array[i]));
|
---|
86 | }
|
---|
87 |
|
---|
88 | // check if two strings are both null or are equal
|
---|
89 | boolean equal(String s1, String s2) {
|
---|
90 | return (s1 == null ? s2 == null : s1.equals(s2));
|
---|
91 | }
|
---|
92 |
|
---|
93 | String str(String s) {
|
---|
94 | return (s == null ? "null" : '"' + s + '"');
|
---|
95 | }
|
---|
96 |
|
---|
97 | void error(String message) {
|
---|
98 | System.err.println(message);
|
---|
99 | error = true;
|
---|
100 | }
|
---|
101 | }
|
---|