Tue, 21 May 2013 13:50:56 +0100
8005207: test has 2 @bug tags
Reviewed-by: mcimadamore
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 */
24 /*
25 * @test
26 * @summary com.sun.tools.util.List.toArray violates Collection spec
27 */
29 import com.sun.tools.javac.util.List;
31 public class T6238612 {
32 public static void main(String... args) {
33 new T6238612().test();
34 }
36 boolean error = false;
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 }
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]);
54 if (n > 1)
55 test(list, new String[n - 1]);
57 test(list, new String[n]);
58 test(list, new String[n + 1]);
59 test(list, new String[n + 5]);
60 }
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);
66 if (result.length < list.size()) {
67 error("returned array is too small; expected: " + list.size() + ", found: " + result.length);
68 return;
69 }
71 if (list.size() <= array.length && result != array)
72 error("new array wrongly created");
74 int i = 0;
75 for (String s: list)
76 check(result, i++, s);
78 if (i < result.length)
79 check(result, i, null);
80 }
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 }
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 }
93 String str(String s) {
94 return (s == null ? "null" : '"' + s + '"');
95 }
97 void error(String message) {
98 System.err.println(message);
99 error = true;
100 }
101 }