Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/NCopies.java
38812 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.*;31import java.util.concurrent.*;3233public class NCopies {34static volatile int passed = 0, failed = 0;3536static void fail(String msg) {37failed++;38new AssertionError(msg).printStackTrace();39}4041static void pass() {42passed++;43}4445static void unexpected(Throwable t) {46failed++;47t.printStackTrace();48}4950static void check(boolean condition, String msg) {51if (condition)52passed++;53else54fail(msg);55}5657static void check(boolean condition) {58check(condition, "Assertion failure");59}6061private static void checkEmpty(List<String> x) {62check(x.isEmpty());63check(x.size() == 0);64check(x.indexOf("foo") == -1);65check(x.lastIndexOf("foo") == -1);66check(x.toArray().length == 0);67check(x.toArray().getClass() == Object[].class);68}6970private static void checkFoos(List<String> x) {71check(! x.isEmpty());72check(x.indexOf(new String("foo")) == 0);73check(x.lastIndexOf(new String("foo")) == x.size()-1);74check(x.toArray().length == x.size());75check(x.toArray().getClass() == Object[].class);76String[] sa = x.toArray(new String[x.size()]);77check(sa.getClass() == String[].class);78check(sa[0].equals("foo"));79check(sa[sa.length-1].equals("foo"));80check(x.get(x.size()/2).equals("foo"));81checkEmpty(x.subList(x.size()/2, x.size()/2));82}8384private static <T> List<T> referenceNCopies(int n, T o) {85// A simplest correct implementation of nCopies to compare with the actual optimized implementation86return new AbstractList<T>() {87public int size() { return n; }8889public T get(int index) {90check(0 <= index && index < n, "Index is incorrect");91return o;92}93};94}9596private static void checkHashCode() {97int[] sizes = {0, 1, 2, 3, 5, 10, 31, 32, 100, 1000};98String[] elements = {null, "non-null"};99for (int size : sizes) {100for (String element : elements) {101int expectedHashCode = referenceNCopies(size, element).hashCode();102int actualHashCode = Collections.nCopies(size, element).hashCode();103check(expectedHashCode == actualHashCode,104"Collections.nCopies(" + size + ", " + element + ").hashCode()");105}106}107}108109private static void checkEquals() {110int[][] sizePairs = {{0, 0}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 1}};111String[] elements = {null, "non-null"};112for (int[] pair : sizePairs) {113for (String element : elements) {114boolean equal = pair[0] == pair[1];115String msg = "[" + pair[0] + ", " + element + "] <=> [" + pair[1] + ", " + element + "]";116check(equal == Collections.nCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);117check(equal == Collections.nCopies(pair[0], element).equals(referenceNCopies(pair[1], element)), msg);118check(equal == referenceNCopies(pair[0], element).equals(Collections.nCopies(pair[1], element)), msg);119}120}121List<String> nulls = Collections.nCopies(10, null);122List<String> nonNulls = Collections.nCopies(10, "non-null");123List<String> nullsButOne = new ArrayList<>(nulls);124nullsButOne.set(9, "non-null");125List<String> nonNullsButOne = new ArrayList<>(nonNulls);126nonNullsButOne.set(9, null);127check(!nulls.equals(nonNulls));128check(!nulls.equals(nullsButOne));129check(!nulls.equals(nonNullsButOne));130check(!nonNulls.equals(nonNullsButOne));131check(Collections.nCopies(0, null).equals(Collections.nCopies(0, "non-null")));132}133134public static void main(String[] args) {135try {136List<String> empty = Collections.nCopies(0, "foo");137checkEmpty(empty);138checkEmpty(empty.subList(0,0));139140List<String> foos = Collections.nCopies(42, "foo");141check(foos.size() == 42);142checkFoos(foos.subList(foos.size()/2, foos.size()-1));143144checkHashCode();145146checkEquals();147148} catch (Throwable t) { unexpected(t); }149150System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);151if (failed > 0) throw new Error("Some tests failed");152}153}154155156