Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collection/BiggernYours.java
38812 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.io.*;31import java.util.*;32import java.util.concurrent.*;3334@SuppressWarnings("unchecked")35public class BiggernYours {36static final Random rnd = new Random(18675309);3738static void compareCollections(Collection c1, Collection c2) {39Object[] c1Array = c1.toArray();40Object[] c2Array = c2.toArray();4142check(c1Array.length == c2Array.length);43for(Object aC1 : c1Array) {44boolean found = false;45for(Object aC2 : c2Array) {46if(Objects.equals(aC1, aC2)) {47found = true;48break;49}50}5152if(!found)53fail(aC1 + " not found in " + Arrays.toString(c2Array));54}55}5657static void compareMaps(Map m1, Map m2) {58compareCollections(m1.keySet(),59m2.keySet());60compareCollections(m1.values(),61m2.values());62compareCollections(m1.entrySet(),63m2.entrySet());64}6566static void compareNavigableMaps(NavigableMap m1, NavigableMap m2) {67compareMaps(m1, m2);68compareMaps(m1.descendingMap(),69m2.descendingMap());70compareMaps(m1.tailMap(Integer.MIN_VALUE),71m2.tailMap(Integer.MIN_VALUE));72compareMaps(m1.headMap(Integer.MAX_VALUE),73m2.headMap(Integer.MAX_VALUE));74}7576static void compareNavigableSets(NavigableSet s1, NavigableSet s2) {77compareCollections(s1, s2);78compareCollections(s1.descendingSet(),79s2.descendingSet());80compareCollections(s1.tailSet(Integer.MIN_VALUE),81s2.tailSet(Integer.MIN_VALUE));82}8384static abstract class MapFrobber { abstract void frob(Map m); }85static abstract class SetFrobber { abstract void frob(Set s); }86static abstract class ColFrobber { abstract void frob(Collection c); }8788static ColFrobber adder(final int i) {89return new ColFrobber() {void frob(Collection c) { c.add(i); }};90}9192static final ColFrobber[] adders =93{ adder(1), adder(3), adder(2) };9495static MapFrobber putter(final int k, final int v) {96return new MapFrobber() {void frob(Map m) { m.put(k,v); }};97}9899static final MapFrobber[] putters =100{ putter(1, -2), putter(3, -6), putter(2, -4) };101102static void unexpected(Throwable t, Object suspect) {103System.out.println(suspect.getClass());104unexpected(t);105}106107static void testCollections(Collection c1, Collection c2) {108try {109compareCollections(c1, c2);110for (ColFrobber adder : adders) {111for (Collection c : new Collection[]{c1, c2})112adder.frob(c);113compareCollections(c1, c2);114}115} catch (Throwable t) { unexpected(t, c1); }116}117118static void testNavigableSets(NavigableSet s1, NavigableSet s2) {119try {120compareNavigableSets(s1, s2);121for (ColFrobber adder : adders) {122for (Set s : new Set[]{s1, s2})123adder.frob(s);124compareNavigableSets(s1, s2);125}126} catch (Throwable t) { unexpected(t, s1); }127}128129static void testMaps(Map m1, Map m2) {130try {131compareMaps(m1, m2);132for (MapFrobber putter : putters) {133for (Map m : new Map[]{m1, m2})134putter.frob(m);135compareMaps(m1, m2);136}137} catch (Throwable t) { unexpected(t, m1); }138}139140static void testNavigableMaps(NavigableMap m1, NavigableMap m2) {141try {142compareNavigableMaps(m1, m2);143for (MapFrobber putter : putters) {144for (Map m : new Map[]{m1, m2})145putter.frob(m);146compareNavigableMaps(m1, m2);147}148} catch (Throwable t) { unexpected(t, m1); }149}150151static int randomize(int size) { return rnd.nextInt(size + 2); }152153@SuppressWarnings("serial")154private static void realMain(String[] args) throws Throwable {155testNavigableMaps(156new ConcurrentSkipListMap(),157new ConcurrentSkipListMap() {158public int size() {return randomize(super.size());}});159160testNavigableSets(161new ConcurrentSkipListSet(),162new ConcurrentSkipListSet() {163public int size() {return randomize(super.size());}});164165testCollections(166new CopyOnWriteArraySet(),167new CopyOnWriteArraySet() {168public int size() {return randomize(super.size());}});169170testCollections(171new CopyOnWriteArrayList(),172new CopyOnWriteArrayList() {173public int size() {return randomize(super.size());}});174175testCollections(176new TreeSet(),177new TreeSet() {178public int size() {return randomize(super.size());}});179180testMaps(181new ConcurrentHashMap(),182new ConcurrentHashMap() {183public int size() {return randomize(super.size());}});184185testCollections(186new ConcurrentLinkedDeque(),187new ConcurrentLinkedDeque() {188public int size() {return randomize(super.size());}});189190testCollections(191new ConcurrentLinkedQueue(),192new ConcurrentLinkedQueue() {193public int size() {return randomize(super.size());}});194195testCollections(196new LinkedTransferQueue(),197new LinkedTransferQueue() {198public int size() {return randomize(super.size());}});199200testCollections(201new LinkedBlockingQueue(),202new LinkedBlockingQueue() {203public int size() {return randomize(super.size());}});204205testCollections(206new LinkedBlockingDeque(),207new LinkedBlockingDeque() {208public int size() {return randomize(super.size());}});209210testCollections(211new ArrayBlockingQueue(5),212new ArrayBlockingQueue(5) {213public int size() {return randomize(super.size());}});214215testCollections(216new PriorityBlockingQueue(5),217new PriorityBlockingQueue(5) {218public int size() {return randomize(super.size());}});219}220221//--------------------- Infrastructure ---------------------------222static volatile int passed = 0, failed = 0;223static void pass() {passed++;}224static void fail() {failed++; Thread.dumpStack();}225static void fail(String msg) {System.out.println(msg); fail();}226static void unexpected(Throwable t) {failed++; t.printStackTrace();}227static void check(boolean cond) {if (cond) pass(); else fail();}228static void equal(Object x, Object y) {229if (x == null ? y == null : x.equals(y)) pass();230else fail(x + " not equal to " + y);}231static void arrayEqual(Object[] x, Object[] y) {232if (x == null ? y == null : Arrays.equals(x, y)) pass();233else fail(Arrays.toString(x) + " not equal to " + Arrays.toString(y));}234public static void main(String[] args) throws Throwable {235try {realMain(args);} catch (Throwable t) {unexpected(t);}236System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);237if (failed > 0) throw new AssertionError("Some tests failed");}238private static abstract class CheckedThread extends Thread {239abstract void realRun() throws Throwable;240public void run() {241try {realRun();} catch (Throwable t) {unexpected(t);}}}242}243244245