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.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
20 import static com.google.common.collect.testing.features.CollectionSize.ONE;
21 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22 import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_REMOVE_WITH_INDEX;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.Helpers;
26 import com.google.common.collect.testing.features.CollectionFeature;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import com.google.common.collect.testing.features.ListFeature;
29 
30 import java.util.ConcurrentModificationException;
31 import java.util.Iterator;
32 import 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  * @author Chris Povirk
40  */
41 @GwtCompatible
42 public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> {
43   @ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX)
44   @CollectionSize.Require(absent = ZERO)
testRemoveAtIndex_unsupported()45   public void testRemoveAtIndex_unsupported() {
46     try {
47       getList().remove(0);
48       fail("remove(i) should throw");
49     } catch (UnsupportedOperationException expected) {
50     }
51     expectUnchanged();
52   }
53 
54   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
testRemoveAtIndex_negative()55   public void testRemoveAtIndex_negative() {
56     try {
57       getList().remove(-1);
58       fail("remove(-1) should throw");
59     } catch (IndexOutOfBoundsException expected) {
60     }
61     expectUnchanged();
62   }
63 
64   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
testRemoveAtIndex_tooLarge()65   public void testRemoveAtIndex_tooLarge() {
66     try {
67       getList().remove(getNumElements());
68       fail("remove(size) should throw");
69     } catch (IndexOutOfBoundsException expected) {
70     }
71     expectUnchanged();
72   }
73 
74   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
75   @CollectionSize.Require(absent = ZERO)
testRemoveAtIndex_first()76   public void testRemoveAtIndex_first() {
77     runRemoveTest(0);
78   }
79 
80   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
81   @CollectionSize.Require(absent = {ZERO, ONE})
testRemoveAtIndex_middle()82   public void testRemoveAtIndex_middle() {
83     runRemoveTest(getNumElements() / 2);
84   }
85 
86   @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
87   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
88   @CollectionSize.Require(absent = ZERO)
testRemoveAtIndexConcurrentWithIteration()89   public void testRemoveAtIndexConcurrentWithIteration() {
90     try {
91       Iterator<E> iterator = collection.iterator();
92       getList().remove(getNumElements() / 2);
93       iterator.next();
94       fail("Expected ConcurrentModificationException");
95     } catch (ConcurrentModificationException expected) {
96       // success
97     }
98   }
99 
100   @ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX)
101   @CollectionSize.Require(absent = ZERO)
testRemoveAtIndex_last()102   public void testRemoveAtIndex_last() {
103     runRemoveTest(getNumElements() - 1);
104   }
105 
runRemoveTest(int index)106   private void runRemoveTest(int index) {
107     assertEquals(Platform.format(
108         "remove(%d) should return the element at index %d", index, index),
109         getList().get(index), getList().remove(index));
110     List<E> expected = Helpers.copyToList(createSamplesArray());
111     expected.remove(index);
112     expectContents(expected);
113   }
114 }
115