Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/AddAll.java
38812 views
/*1* Copyright (c) 2003, 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 482288726* @summary Basic test for Collections.addAll27* @author Josh Bloch28* @key randomness29*/3031import java.util.*;3233public class AddAll {34final static int N = 100;35public static void main(String args[]) {36test(new ArrayList<Integer>());37test(new LinkedList<Integer>());38test(new HashSet<Integer>());39test(new LinkedHashSet<Integer>());40}4142private static Random rnd = new Random();4344static void test(Collection<Integer> c) {45int x = 0;46for (int i = 0; i < N; i++) {47int rangeLen = rnd.nextInt(10);48if (Collections.addAll(c, range(x, x + rangeLen)) !=49(rangeLen != 0))50throw new RuntimeException("" + rangeLen);51x += rangeLen;52}53if (c instanceof List) {54if (!c.equals(Arrays.asList(range(0, x))))55throw new RuntimeException(x +": "+c);56} else {57if (!c.equals(new HashSet<Integer>(Arrays.asList(range(0, x)))))58throw new RuntimeException(x +": "+c);59}60}6162private static Integer[] range(int from, int to) {63Integer[] result = new Integer[to - from];64for (int i = from, j=0; i < to; i++, j++)65result[j] = new Integer(i);66return result;67}68}697071