-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Use SUPPORTS_REMOVE for CollectionRemoveIfTester feature requirements.
#8643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Divyansh151005
wants to merge
2
commits into
google:master
from
Divyansh151005:fix/collection-removeif-feature-flags
+131
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
guava-testlib/test/com/google/common/collect/testing/CollectionRemoveIfFeatureTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Guava Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.common.collect.testing; | ||
|
|
||
| import static com.google.common.collect.testing.features.CollectionFeature.KNOWN_ORDER; | ||
| import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE; | ||
| import static com.google.common.collect.testing.features.CollectionSize.SEVERAL; | ||
|
|
||
| import com.google.common.collect.Lists; | ||
| import java.util.AbstractList; | ||
| import java.util.Collection; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.function.Predicate; | ||
| import junit.framework.Test; | ||
| import junit.framework.TestCase; | ||
| import junit.framework.TestSuite; | ||
|
|
||
| /** | ||
| * Regression test for {@link testers.CollectionRemoveIfTester} feature requirements. | ||
| */ | ||
| @AndroidIncompatible // test-suite builders | ||
| public class CollectionRemoveIfFeatureTest extends TestCase { | ||
|
|
||
| /** | ||
| * A collection that supports {@link Collection#removeIf} and {@link Collection#remove} but whose | ||
| * iterator does not support {@link Iterator#remove()}. | ||
| */ | ||
| private static final class RemoveIfOnlyList extends AbstractList<String> { | ||
| private final List<String> elements = Lists.newArrayList("a", "b", "c"); | ||
|
|
||
| void resetTo(String[] newElements) { | ||
| elements.clear(); | ||
| elements.addAll(Lists.newArrayList(newElements)); | ||
| } | ||
|
|
||
| @Override | ||
| public String get(int index) { | ||
| return elements.get(index); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return elements.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean remove(Object o) { | ||
| return elements.remove(o); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean removeIf(Predicate<? super String> filter) { | ||
| return elements.removeIf(filter); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<String> iterator() { | ||
| return new Iterator<String>() { | ||
| private int index; | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return index < size(); | ||
| } | ||
|
|
||
| @Override | ||
| public String next() { | ||
| return get(index++); | ||
| } | ||
|
|
||
| @Override | ||
| public void remove() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| public void testRemoveIfTestsRunWhenOnlySupportsRemove() { | ||
| TestSuite suite = | ||
| CollectionTestSuiteBuilder.using( | ||
| new TestStringCollectionGenerator() { | ||
| @Override | ||
| protected Collection<String> create(String[] elements) { | ||
| RemoveIfOnlyList list = new RemoveIfOnlyList(); | ||
| list.resetTo(elements); | ||
| return list; | ||
| } | ||
| }) | ||
| .named("RemoveIfOnlyList") | ||
| .withFeatures(SUPPORTS_REMOVE, KNOWN_ORDER, SEVERAL) | ||
| .createTestSuite(); | ||
|
|
||
| int removeIfTestCount = countTestsWithNameContaining(suite, "testRemoveIf"); | ||
| assertTrue( | ||
| "removeIf tests should run for collections with SUPPORTS_REMOVE, even without" | ||
| + " SUPPORTS_ITERATOR_REMOVE", | ||
| removeIfTestCount > 0); | ||
| } | ||
|
|
||
| private static int countTestsWithNameContaining(Test test, String substring) { | ||
| if (test instanceof TestSuite) { | ||
| TestSuite testSuite = (TestSuite) test; | ||
| int count = 0; | ||
| for (int i = 0; i < testSuite.testCount(); i++) { | ||
| count += countTestsWithNameContaining(testSuite.testAt(i), substring); | ||
| } | ||
| return count; | ||
| } | ||
| return test.toString().contains(substring) ? 1 : 0; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.