You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/09/24 20:13:25 UTC

(commons-collections) branch master updated: Add AbstractMapTest.testMapGetOrDefault()

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


The following commit(s) were added to refs/heads/master by this push:
     new 17aca847b Add AbstractMapTest.testMapGetOrDefault()
17aca847b is described below

commit 17aca847b15e5264e9628ce2df58f92674a1198f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Sep 24 16:13:16 2024 -0400

    Add AbstractMapTest.testMapGetOrDefault()
---
 .../commons/collections4/map/AbstractMapTest.java  | 149 +++++++++++++--------
 1 file changed, 90 insertions(+), 59 deletions(-)

diff --git a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java
index f025d318e..e39577bd1 100644
--- a/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/AbstractMapTest.java
@@ -1383,6 +1383,37 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
         }
     }
 
+    /**
+     * Tests {@link Map#getOrDefault(Object, Object)}.
+     */
+    @Test
+    public void testMapGetOrDefault() {
+        resetEmpty();
+        final K[] keys = getSampleKeys();
+        final V[] values = getSampleValues();
+        for (final K key : keys) {
+            assertNull(getMap().getOrDefault(key, null));
+        }
+        final K[] otherKeys = getOtherKeys();
+        final V[] otherValues = getOtherValues();
+        for (int i = 0; i < otherKeys.length; i++) {
+            final K otherKey = otherKeys[i];
+            assertNull(getMap().getOrDefault(otherKey, null));
+            final V otherValue = otherValues[i];
+            if (getMap().containsKey(otherKey)) {
+                assertEquals(getMap().get(otherKey), getMap().getOrDefault(otherKey, otherValue));
+            } else {
+                assertEquals(otherValue, getMap().getOrDefault(otherKey, otherValue));
+            }
+        }
+        // LazyMap does not like this check:
+        // verify();
+        resetFull();
+        for (int i = 0; i < keys.length; i++) {
+            assertEquals(values[i], getMap().getOrDefault(keys[i], values[i]));
+        }
+    }
+
     /**
      * Tests Map.hashCode()
      */
@@ -1484,6 +1515,65 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
         }
     }
 
+    /**
+     * Tests Map.putAll(map)
+     */
+    @Test
+    public void testMapPutAll() {
+        if (!isPutAddSupported()) {
+            if (!isPutChangeSupported()) {
+                final Map<K, V> temp = makeFullMap();
+                resetEmpty();
+                assertThrows(UnsupportedOperationException.class, () -> getMap().putAll(temp), "Expected UnsupportedOperationException on putAll");
+            }
+            return;
+        }
+
+        // check putAll OK adding empty map to empty map
+        resetEmpty();
+        assertEquals(0, getMap().size());
+        getMap().putAll(new HashMap<>());
+        assertEquals(0, getMap().size());
+
+        // check putAll OK adding empty map to non-empty map
+        resetFull();
+        final int size = getMap().size();
+        getMap().putAll(new HashMap<>());
+        assertEquals(size, getMap().size());
+
+        // check putAll OK adding non-empty map to empty map
+        resetEmpty();
+        Map<K, V> m2 = makeFullMap();
+        getMap().putAll(m2);
+        getConfirmed().putAll(m2);
+        verify();
+
+        // check putAll OK adding non-empty JDK map to empty map
+        resetEmpty();
+        m2 = makeConfirmedMap();
+        final K[] keys = getSampleKeys();
+        final V[] values = getSampleValues();
+        for (int i = 0; i < keys.length; i++) {
+            m2.put(keys[i], values[i]);
+        }
+        getMap().putAll(m2);
+        getConfirmed().putAll(m2);
+        verify();
+
+        // check putAll OK adding non-empty JDK map to non-empty map
+        resetEmpty();
+        m2 = makeConfirmedMap();
+        getMap().put(keys[0], values[0]);
+        getConfirmed().put(keys[0], values[0]);
+        verify();
+        for (int i = 1; i < keys.length; i++) {
+            m2.put(keys[i], values[i]);
+        }
+        getMap().putAll(m2);
+        getConfirmed().putAll(m2);
+        verify();
+    }
+
     /**
      * Tests {@link Map#putIfAbsent(Object, Object)}.
      */
@@ -1588,65 +1678,6 @@ public abstract class AbstractMapTest<K, V> extends AbstractObjectTest {
         }
     }
 
-    /**
-     * Tests Map.putAll(map)
-     */
-    @Test
-    public void testMapPutAll() {
-        if (!isPutAddSupported()) {
-            if (!isPutChangeSupported()) {
-                final Map<K, V> temp = makeFullMap();
-                resetEmpty();
-                assertThrows(UnsupportedOperationException.class, () -> getMap().putAll(temp), "Expected UnsupportedOperationException on putAll");
-            }
-            return;
-        }
-
-        // check putAll OK adding empty map to empty map
-        resetEmpty();
-        assertEquals(0, getMap().size());
-        getMap().putAll(new HashMap<>());
-        assertEquals(0, getMap().size());
-
-        // check putAll OK adding empty map to non-empty map
-        resetFull();
-        final int size = getMap().size();
-        getMap().putAll(new HashMap<>());
-        assertEquals(size, getMap().size());
-
-        // check putAll OK adding non-empty map to empty map
-        resetEmpty();
-        Map<K, V> m2 = makeFullMap();
-        getMap().putAll(m2);
-        getConfirmed().putAll(m2);
-        verify();
-
-        // check putAll OK adding non-empty JDK map to empty map
-        resetEmpty();
-        m2 = makeConfirmedMap();
-        final K[] keys = getSampleKeys();
-        final V[] values = getSampleValues();
-        for (int i = 0; i < keys.length; i++) {
-            m2.put(keys[i], values[i]);
-        }
-        getMap().putAll(m2);
-        getConfirmed().putAll(m2);
-        verify();
-
-        // check putAll OK adding non-empty JDK map to non-empty map
-        resetEmpty();
-        m2 = makeConfirmedMap();
-        getMap().put(keys[0], values[0]);
-        getConfirmed().put(keys[0], values[0]);
-        verify();
-        for (int i = 1; i < keys.length; i++) {
-            m2.put(keys[i], values[i]);
-        }
-        getMap().putAll(m2);
-        getConfirmed().putAll(m2);
-        verify();
-    }
-
     /**
      * Tests Map.put(null, value)
      */