001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect.testing.testers; 018 019import static com.google.common.collect.testing.Helpers.copyToList; 020import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION; 021import static com.google.common.collect.testing.features.CollectionSize.ONE; 022import static com.google.common.collect.testing.features.CollectionSize.ZERO; 023import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_REMOVE_WITH_INDEX; 024import static com.google.common.collect.testing.testers.ReflectionFreeAssertThrows.assertThrows; 025 026import com.google.common.annotations.GwtCompatible; 027import com.google.common.collect.testing.features.CollectionFeature; 028import com.google.common.collect.testing.features.CollectionSize; 029import com.google.common.collect.testing.features.ListFeature; 030import java.util.ConcurrentModificationException; 031import java.util.Iterator; 032import java.util.List; 033import org.junit.Ignore; 034 035/** 036 * A generic JUnit test which tests {@code remove(int)} operations on a list. Can't be invoked 037 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 038 * 039 * @author Chris Povirk 040 */ 041@GwtCompatible 042@Ignore("test runners must not instantiate and run this directly, only via suites we build") 043// @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. 044@SuppressWarnings("JUnit4ClassUsedInJUnit3") 045public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> { 046 @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX) 047 @CollectionSize.Require(absent = ZERO) 048 public void testRemoveAtIndex_unsupported() { 049 assertThrows(UnsupportedOperationException.class, () -> getList().remove(0)); 050 expectUnchanged(); 051 } 052 053 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 054 public void testRemoveAtIndex_negative() { 055 assertThrows(IndexOutOfBoundsException.class, () -> getList().remove(-1)); 056 expectUnchanged(); 057 } 058 059 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 060 public void testRemoveAtIndex_tooLarge() { 061 assertThrows(IndexOutOfBoundsException.class, () -> getList().remove(getNumElements())); 062 expectUnchanged(); 063 } 064 065 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 066 @CollectionSize.Require(absent = ZERO) 067 public void testRemoveAtIndex_first() { 068 runRemoveTest(0); 069 } 070 071 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 072 @CollectionSize.Require(absent = {ZERO, ONE}) 073 public void testRemoveAtIndex_middle() { 074 runRemoveTest(getNumElements() / 2); 075 } 076 077 @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 078 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 079 @CollectionSize.Require(absent = ZERO) 080 public void testRemoveAtIndexConcurrentWithIteration() { 081 assertThrows( 082 ConcurrentModificationException.class, 083 () -> { 084 Iterator<E> iterator = collection.iterator(); 085 getList().remove(getNumElements() / 2); 086 iterator.next(); 087 }); 088 } 089 090 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 091 @CollectionSize.Require(absent = ZERO) 092 public void testRemoveAtIndex_last() { 093 runRemoveTest(getNumElements() - 1); 094 } 095 096 private void runRemoveTest(int index) { 097 assertEquals( 098 Platform.format("remove(%d) should return the element at index %d", index, index), 099 getList().get(index), 100 getList().remove(index)); 101 List<E> expected = copyToList(createSamplesArray()); 102 expected.remove(index); 103 expectContents(expected); 104 } 105}