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 org.junit.Assert.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; 033 034/** 035 * A generic JUnit test which tests {@code remove(int)} operations on a list. Can't be invoked 036 * directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. 037 * 038 * @author Chris Povirk 039 */ 040@GwtCompatible 041public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> { 042 @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX) 043 @CollectionSize.Require(absent = ZERO) 044 public void testRemoveAtIndex_unsupported() { 045 assertThrows(UnsupportedOperationException.class, () -> getList().remove(0)); 046 expectUnchanged(); 047 } 048 049 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 050 public void testRemoveAtIndex_negative() { 051 assertThrows(IndexOutOfBoundsException.class, () -> getList().remove(-1)); 052 expectUnchanged(); 053 } 054 055 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 056 public void testRemoveAtIndex_tooLarge() { 057 assertThrows(IndexOutOfBoundsException.class, () -> getList().remove(getNumElements())); 058 expectUnchanged(); 059 } 060 061 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 062 @CollectionSize.Require(absent = ZERO) 063 public void testRemoveAtIndex_first() { 064 runRemoveTest(0); 065 } 066 067 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 068 @CollectionSize.Require(absent = {ZERO, ONE}) 069 public void testRemoveAtIndex_middle() { 070 runRemoveTest(getNumElements() / 2); 071 } 072 073 @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION) 074 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 075 @CollectionSize.Require(absent = ZERO) 076 public void testRemoveAtIndexConcurrentWithIteration() { 077 Iterator<E> iterator = collection.iterator(); 078 getList().remove(getNumElements() / 2); 079 assertThrows(ConcurrentModificationException.class, iterator::next); 080 } 081 082 @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) 083 @CollectionSize.Require(absent = ZERO) 084 public void testRemoveAtIndex_last() { 085 runRemoveTest(getNumElements() - 1); 086 } 087 088 private void runRemoveTest(int index) { 089 assertEquals( 090 Platform.format("remove(%d) should return the element at index %d", index, index), 091 getList().get(index), 092 getList().remove(index)); 093 List<E> expected = copyToList(createSamplesArray()); 094 expected.remove(index); 095 expectContents(expected); 096 } 097}