Path: blob/master/test/jdk/java/util/Collections/Disjoint.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 433979226* @summary Basic test for Collections.disjoint27* @author Josh Bloch28* @key randomness29*/3031import java.util.ArrayList;32import java.util.Collections;33import java.util.HashSet;34import java.util.List;35import java.util.Random;3637public class Disjoint {38static final int N = 20;3940public static void main(String[] args) {41// Make an array of lists each of which shares a single element42// with its "neighbors," and no elements with other lists in the array43Random rnd = new Random();44List[] lists = new List[N];45int x = 0;46for (int i = 0; i < N; i++) {47int size = rnd.nextInt(10) + 2;48List<Integer> list = new ArrayList<>(size);49for (int j = 1; j < size; j++)50list.add(x++);51list.add(x);52Collections.shuffle(list);5354lists[i] = list;55}5657for (int i = 0; i < N; i++) {58for (int j = 0; j < N; j++) {59boolean disjoint = (Math.abs(i - j) > 1);60List<Integer> a = (List<Integer>) lists[i];61List<Integer> b = (List<Integer>) lists[j];6263if (Collections.disjoint(a, b) != disjoint)64throw new RuntimeException("A: " + i + ", " + j);65if (Collections.disjoint(new HashSet<Integer>(a), b)66!= disjoint)67throw new RuntimeException("B: " + i + ", " + j);68if (Collections.disjoint(new HashSet<Integer>(a),69new HashSet<Integer>(b)) != disjoint)70throw new RuntimeException("C: " + i + ", " + j);71}72}73}74}757677