Map Tests & BugFixes.

-Added: Tests for all map implementations.
-Added: Missing Map Constructors.
-Fixed: Bugs with Maps & Sets.
-Fixed: Gradle Java Container.
-Fixed: Some javadoc stuff.
-Note: SubMap/List implementation are not really well tested and most likely buggy
-Changed: set JavaDoc to be quiet for now. Later goal.
This commit is contained in:
2021-04-22 23:02:04 +02:00
parent aaee550ea9
commit 0b11c3929a
43 changed files with 1206 additions and 149 deletions
@@ -0,0 +1,152 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
import speiger.src.collections.ints.utils.IntStrategy;
import speiger.src.collections.tests.MapTests;
public abstract class BaseInt2IntMapTest
{
protected static final IntStrategy STRATEGY = new Strategy();
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
protected static final int[] PUT_ARRAY = IntStream.range(512, 1024).toArray();
protected static final int[] PUT_VALUE_ARRAY = IntStream.range(0, 512).toArray();
public abstract Int2IntMap createMap(int[] keys, int[] values);
public abstract Int2IntMap createEmptyMap();
public EnumSet<MapTests> getValidMapTests() { return EnumSet.allOf(MapTests.class); }
@Test
public void testPut()
{
if(!getValidMapTests().contains(MapTests.PUT)) return;
Int2IntMap putMap = createMap(PUT_ARRAY, PUT_VALUE_ARRAY);
Assert.assertEquals(PUT_ARRAY.length, putMap.size());
Assert.assertEquals(0, putMap.put(0, 512));
Assert.assertEquals(1, putMap.put(513, 2));
Assert.assertEquals(PUT_ARRAY.length + 1, putMap.size());
Assert.assertEquals(512, putMap.addTo(0, 1));
Assert.assertEquals(513, putMap.getInt(0));
}
@Test
public void testPutAll()
{
if(!getValidMapTests().contains(MapTests.PUT_ALL)) return;
Int2IntMap putMap = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, putMap.size());
putMap.putAll(createMap(PUT_ARRAY, PUT_VALUE_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + PUT_ARRAY.length, putMap.size());
putMap = createMap(TEST_ARRAY, TEST_ARRAY);
putMap.putAll(createMap(PUT_VALUE_ARRAY, PUT_ARRAY));
Assert.assertEquals(PUT_ARRAY.length, putMap.size());
}
@Test
public void testContains()
{
if(!getValidMapTests().contains(MapTests.CONTAINS)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertTrue(map.containsKey(0));
Assert.assertFalse(map.containsKey(-1));
Assert.assertTrue(map.containsKey(Integer.valueOf(10)));
Assert.assertFalse(map.containsKey(Short.valueOf((short)10)));
Assert.assertTrue(map.containsValue(50));
Assert.assertFalse(map.containsValue(150));
Assert.assertTrue(map.containsValue(Integer.valueOf(10)));
Assert.assertFalse(map.containsValue(Short.valueOf((short)10)));
}
@Test
public void testReplace()
{
if(!getValidMapTests().contains(MapTests.REPLACE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(0, map.replace(0, 512));
Assert.assertEquals(512, map.getInt(0));
Assert.assertTrue(map.replace(0, 512, 0));
Assert.assertFalse(map.replace(0, 512, 0));
map = createMap(TEST_ARRAY, TEST_ARRAY);
map.replaceInts((K, V) -> 99 - V);
Assert.assertEquals(99, map.getInt(0));
Assert.assertEquals(0, map.getInt(99));
}
@Test
public void testCompute()
{
if(!getValidMapTests().contains(MapTests.COMPUTE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(512, map.computeInt(0, (K, V) -> 512));
Assert.assertEquals(512, map.getInt(0));
Assert.assertEquals(512, map.computeIntIfAbsent(0, T -> 0));
Assert.assertEquals(0, map.computeIntIfPresent(0, (T, V) -> 0));
Assert.assertEquals(0, map.computeIntIfAbsent(-10, T -> 0));
}
@Test
public void testMerge()
{
if(!getValidMapTests().contains(MapTests.MERGE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(50, map.mergeInt(1, 50, Integer::max));
Assert.assertEquals(2, map.mergeInt(2, 50, Integer::min));
}
@Test
public void testGet()
{
if(!getValidMapTests().contains(MapTests.GET)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = 0;i<TEST_ARRAY.length;i++)
{
Assert.assertEquals(i, map.getInt(i));
}
}
@Test
public void testIterators()
{
if(!getValidMapTests().contains(MapTests.ITERATORS)) return;
Int2IntMap map = createMap(PUT_VALUE_ARRAY, PUT_ARRAY);
map.forEach((K, V) -> Assert.assertEquals(PUT_ARRAY[K], V));
map.int2IntEntrySet().forEach(T -> Assert.assertEquals(PUT_ARRAY[T.getIntKey()], T.getIntValue()));
map.keySet().forEach(T -> Assert.assertEquals(PUT_VALUE_ARRAY[T], T));
map.values().forEach(T -> Assert.assertTrue(T >= 512 && T <= 1024));
Assert.assertTrue(map.keySet().contains(50));
Assert.assertFalse(map.keySet().contains(-50));
Assert.assertTrue(map.values().contains(1000));
Assert.assertFalse(map.values().contains(-1000));
}
@Test
public void testRemove()
{
if(!getValidMapTests().contains(MapTests.REMOVE)) return;
Int2IntMap map = createMap(PUT_VALUE_ARRAY, PUT_ARRAY);
Assert.assertEquals(PUT_ARRAY[50], map.remInt(PUT_VALUE_ARRAY[50]));
Assert.assertTrue(map.remove(PUT_VALUE_ARRAY[51], PUT_ARRAY[51]));
}
public static class Strategy implements IntStrategy
{
@Override
public int hashCode(int o)
{
return o;
}
@Override
public boolean equals(int key, int value)
{
return key == value;
}
}
}
@@ -0,0 +1,92 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.NavigableSetTest;
public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTest
{
@Override
public abstract Int2IntNavigableMap createMap(int[] keys, int[] values);
@Override
public abstract Int2IntNavigableMap createEmptyMap();
public EnumSet<NavigableSetTest> getValidNavigableMapTests() { return EnumSet.allOf(NavigableSetTest.class); }
@Test
public void desendingTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.DESENDING)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY).descendingMap();
Assert.assertEquals(TEST_ARRAY[TEST_ARRAY.length - 1], set.firstIntKey());
Assert.assertEquals(TEST_ARRAY[0], set.lastIntKey());
}
}
@Test
public void lowerTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.LOWER)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).lowerKey(50) < 50);
}
}
@Test
public void higherTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.HIGHER)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).higherKey(50) > 50);
}
}
@Test
public void ceilTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.CEILING)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).ceilingKey(50) >= 50);
}
}
@Test
public void floorTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.FLOOR)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).floorKey(50) <= 50);
}
}
@Test
public void naviSubSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.SUB_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.subMap(25, 75);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
@Test
public void naviHeadSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.HEAD_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.headMap(75);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
@Test
public void naviTailSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.TAIL_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.tailMap(25);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
}
@@ -0,0 +1,116 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public abstract class BaseInt2IntSortedMapTest extends BaseInt2IntMapTest
{
@Override
public abstract Int2IntSortedMap createMap(int[] keys, int[] values);
@Override
public abstract Int2IntSortedMap createEmptyMap();
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.allOf(SortedMapTests.class); }
@Test
public void testPutMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.PUT_MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(0, map.putAndMoveToFirst(120, -1));
Assert.assertEquals(120, map.firstIntKey());
Assert.assertEquals(-1, map.firstIntValue());
Assert.assertEquals(0, map.putAndMoveToLast(121, -2));
Assert.assertEquals(121, map.lastIntKey());
Assert.assertEquals(-2, map.lastIntValue());
}
@Test
public void testMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertTrue(map.moveToFirst(99));
Assert.assertFalse(map.moveToFirst(99));
Assert.assertEquals(99, map.firstIntKey());
Assert.assertTrue(map.moveToLast(0));
Assert.assertFalse(map.moveToLast(0));
Assert.assertEquals(0, map.lastIntKey());
}
@Test
public void testGetMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.GET_MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertNotEquals(99, map.firstIntValue());
Assert.assertEquals(99, map.getAndMoveToFirst(99));
Assert.assertEquals(99, map.firstIntValue());
Assert.assertNotEquals(0, map.lastIntValue());
Assert.assertEquals(0, map.getAndMoveToLast(0));
Assert.assertEquals(0, map.lastIntValue());
}
@Test
public void testFirst()
{
if(!getValidSortedMapTests().contains(SortedMapTests.FIRST)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = 0;i<TEST_ARRAY.length;i++)
{
Assert.assertEquals(TEST_ARRAY[i], map.pollFirstIntKey());
}
Assert.assertEquals(0, map.size());
}
@Test
public void testLast()
{
if(!getValidSortedMapTests().contains(SortedMapTests.LAST)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = TEST_ARRAY.length-1;i>=0;i--)
{
Assert.assertEquals(TEST_ARRAY[i], map.pollLastIntKey());
}
Assert.assertEquals(0, map.size());
}
@Test
public void testSubMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.SUB_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.subMap(25, 75);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(20));
Assert.assertFalse(subMap.containsKey(80));
}
@Test
public void testHeadMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.HEAD_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.headMap(75);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(80));
}
@Test
public void testTailMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.TAIL_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.tailMap(25);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(20));
}
}
@@ -2,6 +2,7 @@ package speiger.src.collections.ints.base;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
@@ -16,6 +17,7 @@ import speiger.src.collections.tests.CollectionTest;
public abstract class BaseIntCollectionTest extends BaseIntIterableTest
{
protected static final int[] ADD_ARRAY = new int[]{3212, -12, 423, -182, -4912};
protected static final int[] BULK_ADD_ARRAY = IntStream.range(200, 500).toArray();
protected static final int[] CONTAINS_ARRAY = new int[]{23, 45, 63, 89, 32};
@Override
@@ -40,6 +42,10 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + ADD_ARRAY.length, collection.size());
collection = create(TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(BULK_ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + BULK_ADD_ARRAY.length, collection.size());
}
@Test
@@ -15,6 +15,15 @@ public abstract class BaseIntNavigableSetTest extends BaseIntSortedSetTest
protected EnumSet<NavigableSetTest> getValidNavigableSetTests() { return EnumSet.allOf(NavigableSetTest.class); }
@Test
public void desendingTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.DESENDING)) {
IntNavigableSet set = create(TEST_ARRAY).descendingSet();
Assert.assertEquals(TEST_ARRAY[TEST_ARRAY.length - 1], set.firstInt());
Assert.assertEquals(TEST_ARRAY[0], set.lastInt());
}
}
@Test
public void lowerTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.LOWER)) {
@@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntNavigableMapTest;
import speiger.src.collections.ints.maps.impl.tree.Int2IntAVLTreeMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntAVLTreeMapTest extends BaseInt2IntNavigableMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.GET_MOVE, SortedMapTests.MOVE, SortedMapTests.PUT_MOVE)); }
@Override
public Int2IntNavigableMap createMap(int[] keys, int[] values)
{
return new Int2IntAVLTreeMap(keys, values);
}
@Override
public Int2IntNavigableMap createEmptyMap()
{
return new Int2IntAVLTreeMap();
}
}
@@ -0,0 +1,20 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.misc.Int2IntArrayMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
public class Int2IntArrayMapTest extends BaseInt2IntSortedMapTest
{
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntArrayMap(keys, values);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntArrayMap();
}
}
@@ -0,0 +1,20 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntMapTest;
import speiger.src.collections.ints.maps.impl.customHash.Int2IntOpenCustomHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
public class Int2IntCustomHashMapTest extends BaseInt2IntMapTest
{
@Override
public Int2IntMap createMap(int[] keys, int[] values)
{
return new Int2IntOpenCustomHashMap(keys, values, STRATEGY);
}
@Override
public Int2IntMap createEmptyMap()
{
return new Int2IntOpenCustomHashMap(STRATEGY);
}
}
@@ -0,0 +1,22 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntMapTest;
import speiger.src.collections.ints.maps.impl.hash.Int2IntOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
public class Int2IntHashMapTest extends BaseInt2IntMapTest
{
@Override
public Int2IntMap createMap(int[] keys, int[] values)
{
return new Int2IntOpenHashMap(keys, values);
}
@Override
public Int2IntMap createEmptyMap()
{
return new Int2IntOpenHashMap();
}
}
@@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.customHash.Int2IntLinkedOpenCustomHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntLinkedOpenCustomHashMapTest extends BaseInt2IntSortedMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.TAIL_MAP, SortedMapTests.HEAD_MAP)); }
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntLinkedOpenCustomHashMap(keys, values, STRATEGY);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntLinkedOpenCustomHashMap(STRATEGY);
}
}
@@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.hash.Int2IntLinkedOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntLinkedOpenHashMapTest extends BaseInt2IntSortedMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.TAIL_MAP, SortedMapTests.HEAD_MAP)); }
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntLinkedOpenHashMap(keys, values);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntLinkedOpenHashMap();
}
}
@@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntNavigableMapTest;
import speiger.src.collections.ints.maps.impl.tree.Int2IntRBTreeMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntRBTreeMapTest extends BaseInt2IntNavigableMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.GET_MOVE, SortedMapTests.MOVE, SortedMapTests.PUT_MOVE)); }
@Override
public Int2IntNavigableMap createMap(int[] keys, int[] values)
{
return new Int2IntRBTreeMap(keys, values);
}
@Override
public Int2IntNavigableMap createEmptyMap()
{
return new Int2IntRBTreeMap();
}
}
@@ -0,0 +1,14 @@
package speiger.src.collections.tests;
public enum MapTests
{
PUT,
PUT_ALL,
CONTAINS,
REPLACE,
COMPUTE,
MERGE,
GET,
ITERATORS,
REMOVE;
}
@@ -0,0 +1,13 @@
package speiger.src.collections.tests;
public enum NavigableMapTests
{
DESENDING,
LOWER,
HIGHER,
CEILING,
FLOOR,
SUB_SET,
HEAD_SET,
TAIL_SET;
}
@@ -2,6 +2,7 @@ package speiger.src.collections.tests;
public enum NavigableSetTest
{
DESENDING,
LOWER,
HIGHER,
CEILING,
@@ -0,0 +1,13 @@
package speiger.src.collections.tests;
public enum SortedMapTests
{
PUT_MOVE,
MOVE,
GET_MOVE,
FIRST,
LAST,
SUB_MAP,
TAIL_MAP,
HEAD_MAP;
}