-
Notifications
You must be signed in to change notification settings - Fork 11.1k
testlib: Add ConcurrentMapSpliteratorTester for spliterator characteristics #8173
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
4d66578
d9764e2
fcdfd31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,6 +75,56 @@ public void testSpliteratorNotImmutable_collectionAllowsRemove() { | |
| assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the spliterator does not report both {@code CONCURRENT} and {@code SIZED} | ||
| * characteristics, which are incompatible per the {@link Spliterator} specification. | ||
| */ | ||
| public void testSpliteratorNotConcurrentAndSized() { | ||
| Spliterator<E> spliterator = collection.spliterator(); | ||
| assertFalse( | ||
| "spliterator should not have both CONCURRENT and SIZED characteristics", | ||
| spliterator.hasCharacteristics(Spliterator.CONCURRENT) | ||
| && spliterator.hasCharacteristics(Spliterator.SIZED)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the spliterator does not report both {@code CONCURRENT} and {@code IMMUTABLE} | ||
| * characteristics, which are incompatible per the {@link Spliterator} specification. | ||
| */ | ||
| public void testSpliteratorNotConcurrentAndImmutable() { | ||
| Spliterator<E> spliterator = collection.spliterator(); | ||
| assertFalse( | ||
| "spliterator should not have both CONCURRENT and IMMUTABLE characteristics", | ||
| spliterator.hasCharacteristics(Spliterator.CONCURRENT) | ||
| && spliterator.hasCharacteristics(Spliterator.IMMUTABLE)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that if the spliterator reports {@code SORTED}, it also reports {@code ORDERED}, as | ||
| * required by the {@link Spliterator} specification. | ||
| */ | ||
| public void testSpliteratorSortedRequiresOrdered() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Thanks for adding this as well. |
||
| Spliterator<E> spliterator = collection.spliterator(); | ||
| if (spliterator.hasCharacteristics(Spliterator.SORTED)) { | ||
| assertTrue( | ||
| "spliterator reporting SORTED must also report ORDERED", | ||
| spliterator.hasCharacteristics(Spliterator.ORDERED)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tests that if the spliterator reports {@code SUBSIZED}, it also reports {@code SIZED}, as | ||
| * required by the {@link Spliterator} specification. | ||
| */ | ||
| public void testSpliteratorSubsizedRequiresSized() { | ||
| Spliterator<E> spliterator = collection.spliterator(); | ||
| if (spliterator.hasCharacteristics(Spliterator.SUBSIZED)) { | ||
| assertTrue( | ||
| "spliterator reporting SUBSIZED must also report SIZED", | ||
| spliterator.hasCharacteristics(Spliterator.SIZED)); | ||
| } | ||
| } | ||
|
|
||
| @J2ktIncompatible | ||
| @GwtIncompatible // reflection | ||
| public static Method getSpliteratorNotImmutableCollectionAllowsAddMethod() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright (C) 2025 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.testers; | ||
|
|
||
| import com.google.common.annotations.GwtCompatible; | ||
| import com.google.common.collect.testing.AbstractMapTester; | ||
| import java.util.Spliterator; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.junit.Ignore; | ||
|
|
||
| /** | ||
| * A generic JUnit test which tests spliterator characteristics on concurrent map views. Verifies | ||
| * that {@link ConcurrentMap} implementations return spliterators with the {@link | ||
| * Spliterator#CONCURRENT} characteristic on their {@code entrySet()}, {@code keySet()}, and {@code | ||
| * values()} views. | ||
| * | ||
| * <p>Can't be invoked directly; please see {@link | ||
| * com.google.common.collect.testing.ConcurrentMapTestSuiteBuilder}. | ||
| * | ||
| * @author Guava Authors | ||
| */ | ||
| @GwtCompatible | ||
| @Ignore("test runners must not instantiate and run this directly, only via suites we build") | ||
| // @Ignore affects the Android test runner, which respects JUnit 4 annotations on JUnit 3 tests. | ||
| @SuppressWarnings("JUnit4ClassUsedInJUnit3") | ||
| @NullMarked | ||
| public class ConcurrentMapSpliteratorTester<K, V> extends AbstractMapTester<K, V> { | ||
| @Override | ||
| protected ConcurrentMap<K, V> getMap() { | ||
| return (ConcurrentMap<K, V>) super.getMap(); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the spliterator returned by {@code entrySet().spliterator()} has the {@code | ||
| * CONCURRENT} characteristic. | ||
| */ | ||
| public void testEntrySetSpliteratorHasConcurrent() { | ||
| Spliterator<?> spliterator = getMap().entrySet().spliterator(); | ||
| assertTrue( | ||
| "entrySet().spliterator() should have CONCURRENT characteristic", | ||
| spliterator.hasCharacteristics(Spliterator.CONCURRENT)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the spliterator returned by {@code keySet().spliterator()} has the {@code | ||
| * CONCURRENT} characteristic. | ||
| */ | ||
| public void testKeySetSpliteratorHasConcurrent() { | ||
| Spliterator<?> spliterator = getMap().keySet().spliterator(); | ||
| assertTrue( | ||
| "keySet().spliterator() should have CONCURRENT characteristic", | ||
| spliterator.hasCharacteristics(Spliterator.CONCURRENT)); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the spliterator returned by {@code values().spliterator()} has the {@code | ||
| * CONCURRENT} characteristic. | ||
| */ | ||
| public void testValuesSpliteratorHasConcurrent() { | ||
| Spliterator<?> spliterator = getMap().values().spliterator(); | ||
| assertTrue( | ||
| "values().spliterator() should have CONCURRENT characteristic", | ||
| spliterator.hasCharacteristics(Spliterator.CONCURRENT)); | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new tester here, we can maybe override the
createDerivedEntrySetSuitecreateDerivedKeySetSuitecreateDerivedValueCollectionSuitemethods to append a
ConcurrentCollectionSpliteratorTester, so it could be reused for arbitrary concurrent collections.