Path: blob/master/test/jdk/java/util/Collections/AddAll.java
41149 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.ArrayList;32import java.util.Arrays;33import java.util.Collection;34import java.util.Collections;35import java.util.HashSet;36import java.util.LinkedHashSet;37import java.util.LinkedList;38import java.util.List;39import java.util.Random;4041public class AddAll {42static final int N = 100;43public static void main(String[] args) {44test(new ArrayList<Integer>());45test(new LinkedList<Integer>());46test(new HashSet<Integer>());47test(new LinkedHashSet<Integer>());48}4950private static Random rnd = new Random();5152static void test(Collection<Integer> c) {53int x = 0;54for (int i = 0; i < N; i++) {55int rangeLen = rnd.nextInt(10);56if (Collections.addAll(c, range(x, x + rangeLen)) !=57(rangeLen != 0))58throw new RuntimeException("" + rangeLen);59x += rangeLen;60}61if (c instanceof List) {62if (!c.equals(Arrays.asList(range(0, x))))63throw new RuntimeException(x +": "+c);64} else {65if (!c.equals(new HashSet<Integer>(Arrays.asList(range(0, x)))))66throw new RuntimeException(x +": "+c);67}68}6970private static Integer[] range(int from, int to) {71Integer[] result = new Integer[to - from];72for (int i = from, j=0; i < to; i++, j++)73result[j] = new Integer(i);74return result;75}76}777879