Path: blob/master/test/jdk/java/util/Collections/NCopies.java
41149 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6267846 627500926* @summary Test Collections.nCopies27* @author Martin Buchholz28*/2930import java.util.ArrayList;31import java.util.Collections;32import java.util.AbstractList;33import java.util.List;34import java.util.Objects;3536public class NCopies {37static volatile int passed = 0, failed = 0;3839static void fail(String msg) {40failed++;41new AssertionError(msg).printStackTrace();42}4344static void pass() {45passed++;46}4748static void unexpected(Throwable t) {49failed++;50t.printStackTrace();51}5253static void check(boolean condition, String msg) {54if (condition)55passed++;56else57fail(msg);58}5960static void check(boolean condition) {61check(condition, "Assertion failure");62}6364private static void checkEmpty(List<String> x) {65check(x.isEmpty());66check(x.size() == 0);67check(x.indexOf("foo") == -1);68check(x.lastIndexOf("foo") == -1);69check(x.toArray().length == 0);70check(x.toArray().getClass() == Object[].class);71}7273private static void checkFoos(List<String> x) {74check(! x.isEmpty());75check(x.indexOf(new String("foo")) == 0);76check(x.lastIndexOf(new String("foo")) == x.size()-1);77check(x.toArray().length == x.size());78check(x.toArray().getClass() == Object[].class);79String[] sa = x.toArray(new String[x.size()]);80check(sa.getClass() == String[].class);81check(sa[0].equals("foo"));82check(sa[sa.length-1].equals("foo"));83check(x.get(x.size()/2).equals("foo"));84checkEmpty(x.subList(x.size()/2, x.size()/2));85}8687private static <T> List<T> referenceNCopies(int n, T o) {88// A simplest correct implementation of nCopies to compare with the actual optimized implementation89return new AbstractList<>() {90public int size() { return n; }9192public T get(int index) {93Objects.checkIndex(index, n);94return o;95}96};97}9899private static void checkHashCode() {100int[] sizes = {0, 1, 2, 3, 5, 10, 31, 32, 100, 1000};101String[] elements = {null, "non-null"};102for (int size : sizes) {103for (String element : elements) {104int expectedHashCode = referenceNCopies(size, element).hashCode();105int actualHashCode = Collections.nCopies(size, element).hashCode();106check(expectedHashCode == actualHashCode,107"Collections.nCopies(" + size + ", " + element + ").hashCode()");108}109}110}111112private static void checkEquals() {113int[][] sizePairs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 1}};114String[] elements = {null, "non-null"};115for (int[] pair : sizePairs) {116for (String element : elements) {117boolean equal = pair[0] == pair[1];118String msg = "[" + pair[0] + ", " + element + "] <=> [" + pair[1] + ", " + element + "]";119check(equal == Collections.nCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);120check(equal == Collections.nCopies(pair[0], element).equals(referenceNCopies(pair[1], element)), msg);121check(equal == referenceNCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);122}123}124List<String> nulls = Collections.nCopies(10, null);125List<String> nonNulls = Collections.nCopies(10, "non-null");126List<String> nullsButOne = new ArrayList<>(nulls);127nullsButOne.set(9, "non-null");128List<String> nonNullsButOne = new ArrayList<>(nonNulls);129nonNullsButOne.set(9, null);130check(!nulls.equals(nonNulls));131check(!nulls.equals(nullsButOne));132check(!nulls.equals(nonNullsButOne));133check(!nonNulls.equals(nonNullsButOne));134check(Collections.nCopies(0, null).equals(Collections.nCopies(0, "non-null")));135}136137public static void main(String[] args) {138try {139List<String> empty = Collections.nCopies(0, "foo");140checkEmpty(empty);141checkEmpty(empty.subList(0,0));142143List<String> foos = Collections.nCopies(42, "foo");144check(foos.size() == 42);145checkFoos(foos.subList(foos.size()/2, foos.size()-1));146147checkHashCode();148149checkEquals();150151} catch (Throwable t) { unexpected(t); }152153System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);154if (failed > 0) throw new Error("Some tests failed");155}156}157158159