author | jjg |
Wed, 10 Aug 2016 15:47:46 -0700 | |
changeset 40308 | 274367a99f98 |
parent 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary com.sun.tools.util.List.toArray violates Collection spec |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
5520
diff
changeset
|
27 |
* @modules jdk.compiler/com.sun.tools.javac.util |
10 | 28 |
*/ |
29 |
||
30 |
import com.sun.tools.javac.util.List; |
|
31 |
||
32 |
public class T6238612 { |
|
33 |
public static void main(String... args) { |
|
34 |
new T6238612().test(); |
|
35 |
} |
|
36 |
||
37 |
boolean error = false; |
|
38 |
||
39 |
// exercise the List.toArray method for a variety of lists |
|
40 |
void test() { |
|
41 |
test(List.<String>nil()); |
|
42 |
test(List.of("a")); |
|
43 |
test(List.of("a", "b", "c")); |
|
44 |
test(List.of("a", "b", "c", "d", "e", "f")); |
|
45 |
if (error) |
|
46 |
throw new Error("test failed"); |
|
47 |
} |
|
48 |
||
49 |
// given a list, exercise the List.toArray method for a variety of arrays |
|
50 |
void test(List<String> list) { |
|
51 |
int n = list.size(); |
|
52 |
if (n > 0) |
|
53 |
test(list, new String[0]); |
|
54 |
||
55 |
if (n > 1) |
|
56 |
test(list, new String[n - 1]); |
|
57 |
||
58 |
test(list, new String[n]); |
|
59 |
test(list, new String[n + 1]); |
|
60 |
test(list, new String[n + 5]); |
|
61 |
} |
|
62 |
||
63 |
// test the List.toArray method for a particular list and array |
|
64 |
void test(List<String> list, String[] array) { |
|
65 |
String[] result = list.toArray(array); |
|
66 |
||
67 |
if (result.length < list.size()) { |
|
68 |
error("returned array is too small; expected: " + list.size() + ", found: " + result.length); |
|
69 |
return; |
|
70 |
} |
|
71 |
||
72 |
if (list.size() <= array.length && result != array) |
|
73 |
error("new array wrongly created"); |
|
74 |
||
75 |
int i = 0; |
|
76 |
for (String s: list) |
|
77 |
check(result, i++, s); |
|
78 |
||
79 |
if (i < result.length) |
|
80 |
check(result, i, null); |
|
81 |
} |
|
82 |
||
83 |
// check a specific element of an array |
|
84 |
void check(String[] array, int i, String expected) { |
|
85 |
if (!equal(array[i], expected)) |
|
86 |
error("element " + i + " incorrect; expected: " + str(expected) + ", found: " + str(array[i])); |
|
87 |
} |
|
88 |
||
89 |
// check if two strings are both null or are equal |
|
90 |
boolean equal(String s1, String s2) { |
|
91 |
return (s1 == null ? s2 == null : s1.equals(s2)); |
|
92 |
} |
|
93 |
||
94 |
String str(String s) { |
|
95 |
return (s == null ? "null" : '"' + s + '"'); |
|
96 |
} |
|
97 |
||
98 |
void error(String message) { |
|
99 |
System.err.println(message); |
|
100 |
error = true; |
|
101 |
} |
|
102 |
} |