Skip to content

Commit

Permalink
Merge pull request #1678 from motlin/map-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
motlin authored Oct 16, 2024
2 parents 5020bf0 + ddb02d6 commit 913adc0
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.eclipse.collections.api.bimap.MutableBiMap;
import org.eclipse.collections.impl.bimap.mutable.HashBiMap;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.test.NoIteratorTestCase;
import org.eclipse.collections.test.map.mutable.MapTestCase;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class HashBiMapNoIteratorTest implements MutableBiMapTestCase, NoIteratorTestCase
Expand Down Expand Up @@ -85,6 +90,32 @@ public void RichIterable_getLast()
NoIteratorTestCase.super.RichIterable_getLast();
}

@Override
@Test
public void MapIterable_keySet_equals()
{
// HashBiMap.keySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
Set<Integer> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertThrows(AssertionError.class, () -> map.keySet().equals(expected));
}

@Override
@Test
public void MapIterable_entrySet_equals()
{
// HashBiMap.entrySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(1, "One", 2, "Two", 3, "Three");
Set<Map.Entry<Integer, String>> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(ImmutableEntry.of(1, "One"));
expected.add(ImmutableEntry.of(2, "Two"));
expected.add(ImmutableEntry.of(3, "Three"));
assertThrows(AssertionError.class, () -> map.entrySet().equals(expected));
}

public static class HashBiMapNoIterator<K, V> extends HashBiMap<K, V>
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@

package org.eclipse.collections.test.map;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.eclipse.collections.api.collection.MutableCollection;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.impl.block.procedure.CollectionAddProcedure;
import org.eclipse.collections.impl.list.Interval;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.test.SerializeTestHelper;
import org.eclipse.collections.impl.test.Verify;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.test.RichIterableWithDuplicatesTestCase;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -334,4 +341,53 @@ default void MapIterable_forEach()
map2.forEach((key, value) -> forEach2.add(key + value));
assertIterablesEqual(this.newMutableForFilter("3Three", "2Two", "1Three"), forEach2);
}

@Test
default void MapIterable_keySet_equals()
{
Map<Integer, String> map = (Map<Integer, String>) this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
Set<Integer> expected = new HashSetNoIterator<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertEquals(expected, map.keySet());
}

@Test
default void MapIterable_keySet_forEach()
{
Map<Integer, String> map = (Map<Integer, String>) this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableSet<Integer> actual = Sets.mutable.with();
map.keySet().forEach(actual::add);
assertEquals(Sets.immutable.with(3, 2, 1), actual);
}

@Test
default void MapIterable_entrySet_equals()
{
Map<Integer, String> map = (Map<Integer, String>) this.newWithKeysValues(1, "One", 2, "Two", 3, "Three");
Set<Map.Entry<Integer, String>> expected = new HashSetNoIterator<>();
expected.add(ImmutableEntry.of(1, "One"));
expected.add(ImmutableEntry.of(2, "Two"));
expected.add(ImmutableEntry.of(3, "Three"));
assertEquals(expected, map.entrySet());
}

@Test
default void MapIterable_entrySet_forEach()
{
Map<Integer, String> map = (Map<Integer, String>) this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableSet<String> actual = Sets.mutable.with();
map.entrySet().forEach(each -> actual.add(each.getValue()));
assertEquals(Sets.immutable.with("Three", "Two", "One"), actual);
}

class HashSetNoIterator<T> extends HashSet<T>
{
@Override
public Iterator<T> iterator()
{
throw new AssertionError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

package org.eclipse.collections.test.map.mutable;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.collections.api.factory.Sets;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -85,6 +90,46 @@ default void Map_remove()
}
}

@Test
default void Map_keySet_equals()
{
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
Set<Integer> expected = new HashSetNoIterator<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertEquals(expected, map.keySet());
}

@Test
default void Map_keySet_forEach()
{
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableSet<Integer> actual = Sets.mutable.with();
map.keySet().forEach(actual::add);
assertEquals(Sets.immutable.with(3, 2, 1), actual);
}

@Test
default void Map_entrySet_equals()
{
Map<Integer, String> map = this.newWithKeysValues(1, "One", 2, "Two", 3, "Three");
Set<Map.Entry<Integer, String>> expected = new HashSetNoIterator<>();
expected.add(ImmutableEntry.of(1, "One"));
expected.add(ImmutableEntry.of(2, "Two"));
expected.add(ImmutableEntry.of(3, "Three"));
assertEquals(expected, map.entrySet());
}

@Test
default void Map_entrySet_forEach()
{
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
MutableSet<String> actual = Sets.mutable.with();
map.entrySet().forEach(each -> actual.add(each.getValue()));
assertEquals(Sets.immutable.with("Three", "Two", "One"), actual);
}

@Test
default void Map_entrySet_remove()
{
Expand Down Expand Up @@ -275,4 +320,13 @@ public int compareTo(AlwaysEqual o)
return 0;
}
}

class HashSetNoIterator<T> extends HashSet<T>
{
@Override
public Iterator<T> iterator()
{
throw new AssertionError();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.test.NoIteratorTestCase;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class UnifiedMapNoIteratorTest implements MutableMapTestCase, NoIteratorTestCase
Expand Down Expand Up @@ -97,6 +101,46 @@ public void RichIterable_fused_collectMakeString()
// Not applicable
}

@Override
@Test
public void Map_keySet_equals()
{
// UnifiedMap.keySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
Set<Integer> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertThrows(AssertionError.class, () -> map.keySet().equals(expected));
}

@Override
@Test
public void Map_entrySet_equals()
{
// UnifiedMap.entrySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(1, "One", 2, "Two", 3, "Three");
Set<Map.Entry<Integer, String>> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(ImmutableEntry.of(1, "One"));
expected.add(ImmutableEntry.of(2, "Two"));
expected.add(ImmutableEntry.of(3, "Three"));
assertThrows(AssertionError.class, () -> map.entrySet().equals(expected));
}

@Override
@Test
public void MapIterable_keySet_equals()
{
this.Map_keySet_equals();
}

@Override
@Test
public void MapIterable_entrySet_equals()
{
this.Map_entrySet_equals();
}

@Override
public void MutableMapIterable_updateValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.block.factory.HashingStrategies;
import org.eclipse.collections.impl.map.strategy.mutable.UnifiedMapWithHashingStrategy;
import org.eclipse.collections.impl.tuple.ImmutableEntry;
import org.eclipse.collections.test.NoIteratorTestCase;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;

public class UnifiedMapWithHashingStrategyNoIteratorTest implements MutableMapTestCase, NoIteratorTestCase
Expand Down Expand Up @@ -126,6 +130,46 @@ public void MutableMapIterable_updateValue()
*/
}

@Override
@Test
public void Map_keySet_equals()
{
// UnifiedSetWithHashingStrategy.keySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(3, "Three", 2, "Two", 1, "One");
Set<Integer> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(3);
expected.add(2);
expected.add(1);
assertThrows(AssertionError.class, () -> map.keySet().equals(expected));
}

@Override
@Test
public void Map_entrySet_equals()
{
// UnifiedSetWithHashingStrategy.entrySet().equals() delegates to the other Map's entrySet()
Map<Integer, String> map = this.newWithKeysValues(1, "One", 2, "Two", 3, "Three");
Set<Map.Entry<Integer, String>> expected = new MapTestCase.HashSetNoIterator<>();
expected.add(ImmutableEntry.of(1, "One"));
expected.add(ImmutableEntry.of(2, "Two"));
expected.add(ImmutableEntry.of(3, "Three"));
assertThrows(AssertionError.class, () -> map.entrySet().equals(expected));
}

@Override
@Test
public void MapIterable_keySet_equals()
{
this.Map_keySet_equals();
}

@Override
@Test
public void MapIterable_entrySet_equals()
{
this.Map_entrySet_equals();
}

public static class UnifiedMapWithHashingStrategyNoIterator<K, V> extends UnifiedMapWithHashingStrategy<K, V>
{
public UnifiedMapWithHashingStrategyNoIterator()
Expand Down

0 comments on commit 913adc0

Please sign in to comment.