< prev index next >

test/jdk/java/util/Collections/NCopies.java

Print this page

  1 /*
  2  * Copyright (c) 2005, 2025, 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.ArrayList;
 32 import java.util.Collections;
 33 import java.util.AbstractList;
 34 import java.util.List;
 35 import java.util.Objects;
 36 
 37 public class NCopies {
 38     static volatile int passed = 0, failed = 0;
 39 
 40     static void fail(String msg) {
 41         failed++;
 42         new AssertionError(msg).printStackTrace();
 43     }
 44 
 45     static void pass() {
 46         passed++;
 47     }
 48 
 49     static void unexpected(Throwable t) {
 50         failed++;

125         List<String> nulls = Collections.nCopies(10, null);
126         List<String> nonNulls = Collections.nCopies(10, "non-null");
127         List<String> nullsButOne = new ArrayList<>(nulls);
128         nullsButOne.set(9, "non-null");
129         List<String> nonNullsButOne = new ArrayList<>(nonNulls);
130         nonNullsButOne.set(9, null);
131         check(!nulls.equals(nonNulls));
132         check(!nulls.equals(nullsButOne));
133         check(!nulls.equals(nonNullsButOne));
134         check(!nonNulls.equals(nonNullsButOne));
135         check(Collections.nCopies(0, null).equals(Collections.nCopies(0, "non-null")));
136     }
137 
138     private static void checkReversed() {
139         List<String> copies = Collections.nCopies(10, "content");
140         check(copies.equals(copies.reversed()));
141         List<String> empty = Collections.nCopies(0, "content");
142         check(empty.equals(empty.reversed()));
143     }
144 









145     public static void main(String[] args) {
146         try {
147             List<String> empty = Collections.nCopies(0, "foo");
148             checkEmpty(empty);
149             checkEmpty(empty.subList(0,0));
150 
151             List<String> foos = Collections.nCopies(42, "foo");
152             check(foos.size() == 42);
153             checkFoos(foos.subList(foos.size()/2, foos.size()-1));
154 
155             checkHashCode();
156 
157             checkEquals();
158 
159             checkReversed();
160 


161         } catch (Throwable t) { unexpected(t); }
162 
163         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
164         if (failed > 0) throw new Error("Some tests failed");
165     }
166 }

  1 /*
  2  * Copyright (c) 2005, 2026, 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  * @library /test/lib
 30  */
 31 
 32 import jdk.test.lib.valueclass.VClass;
 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 
 42     static void fail(String msg) {
 43         failed++;
 44         new AssertionError(msg).printStackTrace();
 45     }
 46 
 47     static void pass() {
 48         passed++;
 49     }
 50 
 51     static void unexpected(Throwable t) {
 52         failed++;

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     private static void checkReversed() {
141         List<String> copies = Collections.nCopies(10, "content");
142         check(copies.equals(copies.reversed()));
143         List<String> empty = Collections.nCopies(0, "content");
144         check(empty.equals(empty.reversed()));
145     }
146 
147     private static void checkValueCopies() {
148         List<VClass> copies = Collections.nCopies(10, new VClass(7, new int[] { 7 }));
149         check(copies.indexOf(new VClass(7, new int[] { 7 })) == 0);
150         check(copies.lastIndexOf(new VClass(7, new int[] { 7 })) == 9);
151         check(copies.equals(referenceNCopies(10, new VClass(7, new int[] { 7 }))));
152         check(copies.hashCode() == referenceNCopies(10, new VClass(7, new int[] { 7 })).hashCode());
153         check(copies.equals(copies.reversed()));
154     }
155 
156     public static void main(String[] args) {
157         try {
158             List<String> empty = Collections.nCopies(0, "foo");
159             checkEmpty(empty);
160             checkEmpty(empty.subList(0,0));
161 
162             List<String> foos = Collections.nCopies(42, "foo");
163             check(foos.size() == 42);
164             checkFoos(foos.subList(foos.size()/2, foos.size()-1));
165 
166             checkHashCode();
167 
168             checkEquals();
169 
170             checkReversed();
171 
172             checkValueCopies();
173 
174         } catch (Throwable t) { unexpected(t); }
175 
176         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
177         if (failed > 0) throw new Error("Some tests failed");
178     }
179 }
< prev index next >