Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/Disjoint.java
38821 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 433979226* @summary Basic test for Collections.disjoint27* @author Josh Bloch28* @key randomness29*/3031import java.util.*;3233public class Disjoint {34final static int N = 20;3536public static void main(String args[]) {37// Make an array of lists each of which shares a single element38// with its "neighbors," and no elements with other lists in the array39Random rnd = new Random();40List[] lists = new List[N];41int x = 0;42for (int i = 0; i < N; i++) {43int size = rnd.nextInt(10) + 2;44List<Integer> list = new ArrayList<Integer>(size);45for (int j = 1; j < size; j++)46list.add(x++);47list.add(x);48Collections.shuffle(list);4950lists[i] = list;51}5253for (int i = 0; i < N; i++) {54for (int j = 0; j < N; j++) {55boolean disjoint = (Math.abs(i - j) > 1);56List<Integer> a = (List<Integer>) lists[i];57List<Integer> b = (List<Integer>) lists[j];5859if (Collections.disjoint(a, b) != disjoint)60throw new RuntimeException("A: " + i + ", " + j);61if (Collections.disjoint(new HashSet<Integer>(a), b)62!= disjoint)63throw new RuntimeException("B: " + i + ", " + j);64if (Collections.disjoint(new HashSet<Integer>(a),65new HashSet<Integer>(b)) != disjoint)66throw new RuntimeException("C: " + i + ", " + j);67}68}69}70}717273