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.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
020import static com.google.common.collect.testing.features.CollectionSize.ONE;
021import static com.google.common.collect.testing.features.CollectionSize.ZERO;
022import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_REMOVE_WITH_INDEX;
023
024import com.google.common.annotations.GwtCompatible;
025import com.google.common.collect.testing.Helpers;
026import com.google.common.collect.testing.features.CollectionFeature;
027import com.google.common.collect.testing.features.CollectionSize;
028import com.google.common.collect.testing.features.ListFeature;
029import java.util.ConcurrentModificationException;
030import java.util.Iterator;
031import java.util.List;
032import org.junit.Ignore;
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
041@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests.
042public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> {
043  @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX)
044  @CollectionSize.Require(absent = ZERO)
045  public void testRemoveAtIndex_unsupported() {
046    try {
047      getList().remove(0);
048      fail("remove(i) should throw");
049    } catch (UnsupportedOperationException expected) {
050    }
051    expectUnchanged();
052  }
053
054  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
055  public void testRemoveAtIndex_negative() {
056    try {
057      getList().remove(-1);
058      fail("remove(-1) should throw");
059    } catch (IndexOutOfBoundsException expected) {
060    }
061    expectUnchanged();
062  }
063
064  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
065  public void testRemoveAtIndex_tooLarge() {
066    try {
067      getList().remove(getNumElements());
068      fail("remove(size) should throw");
069    } catch (IndexOutOfBoundsException expected) {
070    }
071    expectUnchanged();
072  }
073
074  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
075  @CollectionSize.Require(absent = ZERO)
076  public void testRemoveAtIndex_first() {
077    runRemoveTest(0);
078  }
079
080  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
081  @CollectionSize.Require(absent = {ZERO, ONE})
082  public void testRemoveAtIndex_middle() {
083    runRemoveTest(getNumElements() / 2);
084  }
085
086  @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
087  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
088  @CollectionSize.Require(absent = ZERO)
089  public void testRemoveAtIndexConcurrentWithIteration() {
090    try {
091      Iterator<E> iterator = collection.iterator();
092      getList().remove(getNumElements() / 2);
093      iterator.next();
094      fail("Expected ConcurrentModificationException");
095    } catch (ConcurrentModificationException expected) {
096      // success
097    }
098  }
099
100  @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
101  @CollectionSize.Require(absent = ZERO)
102  public void testRemoveAtIndex_last() {
103    runRemoveTest(getNumElements() - 1);
104  }
105
106  private void runRemoveTest(int index) {
107    assertEquals(
108        Platform.format("remove(%d) should return the element at index %d", index, index),
109        getList().get(index),
110        getList().remove(index));
111    List<E> expected = Helpers.copyToList(createSamplesArray());
112    expected.remove(index);
113    expectContents(expected);
114  }
115}