From bf7767867378d9d33551ad54687b48c8fbecf424 Mon Sep 17 00:00:00 2001 From: Apirom Na Nakorn Date: Tue, 30 Oct 2018 22:28:31 +0700 Subject: [PATCH] add test use ImmutablePair class with java.util.HashMap and java.util.TreeMap --- .../lang3/tuple/ImmutablePairTest.java | 35 ++++++++++++++++++ .../lang3/tuple/ImmutableTripleTest.java | 36 +++++++++++++++++++ 2 files changed, 71 insertions(+) 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.ByteArrayInputStream; 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 class ImmutablePairTest { assertEquals(origPair, deserializedPair); assertEquals(origPair.hashCode(), deserializedPair.hashCode()); } + + @Test + public void testUseAsKeyOfHashMap() { + HashMap, String> map = new HashMap<>(); + Object o1 = new Object(); + Object o2 = new Object(); + ImmutablePair 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, 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> expected = new ArrayList<>(); + expected.add(ImmutablePair.of(0, 1)); + expected.add(ImmutablePair.of(1, 1)); + expected.add(ImmutablePair.of(1, 2)); + Iterator, String>> it = map.entrySet().iterator(); + for(ImmutablePair item : expected) { + Entry, 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.ByteArrayInputStream; 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 class ImmutableTripleTest { assertEquals(origTriple, deserializedTriple); assertEquals(origTriple.hashCode(), deserializedTriple.hashCode()); } + + @Test + public void testUseAsKeyOfHashMap() { + HashMap, String> map = new HashMap<>(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + ImmutableTriple 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, 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> 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, String>> it = map.entrySet().iterator(); + for(ImmutableTriple item : expected) { + Entry, String> entry = it.next(); + assertEquals(item, entry.getKey()); + assertEquals(item.getLeft() + "" + item.getMiddle() + "" + item.getRight(), entry.getValue()); + } + } }