T6238612.java revision 553
4340N/A/*
4340N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
4340N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4340N/A *
4340N/A * This code is free software; you can redistribute it and/or modify it
4340N/A * under the terms of the GNU General Public License version 2 only, as
4340N/A * published by the Free Software Foundation.
4340N/A *
4340N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4340N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4340N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4340N/A * version 2 for more details (a copy is included in the LICENSE file that
4340N/A * accompanied this code).
4340N/A *
4340N/A * You should have received a copy of the GNU General Public License version
4340N/A * 2 along with this work; if not, write to the Free Software Foundation,
4340N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4340N/A *
4340N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4340N/A * or visit www.oracle.com if you need additional information or have any
4340N/A * questions.
4340N/A */
4340N/A
4340N/A/*
4340N/A * @test
4340N/A * @summary com.sun.tools.util.List.toArray violates Collection spec
4340N/A */
4340N/A
4340N/Aimport com.sun.tools.javac.util.List;
4340N/A
4340N/Apublic class T6238612 {
4340N/A public static void main(String... args) {
4340N/A new T6238612().test();
4340N/A }
4340N/A
4340N/A boolean error = false;
4340N/A
4340N/A // exercise the List.toArray method for a variety of lists
4340N/A void test() {
4340N/A test(List.<String>nil());
4340N/A test(List.of("a"));
4340N/A test(List.of("a", "b", "c"));
4340N/A test(List.of("a", "b", "c", "d", "e", "f"));
4340N/A if (error)
throw new Error("test failed");
}
// given a list, exercise the List.toArray method for a variety of arrays
void test(List<String> list) {
int n = list.size();
if (n > 0)
test(list, new String[0]);
if (n > 1)
test(list, new String[n - 1]);
test(list, new String[n]);
test(list, new String[n + 1]);
test(list, new String[n + 5]);
}
// test the List.toArray method for a particular list and array
void test(List<String> list, String[] array) {
String[] result = list.toArray(array);
if (result.length < list.size()) {
error("returned array is too small; expected: " + list.size() + ", found: " + result.length);
return;
}
if (list.size() <= array.length && result != array)
error("new array wrongly created");
int i = 0;
for (String s: list)
check(result, i++, s);
if (i < result.length)
check(result, i, null);
}
// check a specific element of an array
void check(String[] array, int i, String expected) {
if (!equal(array[i], expected))
error("element " + i + " incorrect; expected: " + str(expected) + ", found: " + str(array[i]));
}
// check if two strings are both null or are equal
boolean equal(String s1, String s2) {
return (s1 == null ? s2 == null : s1.equals(s2));
}
String str(String s) {
return (s == null ? "null" : '"' + s + '"');
}
void error(String message) {
System.err.println(message);
error = true;
}
}