ListRemoveAtIndexTester.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2008 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.common.collect.testing.testers; 18 19import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 20import static com.google.common.collect.testing.features.CollectionSize.ONE; 21import static com.google.common.collect.testing.features.CollectionSize.ZERO; 22import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_REMOVE_WITH_INDEX; 23 24import com.google.common.annotations.GwtCompatible; 25import com.google.common.collect.testing.Helpers; 26import com.google.common.collect.testing.features.CollectionFeature; 27import com.google.common.collect.testing.features.CollectionSize; 28import com.google.common.collect.testing.features.ListFeature; 29 30import java.util.ConcurrentModificationException; 31import java.util.Iterator; 32import java.util.List; 33 34/** 35 * A generic JUnit test which tests {@code remove(int)} operations on a list. 36 * Can't be invoked directly; please see 37 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 38 * 39 * <p>This class is GWT compatible. 40 * 41 * @author Chris Povirk 42 */ 43@GwtCompatible 44public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> { 45 @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX) 46 @CollectionSize.Require(absent = ZERO) 47 public void testRemoveAtIndex_unsupported() { 48 try { 49 getList().remove(0); 50 fail("remove(i) should throw"); 51 } catch (UnsupportedOperationException expected) { 52 } 53 expectUnchanged(); 54 } 55 56 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 57 public void testRemoveAtIndex_negative() { 58 try { 59 getList().remove(-1); 60 fail("remove(-1) should throw"); 61 } catch (IndexOutOfBoundsException expected) { 62 } 63 expectUnchanged(); 64 } 65 66 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 67 public void testRemoveAtIndex_tooLarge() { 68 try { 69 getList().remove(getNumElements()); 70 fail("remove(size) should throw"); 71 } catch (IndexOutOfBoundsException expected) { 72 } 73 expectUnchanged(); 74 } 75 76 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 77 @CollectionSize.Require(absent = ZERO) 78 public void testRemoveAtIndex_first() { 79 runRemoveTest(0); 80 } 81 82 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 83 @CollectionSize.Require(absent = {ZERO, ONE}) 84 public void testRemoveAtIndex_middle() { 85 runRemoveTest(getNumElements() / 2); 86 } 87 88 @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 89 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 90 @CollectionSize.Require(absent = ZERO) 91 public void testRemoveAtIndexConcurrentWithIteration() { 92 try { 93 Iterator<E> iterator = collection.iterator(); 94 getList().remove(getNumElements() / 2); 95 iterator.next(); 96 fail("Expected ConcurrentModificationException"); 97 } catch (ConcurrentModificationException expected) { 98 // success 99 } 100 } 101 102 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 103 @CollectionSize.Require(absent = ZERO) 104 public void testRemoveAtIndex_last() { 105 runRemoveTest(getNumElements() - 1); 106 } 107 108 private void runRemoveTest(int index) { 109 assertEquals(Platform.format( 110 "remove(%d) should return the element at index %d", index, index), 111 getList().get(index), getList().remove(index)); 112 List<E> expected = Helpers.copyToList(createSamplesArray()); 113 expected.remove(index); 114 expectContents(expected); 115 } 116} 117