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 * @bug 6267846 6275009
27 * @summary Test Collections.nCopies
28 * @author Martin Buchholz
29 */
30
31 import java.util.Collections;
32 import java.util.List;
33
34 public class NCopies {
35 static volatile int passed = 0, failed = 0;
36
37 static void fail(String msg) {
38 failed++;
39 new AssertionError(msg).printStackTrace();
40 }
41
42 static void pass() {
43 passed++;
44 }
45
46 static void unexpected(Throwable t) {
47 failed++;
48 t.printStackTrace();
49 }
50
51 static void check(boolean condition, String msg) {
52 if (condition)
65 check(x.indexOf("foo") == -1);
66 check(x.lastIndexOf("foo") == -1);
67 check(x.toArray().length == 0);
68 check(x.toArray().getClass() == Object[].class);
69 }
70
71 private static void checkFoos(List<String> x) {
72 check(! x.isEmpty());
73 check(x.indexOf(new String("foo")) == 0);
74 check(x.lastIndexOf(new String("foo")) == x.size()-1);
75 check(x.toArray().length == x.size());
76 check(x.toArray().getClass() == Object[].class);
77 String[] sa = x.toArray(new String[x.size()]);
78 check(sa.getClass() == String[].class);
79 check(sa[0].equals("foo"));
80 check(sa[sa.length-1].equals("foo"));
81 check(x.get(x.size()/2).equals("foo"));
82 checkEmpty(x.subList(x.size()/2, x.size()/2));
83 }
84
85 public static void main(String[] args) {
86 try {
87 List<String> empty = Collections.nCopies(0, "foo");
88 checkEmpty(empty);
89 checkEmpty(empty.subList(0,0));
90
91 List<String> foos = Collections.nCopies(42, "foo");
92 check(foos.size() == 42);
93 checkFoos(foos.subList(foos.size()/2, foos.size()-1));
94
95 } catch (Throwable t) { unexpected(t); }
96
97 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
98 if (failed > 0) throw new Error("Some tests failed");
99 }
100 }
|
1 /*
2 * Copyright (c) 2005, 2018, 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 * @bug 6267846 6275009
27 * @summary Test Collections.nCopies
28 * @author Martin Buchholz
29 */
30
31 import java.util.Collections;
32 import java.util.AbstractList;
33 import java.util.List;
34 import java.util.Objects;
35
36 public class NCopies {
37 static volatile int passed = 0, failed = 0;
38
39 static void fail(String msg) {
40 failed++;
41 new AssertionError(msg).printStackTrace();
42 }
43
44 static void pass() {
45 passed++;
46 }
47
48 static void unexpected(Throwable t) {
49 failed++;
50 t.printStackTrace();
51 }
52
53 static void check(boolean condition, String msg) {
54 if (condition)
67 check(x.indexOf("foo") == -1);
68 check(x.lastIndexOf("foo") == -1);
69 check(x.toArray().length == 0);
70 check(x.toArray().getClass() == Object[].class);
71 }
72
73 private static void checkFoos(List<String> x) {
74 check(! x.isEmpty());
75 check(x.indexOf(new String("foo")) == 0);
76 check(x.lastIndexOf(new String("foo")) == x.size()-1);
77 check(x.toArray().length == x.size());
78 check(x.toArray().getClass() == Object[].class);
79 String[] sa = x.toArray(new String[x.size()]);
80 check(sa.getClass() == String[].class);
81 check(sa[0].equals("foo"));
82 check(sa[sa.length-1].equals("foo"));
83 check(x.get(x.size()/2).equals("foo"));
84 checkEmpty(x.subList(x.size()/2, x.size()/2));
85 }
86
87 private static <T> List<T> referenceNCopies(int n, T o) {
88 // A simplest correct implementation of nCopies to compare with the actual optimized implementation
89 return new AbstractList<>() {
90 public int size() { return n; }
91
92 public T get(int index) {
93 Objects.checkIndex(index, n);
94 return o;
95 }
96 };
97 }
98
99 private static void checkHashCode() {
100 int[] sizes = {0, 1, 2, 3, 5, 10, 31, 32, 100, 1000};
101 String[] elements = {null, "non-null"};
102 for (int size : sizes) {
103 for (String element : elements) {
104 int expectedHashCode = referenceNCopies(size, element).hashCode();
105 int actualHashCode = Collections.nCopies(size, element).hashCode();
106 check(expectedHashCode == actualHashCode,
107 "Collections.nCopies(" + size + ", " + element + ").hashCode()");
108 }
109 }
110 }
111
112 public static void main(String[] args) {
113 try {
114 List<String> empty = Collections.nCopies(0, "foo");
115 checkEmpty(empty);
116 checkEmpty(empty.subList(0,0));
117
118 List<String> foos = Collections.nCopies(42, "foo");
119 check(foos.size() == 42);
120 checkFoos(foos.subList(foos.size()/2, foos.size()-1));
121
122 checkHashCode();
123
124 } catch (Throwable t) { unexpected(t); }
125
126 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
127 if (failed > 0) throw new Error("Some tests failed");
128 }
129 }
|