Next big batch of features.

-Added: Tests for IntSortedSet, IntNavigableSet.
-Added: Test Classes for: Open/Custom/Linked HashSet, TreeSets, ArraySet
-Changed: MemFreeMergeSort got improved by a lot.
-Fixed: Bugs that the tests uncovered.
-Note: TreeSets still have issues. But every other collection type is fixed.
This commit is contained in:
2021-01-08 21:12:20 +01:00
parent 0123cb8937
commit c0c719f2b6
23 changed files with 765 additions and 265 deletions
@@ -26,7 +26,7 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
@Test
public void testAdd() {
if(!getValidCollectionTests().contains(CollectionTest.ADD)) return;
IntCollection collection = create(TEST_EMPTY);
IntCollection collection = create(EMPTY_ARRAY);
Assert.assertTrue(collection.isEmpty());
collection.add(2012);
Assert.assertFalse(collection.isEmpty());
@@ -56,9 +56,9 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
public void testContainsAll() {
if(!getValidCollectionTests().contains(CollectionTest.CONTAINS_ALL)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(create(CONTAINS_ARRAY).containsAll(collection));
Assert.assertFalse(create(ADD_ARRAY).containsAll(collection));
Assert.assertTrue(collection.containsAll(Arrays.asList(IntArrays.wrap(TEST_ARRAY))));
Assert.assertTrue(collection.containsAll(create(CONTAINS_ARRAY)));
Assert.assertFalse(collection.containsAll(create(ADD_ARRAY)));
Assert.assertTrue(collection.containsAll(Arrays.asList(IntArrays.wrap(CONTAINS_ARRAY))));
}
@Test
@@ -129,11 +129,13 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
IntCollection collection = create(TEST_ARRAY);
int[] array = collection.toIntArray();
IntArrays.stableSort(array);
Assert.assertTrue(Arrays.equals(TEST_ARRAY, array));
Assert.assertArrayEquals(array, TEST_ARRAY);
int[] other = collection.toIntArray(new int[collection.size()]);
IntArrays.stableSort(other);
Assert.assertTrue(Arrays.equals(TEST_ARRAY, other));
Assert.assertTrue(Arrays.equals(TEST_ARRAY, IntArrays.unwrap(collection.toArray(new Integer[collection.size()]))));
Assert.assertArrayEquals(other, TEST_ARRAY);
other = IntArrays.unwrap(collection.toArray(new Integer[collection.size()]));
IntArrays.stableSort(other);
Assert.assertArrayEquals(other, TEST_ARRAY);
}
@Test
@@ -148,7 +150,7 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
@Test
public void testWrapper() {
if(!getValidCollectionTests().contains(CollectionTest.WRAPPER)) return;
IntCollection collection = create(TEST_EMPTY);
IntCollection collection = create(TEST_ARRAY);
collection = IntCollections.synchronizedCollection(collection);
Assert.assertTrue(collection instanceof SynchronizedCollection);
collection = IntCollections.unmodifiableCollection(collection);
@@ -12,7 +12,7 @@ import speiger.src.collections.tests.IterableTest;
public abstract class BaseIntIterableTest
{
protected static final int[] TEST_EMPTY = new int[0];
protected static final int[] EMPTY_ARRAY = new int[0];
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
protected abstract IntIterable create(int[] data);
@@ -0,0 +1,81 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.sets.IntNavigableSet;
import speiger.src.collections.tests.NavigableSetTest;
public abstract class BaseIntNavigableSetTest extends BaseIntSortedSetTest
{
@Override
protected abstract IntNavigableSet create(int[] data);
protected EnumSet<NavigableSetTest> getValidNavigableSetTests() { return EnumSet.allOf(NavigableSetTest.class); }
@Test
public void lowerTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.LOWER)) {
Assert.assertTrue(create(TEST_ARRAY).lower(50) < 50);
}
}
@Test
public void higherTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.HIGHER)) {
Assert.assertTrue(create(TEST_ARRAY).higher(50) > 50);
}
}
@Test
public void ceilTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.CEILING)) {
Assert.assertTrue(create(TEST_ARRAY).ceiling(50) >= 50);
}
}
@Test
public void floorTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.FLOOR)) {
Assert.assertTrue(create(TEST_ARRAY).floor(50) <= 50);
}
}
@Test
public void naviSubSetTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.SUB_SET)) {
IntNavigableSet set = create(TEST_ARRAY);
IntNavigableSet subSet = set.subSet(25, 75);
Assert.assertTrue(subSet.lower(50) < 50);
Assert.assertTrue(subSet.higher(50) > 50);
Assert.assertTrue(subSet.ceiling(50) >= 50);
Assert.assertTrue(subSet.floor(50) <= 50);
}
}
@Test
public void naviHeadSetTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.HEAD_SET)) {
IntNavigableSet set = create(TEST_ARRAY);
IntNavigableSet subSet = set.headSet(75);
Assert.assertTrue(subSet.lower(50) < 50);
Assert.assertTrue(subSet.higher(50) > 50);
Assert.assertTrue(subSet.ceiling(50) >= 50);
Assert.assertTrue(subSet.floor(50) <= 50);
}
}
@Test
public void naviTailSetTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.TAIL_SET)) {
IntNavigableSet set = create(TEST_ARRAY);
IntNavigableSet subSet = set.tailSet(25);
Assert.assertTrue(subSet.lower(50) < 50);
Assert.assertTrue(subSet.higher(50) > 50);
Assert.assertTrue(subSet.ceiling(50) >= 50);
Assert.assertTrue(subSet.floor(50) <= 50);
}
}
}
@@ -0,0 +1,90 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.sets.IntSortedSet;
import speiger.src.collections.tests.SortedSetTest;
public abstract class BaseIntSortedSetTest extends BaseIntCollectionTest
{
@Override
protected abstract IntSortedSet create(int[] data);
protected EnumSet<SortedSetTest> getValidSortedSetTests() { return EnumSet.allOf(SortedSetTest.class); }
@Test
public void addMoveTest() {
if(getValidSortedSetTests().contains(SortedSetTest.ADD_MOVE)) {
IntSortedSet set = create(TEST_ARRAY);
Assert.assertTrue(set.addAndMoveToFirst(1050));
Assert.assertFalse(set.addAndMoveToLast(5));
}
}
@Test
public void moveTest() {
if(getValidSortedSetTests().contains(SortedSetTest.MOVE)) {
IntSortedSet set = create(TEST_ARRAY);
Assert.assertTrue(set.moveToFirst(5));
Assert.assertFalse(set.moveToFirst(5));
Assert.assertTrue(set.moveToLast(5));
Assert.assertFalse(set.moveToLast(5));
}
}
@Test
public void peekTest() {
if(getValidSortedSetTests().contains(SortedSetTest.PEEK)) {
IntSortedSet set = create(TEST_ARRAY);
Assert.assertEquals(set.firstInt(), 0);
Assert.assertEquals(set.lastInt(), 99);
}
}
@Test
public void pollTest() {
if(getValidSortedSetTests().contains(SortedSetTest.POLL)) {
IntSortedSet set = create(TEST_ARRAY);
Assert.assertEquals(set.pollFirstInt(), 0);
Assert.assertEquals(set.pollLastInt(), 99);
}
}
@Test
public void subSetTest() {
if(getValidSortedSetTests().contains(SortedSetTest.SUB_SET)) {
IntSortedSet set = create(TEST_ARRAY);
IntSortedSet subSet = set.subSet(25, 75);
Assert.assertTrue(subSet.remove(50));
Assert.assertFalse(subSet.remove(50));
Assert.assertFalse(subSet.contains(20));
Assert.assertFalse(subSet.contains(80));
}
}
@Test
public void headSetTest() {
if(getValidSortedSetTests().contains(SortedSetTest.HEAD_SET)) {
IntSortedSet set = create(TEST_ARRAY);
IntSortedSet subSet = set.headSet(75);
Assert.assertTrue(subSet.remove(50));
Assert.assertFalse(subSet.remove(50));
Assert.assertFalse(subSet.contains(80));
}
}
@Test
public void tailSetTest() {
if(getValidSortedSetTests().contains(SortedSetTest.TAIL_SET)) {
IntSortedSet set = create(TEST_ARRAY);
IntSortedSet subSet = set.tailSet(25);
Assert.assertTrue(subSet.remove(50));
Assert.assertFalse(subSet.remove(50));
Assert.assertFalse(subSet.contains(20));
}
}
}
@@ -0,0 +1,17 @@
package speiger.src.collections.ints.sets;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseIntNavigableSetTest;
import speiger.src.collections.tests.SortedSetTest;
public class IntAVLTreeSetTests extends BaseIntNavigableSetTest
{
@Override
protected IntNavigableSet create(int[] data) {
return new IntAVLTreeSet(data);
}
@Override
protected EnumSet<SortedSetTest> getValidSortedSetTests() { return EnumSet.of(SortedSetTest.PEEK, SortedSetTest.POLL, SortedSetTest.HEAD_SET, SortedSetTest.SUB_SET, SortedSetTest.TAIL_SET);}
}
@@ -0,0 +1,9 @@
package speiger.src.collections.ints.sets;
import speiger.src.collections.ints.base.BaseIntSortedSetTest;
public class IntArraySetTests extends BaseIntSortedSetTest
{
@Override
protected IntSortedSet create(int[] data) { return new IntArraySet(data.clone()); }
}
@@ -0,0 +1,50 @@
package speiger.src.collections.ints.sets;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseIntCollectionTest;
import speiger.src.collections.ints.base.BaseIntSortedSetTest;
import speiger.src.collections.ints.collections.IntCollection;
import speiger.src.collections.ints.utils.IntStrategy;
import speiger.src.collections.tests.SortedSetTest;
public class IntHashSetTests
{
public static abstract class BaseIntOpenHashSetTests extends BaseIntSortedSetTest
{
@Override
protected EnumSet<SortedSetTest> getValidSortedSetTests() { return EnumSet.of(SortedSetTest.ADD_MOVE, SortedSetTest.MOVE, SortedSetTest.PEEK, SortedSetTest.POLL); }
}
public static class IntOpenHashSetTests extends BaseIntCollectionTest
{
@Override
protected IntCollection create(int[] data) { return new IntOpenHashSet(data); }
}
public static class IntLinkedOpenHashSetTests extends BaseIntOpenHashSetTests
{
@Override
protected IntSortedSet create(int[] data) { return new IntLinkedOpenHashSet(data); }
}
public static class IntOpenCustomHashSetTests extends BaseIntCollectionTest
{
@Override
protected IntCollection create(int[] data) { return new IntOpenCustomHashSet(data, new DefaultStrategy()); }
}
public static class IntLinkedOpenCustomHashSetTests extends BaseIntOpenHashSetTests
{
@Override
protected IntSortedSet create(int[] data) { return new IntLinkedOpenCustomHashSet(data, new DefaultStrategy()); }
}
public static class DefaultStrategy implements IntStrategy
{
@Override
public int hashCode(int o) { return Integer.hashCode(o); }
@Override
public boolean equals(int key, int value) { return key == value; }
}
}
@@ -0,0 +1,18 @@
package speiger.src.collections.ints.sets;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseIntNavigableSetTest;
import speiger.src.collections.tests.SortedSetTest;
public class IntRBTreeSetTests extends BaseIntNavigableSetTest
{
@Override
protected IntNavigableSet create(int[] data) {
return new IntRBTreeSet(data);
}
@Override
protected EnumSet<SortedSetTest> getValidSortedSetTests() { return EnumSet.of(SortedSetTest.PEEK, SortedSetTest.POLL, SortedSetTest.HEAD_SET, SortedSetTest.SUB_SET, SortedSetTest.TAIL_SET);}
}
@@ -0,0 +1,29 @@
package speiger.src.collections.ints.utils;
import java.util.Arrays;
import java.util.TreeSet;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
public class JavaTests
{
protected static final Integer[] CONTAINS_ARRAY = new Integer[]{23, 45, 63, 89, 32};
protected static final Integer[] TEST_ARRAY = IntStream.range(0, 100).mapToObj(Integer::valueOf).toArray(Integer[]::new);
private TreeSet<Integer> create(Integer[] array)
{
TreeSet<Integer> tree = new TreeSet<Integer>();
tree.addAll(Arrays.asList(array));
return tree;
}
@Test
public void simpleTest()
{
TreeSet<Integer> collection = create(TEST_ARRAY);
Assert.assertTrue(collection.removeAll(create(CONTAINS_ARRAY)));
Assert.assertFalse(collection.removeAll(create(CONTAINS_ARRAY)));
}
}
@@ -0,0 +1,12 @@
package speiger.src.collections.tests;
public enum NavigableSetTest
{
LOWER,
HIGHER,
CEILING,
FLOOR,
SUB_SET,
HEAD_SET,
TAIL_SET;
}
@@ -0,0 +1,12 @@
package speiger.src.collections.tests;
public enum SortedSetTest
{
ADD_MOVE,
MOVE,
PEEK,
POLL,
SUB_SET,
HEAD_SET,
TAIL_SET;
}