Add AbstractMapTest.testReplaceKeyValue()

This commit is contained in:
Gary Gregory 2024-09-25 08:20:19 -04:00
parent aba168c497
commit 18c94780f1
1 changed files with 41 additions and 6 deletions

View File

@ -39,6 +39,7 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.apache.commons.collections4.AbstractObjectTest;
import org.apache.commons.collections4.BulkTest;
@ -1122,7 +1123,7 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
@Test
public void testForEach() {
resetFull();
AtomicInteger i = new AtomicInteger();
final AtomicInteger i = new AtomicInteger();
getMap().forEach((k, v) -> {
assertTrue(getMap().containsKey(k));
assertTrue(getMap().containsValue(v));
@ -1827,6 +1828,40 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
assertTrue(getMap().isEmpty());
}
/**
* Tests {@link Map#replace(Object, Object)}.
*/
@Test
public void testReplaceKeyValue() {
assumeTrue(isRemoveSupported());
resetFull();
final K[] sampleKeys = getSampleKeys();
final V[] sampleValues = getSampleValues();
final V[] newValues = getNewSampleValues();
assertFalse(getMap().isEmpty());
for (final AtomicInteger inc = new AtomicInteger(); inc.get() < sampleKeys.length; inc.incrementAndGet()) {
final int i = inc.get();
final V value = sampleValues[i];
final K sampleKey = sampleKeys[i];
final Supplier<String> messageSupplier = () -> String.format("[%,d] key '%s'; %s", inc.get(), sampleKey, getMap());
assertEquals(value, getMap().replace(sampleKey, value), messageSupplier);
assertEquals(value, getMap().replace(sampleKey, value), messageSupplier);
final V newValue = newValues[i];
assertEquals(value, getMap().replace(sampleKey, newValue), messageSupplier);
V oldValue = getMap().get(sampleKey);
assertEquals(oldValue, getMap().replace(sampleKey, newValue), messageSupplier);
oldValue = getMap().get(sampleKey);
assertEquals(oldValue, getMap().replace(sampleKey, value), messageSupplier);
if (isAllowNullValue()) {
final V expected = getMap().get(sampleKey);
assertEquals(expected, getMap().replace(sampleKey, null), messageSupplier);
assertNull(getMap().get(sampleKey), messageSupplier);
assertNull(getMap().replace(sampleKey, null), messageSupplier);
assertNull(getMap().replace(sampleKey, value), messageSupplier);
}
}
}
/**
* Test to ensure the test setup is working properly. This method checks to ensure that the getSampleKeys and getSampleValues methods are returning results
* that look appropriate. That is, they both return a non-null array of equal length. The keys array must not have any duplicate values, and may only