| 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 |
import java.util.ConcurrentModificationException; |
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.List; |
| 32 |
import org.junit.Ignore; |
| 33 |
|
| 34 |
/** |
| 35 |
* A generic JUnit test which tests {@code remove(int)} operations on a list. Can't be invoked |
| 36 |
* directly; please see {@link com.google.common.collect.testing.ListTestSuiteBuilder}. |
| 37 |
* |
| 38 |
* @author Chris Povirk |
| 39 |
*/ |
| 40 |
@GwtCompatible |
| 41 |
@Ignore // Affects only Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. |
| 42 |
public class ListRemoveAtIndexTester<E> extends AbstractListTester<E> { |
| 43 |
@ListFeature.Require(absent = SUPPORTS_REMOVE_WITH_INDEX) |
| 44 |
@CollectionSize.Require(absent = ZERO) |
| 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) |
| 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) |
| 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) |
| 76 |
public void testRemoveAtIndex_first() { |
| 77 |
runRemoveTest(0); |
| 78 |
} |
| 79 |
|
| 80 |
@ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) |
| 81 |
@CollectionSize.Require(absent = {ZERO, ONE}) |
| 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) |
| 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) |
| 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 |
} |