Path: blob/master/test/jdk/java/util/EnumSet/SmallEnumIteratorRemoveResilience.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 SmallEnumIteratorRemoveResilience {39// enum with less than 64 values40private static enum SmallEnum { e0, e1, e2 }4142public static void main(final String[] args) throws Exception {43final Set<SmallEnum> set = EnumSet.noneOf(SmallEnum.class);4445set.add(SmallEnum.e0);46set.add(SmallEnum.e1);4748final Iterator<SmallEnum> iterator = set.iterator();4950int size = set.size();51SmallEnum element = iterator.next();5253iterator.remove();54checkSetAfterRemoval(set, size, element);5556size = set.size();57element = iterator.next();5859set.remove(element);60checkSetAfterRemoval(set, size, element);6162// The Java API declares that the behaviour here - to call63// iterator.remove() after the underlying collection has been64// modified - is "unspecified".65// However, in the case of iterators for EnumSet, it is easy to66// implement their remove() operation such that the set is67// unmodified if it is called for an element that has already been68// removed from the set - this being the naturally "resilient"69// behaviour.70iterator.remove();71checkSetAfterRemoval(set, size, element);72}7374private static void checkSetAfterRemoval(final Set<SmallEnum> set,75final int origSize, final SmallEnum removedElement)76throws Exception {77if (set.size() != (origSize - 1)) {78throw new Exception("Test FAILED: Unexpected set size after removal; expected '" + (origSize - 1) + "' but found '" + set.size() + "'");79}80if (set.contains(removedElement)) {81throw new Exception("Test FAILED: Element returned from iterator unexpectedly still in set after removal.");82}83}84}858687