Skip to content

Commit

Permalink
Make testStableIterationOrder() test iteration order
Browse files Browse the repository at this point in the history
The test tested collection equality (which is ill-defined for
identity-based collections, as they violate general collection contracts
any way).
  • Loading branch information
findepi authored and martint committed Apr 10, 2017
1 parent 5fa170b commit 05a5958
Showing 1 changed file with 17 additions and 5 deletions.
Expand Up @@ -13,14 +13,18 @@
*/ */
package com.facebook.presto.util.maps; package com.facebook.presto.util.maps;


import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.testng.annotations.Test; import org.testng.annotations.Test;


import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;


import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.stream.IntStream.range; import static java.util.stream.IntStream.range;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
Expand Down Expand Up @@ -72,17 +76,25 @@ public void testUsesIdentityAsEquivalenceForKeys()
@Test @Test
public void testStableIterationOrder() public void testStableIterationOrder()
{ {
Set<String> keys = ImmutableSet.of("All", "your", "base", "are", "belong", "to", "us"); List<String> keys = ImmutableList.of("All", "your", "base", "are", "belong", "to", "us");
Map<String, Integer> expectedMap = Maps.asMap(keys, String::length); List<Integer> expectedValues = keys.stream().map(String::length).collect(toImmutableList());


range(0, 10).forEach(attempt -> { range(0, 10).forEach(attempt -> {
IdentityLinkedHashMap<String, Integer> map = new IdentityLinkedHashMap<>(); IdentityLinkedHashMap<String, Integer> map = new IdentityLinkedHashMap<>();


keys.forEach(i -> map.put(i, i.length())); keys.forEach(i -> map.put(i, i.length()));


assertEquals(map.keySet(), keys); assertEquals(ImmutableList.copyOf(map.keySet()), keys);
assertEquals(map.values(), expectedMap.values()); assertEquals(ImmutableList.copyOf(map.keySet().iterator()), keys);
assertEquals(map.entrySet(), expectedMap.entrySet()); assertEquals(map.keySet().stream().collect(toImmutableList()), keys);

assertEquals(ImmutableList.copyOf(map.values()), expectedValues);
assertEquals(ImmutableList.copyOf(map.values().iterator()), expectedValues);
assertEquals(map.values().stream().collect(toImmutableList()), expectedValues);

assertEquals(ImmutableList.copyOf(map.entrySet()).stream().map(Entry::getKey).collect(toImmutableList()), keys);
assertEquals(ImmutableList.copyOf(map.entrySet()::iterator).stream().map(Entry::getKey).collect(toImmutableList()), keys);
assertEquals(map.entrySet().stream().map(Entry::getKey).collect(toImmutableList()), keys);
}); });
} }
} }

0 comments on commit 05a5958

Please sign in to comment.