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 package test.java.util.Collections;
25 
26 /*
27  * @test
28  * @bug     6267846 6275009
29  * @summary Test Collections.nCopies
30  * @author  Martin Buchholz
31  */
32 
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.AbstractList;
36 import java.util.List;
37 import java.util.Objects;
38 
39 public class NCopies {
40     static volatile int passed = 0, failed = 0;
41 
fail(String msg)42     static void fail(String msg) {
43         failed++;
44         new AssertionError(msg).printStackTrace();
45     }
46 
pass()47     static void pass() {
48         passed++;
49     }
50 
unexpected(Throwable t)51     static void unexpected(Throwable t) {
52         failed++;
53         t.printStackTrace();
54     }
55 
check(boolean condition, String msg)56     static void check(boolean condition, String msg) {
57         if (condition)
58             passed++;
59         else
60             fail(msg);
61     }
62 
check(boolean condition)63     static void check(boolean condition) {
64         check(condition, "Assertion failure");
65     }
66 
checkEmpty(List<String> x)67     private static void checkEmpty(List<String> x) {
68             check(x.isEmpty());
69             check(x.size() == 0);
70             check(x.indexOf("foo") == -1);
71             check(x.lastIndexOf("foo") == -1);
72             check(x.toArray().length == 0);
73             check(x.toArray().getClass() == Object[].class);
74     }
75 
checkFoos(List<String> x)76     private static void checkFoos(List<String> x) {
77             check(! x.isEmpty());
78             check(x.indexOf(new String("foo")) == 0);
79             check(x.lastIndexOf(new String("foo")) == x.size()-1);
80             check(x.toArray().length == x.size());
81             check(x.toArray().getClass() == Object[].class);
82             String[] sa = x.toArray(new String[x.size()]);
83             check(sa.getClass() == String[].class);
84             check(sa[0].equals("foo"));
85             check(sa[sa.length-1].equals("foo"));
86             check(x.get(x.size()/2).equals("foo"));
87             checkEmpty(x.subList(x.size()/2, x.size()/2));
88     }
89 
referenceNCopies(int n, T o)90     private static <T> List<T> referenceNCopies(int n, T o) {
91         // A simplest correct implementation of nCopies to compare with the actual optimized implementation
92         return new AbstractList<>() {
93             public int size() { return n; }
94 
95             public T get(int index) {
96                 Objects.checkIndex(index, n);
97                 return o;
98             }
99         };
100     }
101 
102     private static void checkHashCode() {
103         int[] sizes = {0, 1, 2, 3, 5, 10, 31, 32, 100, 1000};
104         String[] elements = {null, "non-null"};
105         for (int size : sizes) {
106             for (String element : elements) {
107                 int expectedHashCode = referenceNCopies(size, element).hashCode();
108                 int actualHashCode = Collections.nCopies(size, element).hashCode();
109                 check(expectedHashCode == actualHashCode,
110                         "Collections.nCopies(" + size + ", " + element + ").hashCode()");
111             }
112         }
113     }
114 
115     private static void checkEquals() {
116         int[][] sizePairs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 1}};
117         String[] elements = {null, "non-null"};
118         for (int[] pair : sizePairs) {
119             for (String element : elements) {
120                 boolean equal = pair[0] == pair[1];
121                 String msg = "[" + pair[0] + ", " + element + "] <=> [" + pair[1] + ", " + element + "]";
122                 check(equal == Collections.nCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);
123                 check(equal == Collections.nCopies(pair[0], element).equals(referenceNCopies(pair[1], element)), msg);
124                 check(equal == referenceNCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);
125             }
126         }
127         List<String> nulls = Collections.nCopies(10, null);
128         List<String> nonNulls = Collections.nCopies(10, "non-null");
129         List<String> nullsButOne = new ArrayList<>(nulls);
130         nullsButOne.set(9, "non-null");
131         List<String> nonNullsButOne = new ArrayList<>(nonNulls);
132         nonNullsButOne.set(9, null);
133         check(!nulls.equals(nonNulls));
134         check(!nulls.equals(nullsButOne));
135         check(!nulls.equals(nonNullsButOne));
136         check(!nonNulls.equals(nonNullsButOne));
137         check(Collections.nCopies(0, null).equals(Collections.nCopies(0, "non-null")));
138     }
139 
140     public static void main(String[] args) {
141         try {
142             List<String> empty = Collections.nCopies(0, "foo");
143             checkEmpty(empty);
144             checkEmpty(empty.subList(0,0));
145 
146             List<String> foos = Collections.nCopies(42, "foo");
147             check(foos.size() == 42);
148             checkFoos(foos.subList(foos.size()/2, foos.size()-1));
149 
150             checkHashCode();
151 
152             checkEquals();
153 
154         } catch (Throwable t) { unexpected(t); }
155 
156         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
157         if (failed > 0) throw new Error("Some tests failed");
158     }
159 }
160