Merge branch 'pr-379'

This closes pr #379

Thanks to @apirom9
This commit is contained in:
Bruno P. Kinoshita 2019-01-08 19:48:34 +13:00
commit 104cc28105
2 changed files with 71 additions and 0 deletions

View File

@ -26,6 +26,11 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; 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; import org.junit.jupiter.api.Test;
@ -136,4 +141,34 @@ public class ImmutablePairTest {
assertEquals(origPair, deserializedPair); assertEquals(origPair, deserializedPair);
assertEquals(origPair.hashCode(), deserializedPair.hashCode()); 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());
}
}
} }

View File

@ -26,6 +26,11 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; 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; import org.junit.jupiter.api.Test;
@ -142,5 +147,36 @@ public class ImmutableTripleTest {
assertEquals(origTriple, deserializedTriple); assertEquals(origTriple, deserializedTriple);
assertEquals(origTriple.hashCode(), deserializedTriple.hashCode()); 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());
}
}
} }