Path: blob/master/test/jdk/java/util/Collection/BiggernYours.java
41149 views
/*1* Copyright (c) 2006, 2014, 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 6415641 637730226* @summary Concurrent collections are permitted to lie about their size27* @author Martin Buchholz28*/2930import java.util.Arrays;31import java.util.Collection;32import java.util.Map;33import java.util.NavigableMap;34import java.util.NavigableSet;35import java.util.Objects;36import java.util.Random;37import java.util.Set;38import java.util.TreeSet;39import java.util.concurrent.ArrayBlockingQueue;40import java.util.concurrent.ConcurrentHashMap;41import java.util.concurrent.ConcurrentLinkedDeque;42import java.util.concurrent.ConcurrentLinkedQueue;43import java.util.concurrent.ConcurrentSkipListMap;44import java.util.concurrent.ConcurrentSkipListSet;45import java.util.concurrent.CopyOnWriteArrayList;46import java.util.concurrent.CopyOnWriteArraySet;47import java.util.concurrent.LinkedBlockingDeque;48import java.util.concurrent.LinkedBlockingQueue;49import java.util.concurrent.LinkedTransferQueue;50import java.util.concurrent.PriorityBlockingQueue;5152@SuppressWarnings("unchecked")53public class BiggernYours {54static final Random rnd = new Random(18675309);5556static void compareCollections(Collection c1, Collection c2) {57Object[] c1Array = c1.toArray();58Object[] c2Array = c2.toArray();5960check(c1Array.length == c2Array.length);61for (Object aC1 : c1Array) {62boolean found = false;63for (Object aC2 : c2Array) {64if (Objects.equals(aC1, aC2)) {65found = true;66break;67}68}6970if (!found)71fail(aC1 + " not found in " + Arrays.toString(c2Array));72}73}7475static void compareMaps(Map m1, Map m2) {76compareCollections(m1.keySet(),77m2.keySet());78compareCollections(m1.values(),79m2.values());80compareCollections(m1.entrySet(),81m2.entrySet());82}8384static void compareNavigableMaps(NavigableMap m1, NavigableMap m2) {85compareMaps(m1, m2);86compareMaps(m1.descendingMap(),87m2.descendingMap());88compareMaps(m1.tailMap(Integer.MIN_VALUE),89m2.tailMap(Integer.MIN_VALUE));90compareMaps(m1.headMap(Integer.MAX_VALUE),91m2.headMap(Integer.MAX_VALUE));92}9394static void compareNavigableSets(NavigableSet s1, NavigableSet s2) {95compareCollections(s1, s2);96compareCollections(s1.descendingSet(),97s2.descendingSet());98compareCollections(s1.tailSet(Integer.MIN_VALUE),99s2.tailSet(Integer.MIN_VALUE));100}101102abstract static class MapFrobber { abstract void frob(Map m); }103abstract static class SetFrobber { abstract void frob(Set s); }104abstract static class ColFrobber { abstract void frob(Collection c); }105106static ColFrobber adder(final int i) {107return new ColFrobber() {void frob(Collection c) { c.add(i); }};108}109110static final ColFrobber[] adders =111{ adder(1), adder(3), adder(2) };112113static MapFrobber putter(final int k, final int v) {114return new MapFrobber() {void frob(Map m) { m.put(k,v); }};115}116117static final MapFrobber[] putters =118{ putter(1, -2), putter(3, -6), putter(2, -4) };119120static void unexpected(Throwable t, Object suspect) {121System.out.println(suspect.getClass());122unexpected(t);123}124125static void testCollections(Collection c1, Collection c2) {126try {127compareCollections(c1, c2);128for (ColFrobber adder : adders) {129for (Collection c : new Collection[]{c1, c2})130adder.frob(c);131compareCollections(c1, c2);132}133} catch (Throwable t) { unexpected(t, c1); }134}135136static void testNavigableSets(NavigableSet s1, NavigableSet s2) {137try {138compareNavigableSets(s1, s2);139for (ColFrobber adder : adders) {140for (Set s : new Set[]{s1, s2})141adder.frob(s);142compareNavigableSets(s1, s2);143}144} catch (Throwable t) { unexpected(t, s1); }145}146147static void testMaps(Map m1, Map m2) {148try {149compareMaps(m1, m2);150for (MapFrobber putter : putters) {151for (Map m : new Map[]{m1, m2})152putter.frob(m);153compareMaps(m1, m2);154}155} catch (Throwable t) { unexpected(t, m1); }156}157158static void testNavigableMaps(NavigableMap m1, NavigableMap m2) {159try {160compareNavigableMaps(m1, m2);161for (MapFrobber putter : putters) {162for (Map m : new Map[]{m1, m2})163putter.frob(m);164compareNavigableMaps(m1, m2);165}166} catch (Throwable t) { unexpected(t, m1); }167}168169static int randomize(int size) { return rnd.nextInt(size + 2); }170171@SuppressWarnings("serial")172private static void realMain(String[] args) {173testNavigableMaps(174new ConcurrentSkipListMap(),175new ConcurrentSkipListMap() {176public int size() {return randomize(super.size());}});177178testNavigableSets(179new ConcurrentSkipListSet(),180new ConcurrentSkipListSet() {181public int size() {return randomize(super.size());}});182183testCollections(184new CopyOnWriteArraySet(),185new CopyOnWriteArraySet() {186public int size() {return randomize(super.size());}});187188testCollections(189new CopyOnWriteArrayList(),190new CopyOnWriteArrayList() {191public int size() {return randomize(super.size());}});192193testCollections(194new TreeSet(),195new TreeSet() {196public int size() {return randomize(super.size());}});197198testMaps(199new ConcurrentHashMap(),200new ConcurrentHashMap() {201public int size() {return randomize(super.size());}});202203testCollections(204new ConcurrentLinkedDeque(),205new ConcurrentLinkedDeque() {206public int size() {return randomize(super.size());}});207208testCollections(209new ConcurrentLinkedQueue(),210new ConcurrentLinkedQueue() {211public int size() {return randomize(super.size());}});212213testCollections(214new LinkedTransferQueue(),215new LinkedTransferQueue() {216public int size() {return randomize(super.size());}});217218testCollections(219new LinkedBlockingQueue(),220new LinkedBlockingQueue() {221public int size() {return randomize(super.size());}});222223testCollections(224new LinkedBlockingDeque(),225new LinkedBlockingDeque() {226public int size() {return randomize(super.size());}});227228testCollections(229new ArrayBlockingQueue(5),230new ArrayBlockingQueue(5) {231public int size() {return randomize(super.size());}});232233testCollections(234new PriorityBlockingQueue(5),235new PriorityBlockingQueue(5) {236public int size() {return randomize(super.size());}});237}238239//--------------------- Infrastructure ---------------------------240static volatile int passed = 0, failed = 0;241static void pass() {passed++;}242static void fail() {failed++; Thread.dumpStack();}243static void fail(String msg) {System.out.println(msg); fail();}244static void unexpected(Throwable t) {failed++; t.printStackTrace();}245static void check(boolean cond) {if (cond) pass(); else fail();}246static void equal(Object x, Object y) {247if (x == null ? y == null : x.equals(y)) pass();248else fail(x + " not equal to " + y);}249static void arrayEqual(Object[] x, Object[] y) {250if (x == null ? y == null : Arrays.equals(x, y)) pass();251else fail(Arrays.toString(x) + " not equal to " + Arrays.toString(y));}252public static void main(String[] args) {253try {realMain(args);} catch (Throwable t) {unexpected(t);}254System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);255if (failed > 0) throw new AssertionError("Some tests failed");}256private abstract static class CheckedThread extends Thread {257abstract void realRun() throws Throwable;258public void run() {259try {realRun();} catch (Throwable t) {unexpected(t);}}}260}261262263