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 17 package com.google.common.collect.testing.testers; 18 19 import static com.google.common.collect.testing.features.CollectionSize.ONE; 20 import static com.google.common.collect.testing.features.CollectionSize.ZERO; 21 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_REMOVE_WITH_INDEX; 22 23 import com.google.common.collect.testing.Helpers; 24 import com.google.common.collect.testing.features.CollectionSize; 25 import com.google.common.collect.testing.features.ListFeature; 26 27 import java.util.List; 28 29 /** 30 * A generic JUnit test which tests {@code remove(int)} operations on a list. 31 * Can't be invoked directly; please see 32 * {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 33 * 34 * <p>This class is GWT compatible. 35 * 36 * @author Chris Povirk 37 */ 38 public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> { 39 @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX) 40 @CollectionSize.Require(absent = ZERO) 41 public void testRemoveAtIndex_unsupported() { 42 try { 43 getList().remove(0); 44 fail("remove(i) should throw"); 45 } catch (UnsupportedOperationException expected) { 46 } 47 expectUnchanged(); 48 } 49 50 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 51 public void testRemoveAtIndex_negative() { 52 try { 53 getList().remove(-1); 54 fail("remove(-1) should throw"); 55 } catch (IndexOutOfBoundsException expected) { 56 } 57 expectUnchanged(); 58 } 59 60 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 61 public void testRemoveAtIndex_tooLarge() { 62 try { 63 getList().remove(getNumElements()); 64 fail("remove(size) should throw"); 65 } catch (IndexOutOfBoundsException expected) { 66 } 67 expectUnchanged(); 68 } 69 70 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 71 @CollectionSize.Require(absent = ZERO) 72 public void testRemoveAtIndex_first() { 73 runRemoveTest(0); 74 } 75 76 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 77 @CollectionSize.Require(absent = {ZERO, ONE}) 78 public void testRemoveAtIndex_middle() { 79 runRemoveTest(getNumElements() / 2); 80 } 81 82 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 83 @CollectionSize.Require(absent = ZERO) 84 public void testRemoveAtIndex_last() { 85 runRemoveTest(getNumElements() - 1); 86 } 87 88 private void runRemoveTest(int index) { 89 assertEquals(Platform.format( 90 "remove(%d) should return the element at index %d", index, index), 91 getList().get(index), getList().remove(index)); 92 List<E> expected = Helpers.copyToList(createSamplesArray()); 93 expected.remove(index); 94 expectContents(expected); 95 } 96 } 97