Skip to content

Commit

Permalink
Add AbstractMapTest.testReplaceKeyValueValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 25, 2024
1 parent 18c9478 commit f4f3e36
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1853,15 +1853,57 @@ public void testReplaceKeyValue() {
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);
oldValue = getMap().get(sampleKey);
assertEquals(oldValue, getMap().replace(sampleKey, null), messageSupplier);
assertNull(getMap().get(sampleKey), messageSupplier);
assertNull(getMap().replace(sampleKey, null), messageSupplier);
assertNull(getMap().replace(sampleKey, value), messageSupplier);
}
}
}

/**
* Tests {@link Map#replace(Object, Object, Object)}.
*/
@Test
public void testReplaceKeyValueValue() {
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 key = sampleKeys[i];
final Supplier<String> messageSupplier = () -> String.format("[%,d] key '%s', value '%s'; %s", inc.get(), key, value, getMap());
boolean containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, value, value), messageSupplier);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, value, value), messageSupplier);
final V newValue = newValues[i];
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, value, newValue), messageSupplier);
V oldValue = getMap().get(key);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, oldValue, newValue), messageSupplier);
oldValue = getMap().get(key);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, oldValue, value), messageSupplier);
if (isAllowNullValue()) {
oldValue = getMap().get(key);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, oldValue, null), messageSupplier);
assertNull(getMap().get(key), messageSupplier);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, null, null), messageSupplier);
containsKey = getMap().containsKey(key);
assertEquals(containsKey, getMap().replace(key, null, 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
Expand Down

0 comments on commit f4f3e36

Please sign in to comment.