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