[GitHub] kinow closed pull request #379: add tests for use ImmutableTriple as key in java.util.HashMap and java.util.TreeMap

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] kinow closed pull request #379: add tests for use ImmutableTriple as key in java.util.HashMap and java.util.TreeMap

GitBox
kinow closed pull request #379: add tests for use ImmutableTriple as key in java.util.HashMap and java.util.TreeMap
URL: https://github.com/apache/commons-lang/pull/379
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java b/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java
index 260879a95..a5b0d9d20 100644
--- a/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java
+++ b/src/test/java/org/apache/commons/lang3/tuple/ImmutablePairTest.java
@@ -26,6 +26,11 @@
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map.Entry;
+import java.util.TreeMap;
 
 import org.junit.jupiter.api.Test;
 
@@ -136,4 +141,34 @@ public void testSerialization() throws Exception {
         assertEquals(origPair, deserializedPair);
         assertEquals(origPair.hashCode(), deserializedPair.hashCode());
     }
+
+    @Test
+    public void testUseAsKeyOfHashMap() {
+        HashMap<ImmutablePair<Object, Object>, String> map = new HashMap<>();
+        Object o1 = new Object();
+        Object o2 = new Object();
+        ImmutablePair<Object, Object> key1 = ImmutablePair.of(o1, o2);
+        String value1 = "a1";
+        map.put(key1, value1);
+        assertEquals(value1, map.get(key1));
+        assertEquals(value1, map.get(ImmutablePair.of(o1, o2)));
+    }
+
+    @Test
+    public void testUseAsKeyOfTreeMap() {
+        TreeMap<ImmutablePair<Integer, Integer>, String> map = new TreeMap<>();
+        map.put(ImmutablePair.of(1, 2), "12");
+        map.put(ImmutablePair.of(1, 1), "11");
+        map.put(ImmutablePair.of(0, 1), "01");
+        ArrayList<ImmutablePair<Integer, Integer>> expected = new ArrayList<>();
+        expected.add(ImmutablePair.of(0, 1));
+        expected.add(ImmutablePair.of(1, 1));
+        expected.add(ImmutablePair.of(1, 2));
+        Iterator<Entry<ImmutablePair<Integer, Integer>, String>> it = map.entrySet().iterator();
+        for(ImmutablePair<Integer, Integer> item : expected) {
+            Entry<ImmutablePair<Integer, Integer>, String> entry = it.next();
+            assertEquals(item, entry.getKey());
+            assertEquals(item.getLeft() + "" + item.getRight(), entry.getValue());
+        }
+    }
 }
diff --git a/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java b/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java
index 8f76000bd..fe73930bb 100644
--- a/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java
+++ b/src/test/java/org/apache/commons/lang3/tuple/ImmutableTripleTest.java
@@ -26,6 +26,11 @@
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.TreeMap;
+import java.util.Map.Entry;
 
 import org.junit.jupiter.api.Test;
 
@@ -142,5 +147,36 @@ public void testSerialization() throws Exception {
         assertEquals(origTriple, deserializedTriple);
         assertEquals(origTriple.hashCode(), deserializedTriple.hashCode());
     }
+
+    @Test
+    public void testUseAsKeyOfHashMap() {
+        HashMap<ImmutableTriple<Object, Object, Object>, String> map = new HashMap<>();
+        Object o1 = new Object();
+        Object o2 = new Object();
+        Object o3 = new Object();
+        ImmutableTriple<Object, Object, Object> key1 = ImmutableTriple.of(o1, o2, o3);
+        String value1 = "a1";
+        map.put(key1, value1);
+        assertEquals(value1, map.get(key1));
+        assertEquals(value1, map.get(ImmutableTriple.of(o1, o2, o3)));
+    }
+
+    @Test
+    public void testUseAsKeyOfTreeMap() {
+        TreeMap<ImmutableTriple<Integer, Integer, Integer>, String> map = new TreeMap<>();
+        map.put(ImmutableTriple.of(0, 1, 2), "012");
+        map.put(ImmutableTriple.of(0, 1, 1), "011");
+        map.put(ImmutableTriple.of(0, 0, 1), "001");
+        ArrayList<ImmutableTriple<Integer, Integer, Integer>> expected = new ArrayList<>();
+        expected.add(ImmutableTriple.of(0, 0, 1));
+        expected.add(ImmutableTriple.of(0, 1, 1));
+        expected.add(ImmutableTriple.of(0, 1, 2));
+        Iterator<Entry<ImmutableTriple<Integer, Integer, Integer>, String>> it = map.entrySet().iterator();
+        for(ImmutableTriple<Integer, Integer, Integer> item : expected) {
+            Entry<ImmutableTriple<Integer, Integer, Integer>, String> entry = it.next();
+            assertEquals(item, entry.getKey());
+            assertEquals(item.getLeft() + "" + item.getMiddle() + "" + item.getRight(), entry.getValue());
+        }
+    }
 }
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services