Path: blob/master/test/jdk/java/util/EnumSet/LargeEnumIteratorRemoveResilience.java
41149 views
/*1* Copyright (c) 2011, 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* Portions Copyright (c) 2011 IBM Corporation25*/2627/*28* @test29* @bug 701463730* @summary EnumSet's iterator.remove() can be resilient to set's modification.31* @author Neil Richards <[email protected]>, <[email protected]>32*/3334import java.util.EnumSet;35import java.util.Iterator;36import java.util.Set;3738public class LargeEnumIteratorRemoveResilience {39// enum with more than 64 values40private static enum LargeEnum {41e00, e01, e02, e03, e04, e05, e06, e07,42e08, e09, e0A, e0B, e0C, e0D, e0E, e0F,43e10, e11, e12, e13, e14, e15, e16, e17,44e18, e19, e1A, e1B, e1C, e1D, e1E, e1F,45e20, e21, e22, e23, e24, e25, e26, e27,46e28, e29, e2A, e2B, e2C, e2D, e2E, e2F,47e30, e31, e32, e33, e34, e35, e36, e37,48e38, e39, e3A, e3B, e3C, e3D, e3E, e3F,49e40, e41, e42, e43, e44, e45, e46, e47,50e48, e49, e4A, e4B, e4C, e4D, e4E, e4F,51}5253public static void main(final String[] args) throws Exception {54final Set<LargeEnum> set = EnumSet.noneOf(LargeEnum.class);5556set.add(LargeEnum.e2D);57set.add(LargeEnum.e42);5859final Iterator<LargeEnum> iterator = set.iterator();6061int size = set.size();62LargeEnum element = iterator.next();6364iterator.remove();65checkSetAfterRemoval(set, size, element);6667size = set.size();68element = iterator.next();6970set.remove(element);71checkSetAfterRemoval(set, size, element);7273// The Java API declares that the behaviour here - to call74// iterator.remove() after the underlying collection has been75// modified - is "unspecified".76// However, in the case of iterators for EnumSet, it is easy to77// implement their remove() operation such that the set is78// unmodified if it is called for an element that has already been79// removed from the set - this being the naturally "resilient"80// behaviour.81iterator.remove();82checkSetAfterRemoval(set, size, element);83}8485private static void checkSetAfterRemoval(final Set<LargeEnum> set,86final int origSize, final LargeEnum removedElement)87throws Exception {88if (set.size() != (origSize - 1)) {89throw new Exception("Test FAILED: Unexpected set size after removal; expected '" + (origSize - 1) + "' but found '" + set.size() + "'");90}91if (set.contains(removedElement)) {92throw new Exception("Test FAILED: Element returned from iterator unexpectedly still in set after removal.");93}94}95}969798