Adding First set of UnitTest

-Fixed: bugs in ArrayList implementation.
-Added: Unit Tests for the following interfaces: Iterable/Collection/List
-Added: ArrayList Test
-Fixed: QuickSort was broken.
-Added: Shuffle method.
-Added: Shared random to SanityChecks
This commit is contained in:
Speiger 2020-12-21 03:05:41 +01:00
parent 787a154042
commit 75c6784ab6
13 changed files with 762 additions and 36 deletions

View File

@ -1,5 +1,6 @@
package speiger.src.collections.utils; package speiger.src.collections.utils;
import java.util.Random;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask; import java.util.concurrent.ForkJoinTask;
@ -11,6 +12,7 @@ public class SanityChecks
{ {
public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool(); private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool();
private static ThreadLocal<Random> RANDOM = ThreadLocal.withInitial(Random::new);
/** /**
* Internal function to cast a Integer to a Byte * Internal function to cast a Integer to a Byte
@ -68,7 +70,7 @@ public class SanityChecks
public static void checkArrayCapacity(int arraySize, int offset, int accessSize) { public static void checkArrayCapacity(int arraySize, int offset, int accessSize) {
if(offset < 0) throw new IllegalStateException("Offset is negative ("+offset+")"); if(offset < 0) throw new IllegalStateException("Offset is negative ("+offset+")");
else if(accessSize < 0) throw new IllegalStateException("Size is negative ("+accessSize+")"); else if(accessSize < 0) throw new IllegalStateException("Size is negative ("+accessSize+")");
else if(arraySize < offset + accessSize) throw new IndexOutOfBoundsException("Index (" + offset + accessSize + ") is not in size (" + arraySize + ")"); else if(arraySize < offset + accessSize) throw new IndexOutOfBoundsException("Index (" + (offset + accessSize) + ") is not in size (" + arraySize + ")");
} }
/** /**
@ -103,4 +105,8 @@ public class SanityChecks
public static void setWorkPool(ForkJoinPool pool) { public static void setWorkPool(ForkJoinPool pool) {
WORK_POOL = pool == null ? ForkJoinPool.commonPool() : pool; WORK_POOL = pool == null ? ForkJoinPool.commonPool() : pool;
} }
public static Random getRandom() {
return RANDOM.get();
}
} }

View File

@ -237,7 +237,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
if(index != size) System.arraycopy(data, index, data, index+add, size - index); if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add; size+=add;
ITERATOR KEY_GENERIC_TYPE iter = c.iterator(); ITERATOR KEY_GENERIC_TYPE iter = c.iterator();
while(add != 0) data[index++] = iter.NEXT(); while(add-- != 0) data[index++] = iter.NEXT();
return true; return true;
} }
@ -256,7 +256,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
grow(size + add); grow(size + add);
if(index != size) System.arraycopy(data, index, data, index+add, size - index); if(index != size) System.arraycopy(data, index, data, index+add, size - index);
size+=add; size+=add;
c.getElements(0, data, index, size); c.getElements(0, data, index, c.size());
return true; return true;
} }
@ -678,14 +678,30 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public KEY_TYPE REMOVE(int index) { public KEY_TYPE REMOVE(int index) {
checkRange(index); checkRange(index);
KEY_TYPE old = data[index]; KEY_TYPE old = data[index];
if(index != size - 1) System.arraycopy(data, index+1, data, index, size - index); size--;
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
#if TYPE_OBJECT #if TYPE_OBJECT
data[size] = null; data[size] = null;
#endif #endif
size--;
return old; return old;
} }
#if !TYPE_OBJECT
/**
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
* It stops after finding the first element
* @param e the element that is searched for
* @return true if the element was found and removed.
*/
@Override
public boolean REMOVE_KEY(KEY_TYPE type) {
int index = indexOf(type);
if(index == -1) return false;
REMOVE(index);
return true;
}
#endif
/** /**
* A Type-Specific pop function to reduce (un)boxing * A Type-Specific pop function to reduce (un)boxing
* @param index the index of the element to fetch * @param index the index of the element to fetch
@ -713,14 +729,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
boolean modified = false; boolean modified = false;
int j = 0; int j = 0;
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(!c.contains(KEY_TO_OBJ(data[i]))) { if(!c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
data[j++] = data[i]; else modified = true;
modified = true;
}
} }
#if TYPE_OBJECT #if TYPE_OBJECT
Arrays.fill(data, j, size, null); Arrays.fill(data, j, size, null);
#endif #endif
size = j;
return modified; return modified;
} }
@ -744,14 +759,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
boolean modified = false; boolean modified = false;
int j = 0; int j = 0;
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(c.contains(KEY_TO_OBJ(data[i]))) { if(c.contains(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
data[j++] = data[i]; else modified = true;
modified = true;
}
} }
#if TYPE_OBJECT #if TYPE_OBJECT
Arrays.fill(data, j, size, null); Arrays.fill(data, j, size, null);
#endif #endif
size = j;
return modified; return modified;
} }
@ -764,19 +778,17 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override @Override
@Primitive @Primitive
public boolean removeIf(Predicate<? super CLASS_TYPE> filter) { public boolean removeIf(Predicate<? super CLASS_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
boolean modified = false; boolean modified = false;
int j = 0; int j = 0;
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(!filter.test(KEY_TO_OBJ(data[i]))) { if(!filter.test(KEY_TO_OBJ(data[i]))) data[j++] = data[i];
data[j++] = data[i]; else modified = true;
modified = true;
}
} }
#if TYPE_OBJECT #if TYPE_OBJECT
Arrays.fill(data, j, size, null); Arrays.fill(data, j, size, null);
#endif #endif
size = j;
return modified; return modified;
} }
@ -792,14 +804,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
boolean modified = false; boolean modified = false;
int j = 0; int j = 0;
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(!c.contains(data[i])) { if(!c.contains(data[i])) data[j++] = data[i];
data[j++] = data[i]; else modified = true;
modified = true;
}
} }
#if TYPE_OBJECT #if TYPE_OBJECT
Arrays.fill(data, j, size, null); Arrays.fill(data, j, size, null);
#endif #endif
size = j;
return modified; return modified;
} }
@ -820,14 +831,13 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
boolean modified = false; boolean modified = false;
int j = 0; int j = 0;
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(c.contains(data[i])) { if(c.contains(data[i])) data[j++] = data[i];
data[j++] = data[i]; else modified = true;
modified = true;
}
} }
#if TYPE_OBJECT #if TYPE_OBJECT
Arrays.fill(data, j, size, null); Arrays.fill(data, j, size, null);
#endif #endif
size = j;
return modified; return modified;
} }
@ -841,12 +851,12 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
public boolean remIf(JAVA_PREDICATE filter) { public boolean remIf(JAVA_PREDICATE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
boolean modified = false; boolean modified = false;
for(int i = 0, j = 0;i<size;i++) { int j = 0;
if(!filter.test(data[i])) { for(int i = 0;i<size;i++) {
data[j++] = data[i]; if(!filter.test(data[i])) data[j++] = data[i];
modified = true; else modified = true;
}
} }
size = j;
return modified; return modified;
} }
@ -909,6 +919,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
data = Arrays.copyOf(data, size); data = Arrays.copyOf(data, size);
else if(size < size() && size >= 0) else if(size < size() && size >= 0)
Arrays.fill(data, size, size(), EMPTY_VALUE); Arrays.fill(data, size, size(), EMPTY_VALUE);
this.size = size;
} }
/** /**
@ -929,7 +940,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override @Override
public boolean trim(int size) { public boolean trim(int size) {
if(size > size() || size() == data.length) return false; if(size > size() || size() == data.length) return false;
int value = Math.min(size, size()); int value = Math.max(size, size());
#if TYPE_OBJECT #if TYPE_OBJECT
data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value); data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value);
#else #else

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.utils; package speiger.src.collections.PACKAGE.utils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.RecursiveAction; import java.util.concurrent.RecursiveAction;
#if !TYPE_OBJECT #if !TYPE_OBJECT
@ -105,6 +106,20 @@ public class ARRAYS
} }
#endif #endif
public static GENERIC_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array) {
return shuffle(array, SanityChecks.getRandom());
}
public static GENERIC_BRACES KEY_TYPE[] shuffle(KEY_TYPE[] array, Random random) {
for(int i = array.length-1; i>=0;i--) {
int p = random.nextInt(i + 1);
KEY_TYPE t = array[i];
array[i] = array[p];
array[p] = t;
}
return array;
}
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator, * Sorts the specified range of elements according to the order induced by the specified comparator,
* potentially dynamically choosing an appropriate algorithm given the type and size of the array. * potentially dynamically choosing an appropriate algorithm given the type and size of the array.
@ -796,7 +811,7 @@ public class ARRAYS
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) { for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b); if(compare == 0) swap(array, a++, b);
} }
for(;b>=c && (compare = comp.compare(array[c], pivot)) >= 0;c--) { for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--); if(compare == 0) swap(array, c, d--);
} }
if(b>c) break; if(b>c) break;
@ -849,7 +864,7 @@ public class ARRAYS
for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) { for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) {
if(comp == 0) swap(array, a++, b); if(comp == 0) swap(array, a++, b);
} }
for(;b>=c && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) { for(;c>=b && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
if(comp == 0) swap(array, c, d--); if(comp == 0) swap(array, c, d--);
} }
if(b>c) break; if(b>c) break;
@ -998,7 +1013,7 @@ public class ARRAYS
for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) { for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;b++) {
if(comp == 0) swap(array, a++, b); if(comp == 0) swap(array, a++, b);
} }
for(;b>=c && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) { for(;c>=b && (comp = COMPARE_TO(array[c], pivot)) >= 0;c--) {
if(comp == 0) swap(array, c, d--); if(comp == 0) swap(array, c, d--);
} }
if(b>c) break; if(b>c) break;
@ -1041,7 +1056,7 @@ public class ARRAYS
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) { for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b); if(compare == 0) swap(array, a++, b);
} }
for(;b>=c && (compare = comp.compare(array[c], pivot)) >= 0;c--) { for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--); if(compare == 0) swap(array, c, d--);
} }
if(b>c) break; if(b>c) break;

View File

@ -0,0 +1,157 @@
package speiger.src.collections.ints.base;
import java.util.Arrays;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.collections.IntCollection;
import speiger.src.collections.ints.utils.IntArrays;
import speiger.src.collections.ints.utils.IntCollections;
import speiger.src.collections.ints.utils.IntCollections.SynchronizedCollection;
import speiger.src.collections.ints.utils.IntCollections.UnmodifiableCollection;
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[] CONTAINS_ARRAY = new int[]{23, 45, 63, 89, 32};
@Override
protected abstract IntCollection create(int[] data);
protected EnumSet<CollectionTest> getValidCollectionTests() { return EnumSet.allOf(CollectionTest.class); }
@Test
public void testAdd() {
if(!getValidCollectionTests().contains(CollectionTest.ADD)) return;
IntCollection collection = create(TEST_EMPTY);
Assert.assertTrue(collection.isEmpty());
collection.add(2012);
Assert.assertFalse(collection.isEmpty());
Assert.assertTrue(collection.contains(2012));
}
@Test
public void testAddAll() {
if(!getValidCollectionTests().contains(CollectionTest.ADD_ALL)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + ADD_ARRAY.length, collection.size());
}
@Test
@SuppressWarnings("deprecation")
public void testContains() {
if(!getValidCollectionTests().contains(CollectionTest.CONTAINS)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(collection.contains(50));
Assert.assertFalse(collection.contains(5120));
Assert.assertTrue(collection.contains(Integer.valueOf(50)));
}
@Test
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))));
}
@Test
@SuppressWarnings("deprecation")
public void testContainsAny() {
if(!getValidCollectionTests().contains(CollectionTest.CONTAINS_ANY)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(collection.containsAny(create(CONTAINS_ARRAY)));
Assert.assertFalse(collection.containsAny(create(ADD_ARRAY)));
Assert.assertTrue(collection.containsAny(Arrays.asList(IntArrays.wrap(CONTAINS_ARRAY))));
}
@Test
@SuppressWarnings("deprecation")
public void testRemove() {
if(!getValidCollectionTests().contains(CollectionTest.REMOVE)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(collection.remInt(50));
Assert.assertFalse(collection.remInt(50));
Assert.assertTrue(collection.remove(Integer.valueOf(75)));
}
@Test
@SuppressWarnings("deprecation")
public void testRemoveIf() {
if(!getValidCollectionTests().contains(CollectionTest.REMOVE_IF)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(collection.remIf(T -> T != 25));
Assert.assertFalse(collection.remIf(T -> T != 25));
Assert.assertEquals(1, collection.size());
collection = create(TEST_ARRAY);
Assert.assertTrue(collection.removeIf(T -> T.intValue() != 25));
Assert.assertFalse(collection.removeIf(T -> T.intValue() != 25));
Assert.assertEquals(1, collection.size());
}
@Test
public void testRemoveAll() {
if(!getValidCollectionTests().contains(CollectionTest.REMOVE_ALL)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertTrue(collection.removeAll(create(CONTAINS_ARRAY)));
Assert.assertFalse(collection.removeAll(create(CONTAINS_ARRAY)));
collection = create(TEST_ARRAY);
Assert.assertFalse(collection.removeAll(Arrays.asList(IntArrays.wrap(ADD_ARRAY))));
Assert.assertTrue(collection.removeAll(Arrays.asList(IntArrays.wrap(CONTAINS_ARRAY))));
}
@Test
public void testRetainAll() {
if(!getValidCollectionTests().contains(CollectionTest.RETAIN_ALL)) return;
IntCollection collection = create(TEST_ARRAY);
IntCollection retained = create(CONTAINS_ARRAY);
Assert.assertTrue(collection.retainAll(retained));
Assert.assertFalse(collection.retainAll(retained));
Assert.assertEquals(CONTAINS_ARRAY.length, collection.size());
int[] retainedArray = retained.toIntArray();
int[] collectionArray = collection.toIntArray();
IntArrays.stableSort(retainedArray);
IntArrays.stableSort(collectionArray);
Assert.assertArrayEquals(retainedArray, collectionArray);
collection.retainAll(IntCollections.EMPTY);
Assert.assertTrue(collection.isEmpty());
}
@Test
public void testToArray() {
if(!getValidCollectionTests().contains(CollectionTest.TO_ARRAY)) return;
IntCollection collection = create(TEST_ARRAY);
int[] array = collection.toIntArray();
IntArrays.stableSort(array);
Assert.assertTrue(Arrays.equals(TEST_ARRAY, 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()]))));
}
@Test
public void testClear() {
if(!getValidCollectionTests().contains(CollectionTest.CLEAR)) return;
IntCollection collection = create(TEST_ARRAY);
Assert.assertFalse(collection.isEmpty());
collection.clear();
Assert.assertTrue(collection.isEmpty());
}
@Test
public void testWrapper() {
if(!getValidCollectionTests().contains(CollectionTest.WRAPPER)) return;
IntCollection collection = create(TEST_EMPTY);
collection = IntCollections.synchronizedCollection(collection);
Assert.assertTrue(collection instanceof SynchronizedCollection);
collection = IntCollections.unmodifiableCollection(collection);
Assert.assertTrue(collection instanceof UnmodifiableCollection);
}
}

View File

@ -0,0 +1,67 @@
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.collections.IntIterable;
import speiger.src.collections.ints.collections.IntIterator;
import speiger.src.collections.tests.IterableTest;
public abstract class BaseIntIterableTest
{
protected static final int[] TEST_EMPTY = new int[0];
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
protected abstract IntIterable create(int[] data);
protected EnumSet<IterableTest> getValidIterableTests() { return EnumSet.allOf(IterableTest.class); }
@Test
public void testForEach() {
if(getValidIterableTests().contains(IterableTest.FOR_EACH)) {
create(TEST_ARRAY).forEach(T -> Assert.assertTrue(T >= 0 && T < 100));
}
}
@Test
public void testIteratorForEach() {
if(getValidIterableTests().contains(IterableTest.ITERATOR_FOR_EACH)) {
create(TEST_ARRAY).iterator().forEachRemaining(T -> Assert.assertTrue(T >= 0 && T < 100));
}
}
@Test
public void testSkip() {
if(getValidIterableTests().contains(IterableTest.ITERATOR_SKIP)) {
IntIterator iter = create(TEST_ARRAY).iterator();
Assert.assertEquals(50, iter.skip(50));
Assert.assertNotEquals(100, iter.skip(100));
}
}
@Test
public void testIteratorLoop() {
if(getValidIterableTests().contains(IterableTest.ITERATOR_LOOP)) {
for(int entry : create(TEST_ARRAY)) Assert.assertTrue(entry >= 0 && entry < 100);
for(IntIterator iter = create(TEST_ARRAY).iterator();iter.hasNext();) {
int entry = iter.nextInt();
Assert.assertTrue(entry >= 0 && entry < 100);
}
}
}
@Test
public void testIteratorRemovalLoop() {
if(getValidIterableTests().contains(IterableTest.ITERATOR_REMOVAL)) {
for(IntIterator iter = create(TEST_ARRAY).iterator();iter.hasNext();) {
int entry = iter.nextInt();
Assert.assertTrue(entry >= 0 && entry < 100);
iter.remove();
}
}
}
}

View File

@ -0,0 +1,196 @@
package speiger.src.collections.ints.base;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.lists.IntList;
import speiger.src.collections.ints.lists.IntListIterator;
import speiger.src.collections.ints.utils.IntCollections;
import speiger.src.collections.tests.ListTest;
public abstract class BaseIntListTest extends BaseIntCollectionTest
{
@Override
protected abstract IntList create(int[] data);
protected EnumSet<ListTest> getValidListTests() { return EnumSet.allOf(ListTest.class); }
@Test
public void testAddIndex() {
if(getValidListTests().contains(ListTest.ADD_INDEX)) return;
IntList list = create(TEST_ARRAY);
list.add(50, -10);
Assert.assertEquals(TEST_ARRAY.length+1, list.size());
Assert.assertEquals(-10, list.getInt(50));
}
@Test
public void testAddList() {
if(getValidListTests().contains(ListTest.ADD_LIST)) return;
IntList list = create(TEST_ARRAY);
IntList adding = create(ADD_ARRAY);
list.addAll(adding);
for(int i = 0;i<ADD_ARRAY.length;i++) Assert.assertEquals(ADD_ARRAY[i], list.getInt(TEST_ARRAY.length + i));
}
@Test
public void testAddIndexCollection() {
if(getValidListTests().contains(ListTest.ADD_INDEX_COLLECTION)) return;
IntList list = create(TEST_ARRAY);
IntList adding = create(ADD_ARRAY);
list.addAll(0, IntCollections.unmodifiableCollection(adding));
for(int i = 0;i<ADD_ARRAY.length;i++) Assert.assertEquals(ADD_ARRAY[i], list.getInt(i));
}
@Test
public void testAddIndexList() {
if(getValidListTests().contains(ListTest.ADD_INDEX_LIST)) return;
IntList list = create(TEST_ARRAY);
IntList adding = create(ADD_ARRAY);
list.addAll(adding);
for(int i = 0;i<ADD_ARRAY.length;i++) Assert.assertEquals(ADD_ARRAY[i], list.getInt(TEST_ARRAY.length + i));
}
@Test
public void testAddElements() {
if(getValidListTests().contains(ListTest.ADD_ELEMENTS)) return;
IntList list = create(TEST_ARRAY);
IntList adding = create(ADD_ARRAY);
list.addAll(0, adding);
for(int i = 0;i<ADD_ARRAY.length;i++) Assert.assertEquals(ADD_ARRAY[i], list.getInt(i));
}
@Test
@SuppressWarnings("deprecation")
public void testIndex() {
if(getValidListTests().contains(ListTest.INDEX)) return;
IntList list = create(TEST_ARRAY);
Assert.assertEquals(50, list.indexOf(50));
Assert.assertEquals(25, list.indexOf(25));
Assert.assertEquals(75, list.indexOf(75));
Assert.assertEquals(50, list.indexOf(Integer.valueOf(50)));
Assert.assertEquals(25, list.indexOf(Integer.valueOf(25)));
Assert.assertEquals(75, list.indexOf(Integer.valueOf(75)));
}
@Test
@SuppressWarnings("deprecation")
public void testLastIndex() {
if(getValidListTests().contains(ListTest.LAST_INDEX)) return;
IntList list = create(TEST_ARRAY);
Assert.assertEquals(50, list.lastIndexOf(50));
Assert.assertEquals(25, list.lastIndexOf(25));
Assert.assertEquals(75, list.lastIndexOf(75));
Assert.assertEquals(50, list.lastIndexOf(Integer.valueOf(50)));
Assert.assertEquals(25, list.lastIndexOf(Integer.valueOf(25)));
Assert.assertEquals(75, list.lastIndexOf(Integer.valueOf(75)));
}
@Test
public void testGet() {
if(getValidListTests().contains(ListTest.GET)) return;
IntList list = create(TEST_ARRAY);
for(int i = 0;i<list.size();i++) Assert.assertEquals(TEST_ARRAY[i], list.getInt(i));
}
@Test
public void testGetElements() {
if(getValidListTests().contains(ListTest.GET_ELEMENTS)) return;
int[] elements = create(TEST_ARRAY).extractElements(25, 75);
for(int i = 0;i<elements.length;i++) Assert.assertEquals(TEST_ARRAY[i+25], elements[i]);
}
@Test
public void testSet() {
if(getValidListTests().contains(ListTest.SET)) return;
IntList list = create(TEST_ARRAY);
Assert.assertEquals(25, list.set(25, -124));
Assert.assertEquals(-124, list.getInt(25));
}
@Test
public void testReSize() {
if(getValidListTests().contains(ListTest.RE_SIZE)) return;
IntList list = create(TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, list.size());
list.size(25);
Assert.assertEquals(25, list.size());
}
@Test
public void testSort() {
if(getValidListTests().contains(ListTest.SORT)) return;
IntList list = create(TEST_ARRAY);
Collections.shuffle(list);
Assert.assertFalse(Arrays.equals(TEST_ARRAY, list.toIntArray()));
list.sort(null);
Assert.assertArrayEquals(TEST_ARRAY, list.toIntArray());
Collections.shuffle(list);
Assert.assertFalse(Arrays.equals(TEST_ARRAY, list.toIntArray()));
list.unstableSort(null);
Assert.assertArrayEquals(TEST_ARRAY, list.toIntArray());
}
@Test
public void testReplace() {
if(getValidListTests().contains(ListTest.REPLACE)) return;
IntList list = create(TEST_ARRAY);
for(int i = 0;i<list.size();i++) Assert.assertEquals(TEST_ARRAY[i], list.getInt(i));
list.replaceInts(T -> 99 - T);
for(int i = 0;i<list.size();i++) Assert.assertEquals(TEST_ARRAY[TEST_ARRAY.length - i - 1], list.getInt(i));
}
@Test
public void testListRemove() {
if(getValidListTests().contains(ListTest.REMOVE)) return;
IntList list = create(TEST_ARRAY);
list.remInt(50);
Assert.assertEquals(TEST_ARRAY.length - 1, list.size());
Assert.assertFalse(list.contains(50));
}
@Test
public void testRemoveElements() {
if(getValidListTests().contains(ListTest.REMOVE_ELEMENTS)) return;
IntList list = create(TEST_ARRAY);
list.removeElements(25, 75);
Assert.assertEquals(TEST_ARRAY.length - 50, list.size());
for(int i = 0;i<50;i++) Assert.assertEquals(TEST_ARRAY[i + (i < 25 ? 0 : 50)], list.getInt(i));
}
@Test
public void testExtractElements() {
if(getValidListTests().contains(ListTest.EXTRACT_ELEMENTS)) return;
IntList list = create(TEST_ARRAY);
int[] elements = list.extractElements(25, 75);
for(int i = 0;i<elements.length;i++) {
Assert.assertEquals(TEST_ARRAY[i + 25], elements[i]);
Assert.assertEquals(TEST_ARRAY[i + (i < 25 ? 0 : 50)], list.getInt(i));
}
}
@Test
public void testListIterator() {
if(getValidListTests().contains(ListTest.LIST_ITERATOR)) return;
IntList list = create(TEST_ARRAY);
IntListIterator iter = list.listIterator(50);
iter.nextInt();
iter.set(-250);
iter.nextInt();
iter.add(50);
Assert.assertEquals(-250, list.getInt(50));
Assert.assertEquals(50, list.getInt(51));
}
@Test
public void testSubList() {
if(getValidListTests().contains(ListTest.SUB_LIST)) return;
IntList list = create(TEST_ARRAY);
IntList subList = list.subList(25, 75);
Assert.assertEquals(50, subList.size());
for(int i = 0;i<subList.size();i++) Assert.assertEquals(TEST_ARRAY[i + 25], subList.getInt(i));
}
}

View File

@ -0,0 +1,40 @@
package speiger.src.collections.ints.base;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.utils.IIntArray;
public interface IIntArrayTest
{
static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
public IIntArray create(int[] data);
@Test
public default void testEnsureCapacity()
{
IIntArray array = create(TEST_ARRAY);
array.ensureCapacity(2500);
Assert.assertTrue(array.elements().length >= 2500);
}
@Test
public default void testElements()
{
IIntArray array = create(TEST_ARRAY);
Assert.assertArrayEquals(TEST_ARRAY, array.elements());
array.elements(T -> Assert.assertArrayEquals(TEST_ARRAY, T));
}
@Test
public default void testTrim()
{
IIntArray array = create(TEST_ARRAY);
array.ensureCapacity(2500);
Assert.assertNotEquals(TEST_ARRAY.length, array.elements().length);
array.trim();
Assert.assertEquals(TEST_ARRAY.length, array.elements().length);
}
}

View File

@ -0,0 +1,29 @@
package speiger.src.collections.ints.base;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.collections.IntStack;
public interface IIntStackTests
{
static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
public IntStack create(int[] data);
@Test
public default void testPush()
{
IntStack stacks = create(TEST_ARRAY);
stacks.pushInt(500);
Assert.assertEquals(500, stacks.topInt());
}
@Test
public default void testPop()
{
Assert.assertEquals(99, create(TEST_ARRAY).topInt());
}
}

View File

@ -0,0 +1,45 @@
package speiger.src.collections.ints.lists;
import org.junit.Test;
import speiger.src.collections.ints.base.BaseIntListTest;
import speiger.src.collections.ints.base.IIntArrayTest;
import speiger.src.collections.ints.base.IIntStackTests;
public class IntArrayListTest extends BaseIntListTest implements IIntStackTests, IIntArrayTest
{
@Override
public IntArrayList create(int[] data) {
return new IntArrayList(data);
}
@Test
@Override
public void testPush() {
IIntStackTests.super.testPush();
}
@Test
@Override
public void testPop() {
IIntStackTests.super.testPop();
}
@Test
@Override
public void testEnsureCapacity() {
IIntArrayTest.super.testEnsureCapacity();
}
@Test
@Override
public void testElements() {
IIntArrayTest.super.testElements();
}
@Test
@Override
public void testTrim() {
IIntArrayTest.super.testTrim();
}
}

View File

@ -0,0 +1,110 @@
package speiger.src.collections.ints.utils;
import java.util.Arrays;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
public class SortingTests
{
public static final int[] SMALL_TEST = IntStream.range(0, 5000).toArray();
public static final int[] LARGE_TEST = IntStream.range(0, 50000).toArray();
@Test
public void testInsertionSort() {
int[] array = IntArrays.shuffle(SMALL_TEST.clone());
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.insertionSort(array);
Assert.assertArrayEquals(SMALL_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.insertionSort(array, Integer::compare);
Assert.assertArrayEquals(SMALL_TEST, array);
}
@Test
public void testMergeSort() {
int[] array = IntArrays.shuffle(SMALL_TEST.clone());
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.mergeSort(array);
Assert.assertArrayEquals(SMALL_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.mergeSort(array, Integer::compare);
Assert.assertArrayEquals(SMALL_TEST, array);
}
@Test
public void testMemFreeMergeSort() {
int[] array = IntArrays.shuffle(SMALL_TEST.clone());
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.memFreeMergeSort(array);
Assert.assertArrayEquals(SMALL_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.memFreeMergeSort(array, Integer::compare);
Assert.assertArrayEquals(SMALL_TEST, array);
}
@Test
public void testSelectionSortSort() {
int[] array = IntArrays.shuffle(SMALL_TEST.clone());
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.selectionSort(array);
Assert.assertArrayEquals(SMALL_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.selectionSort(array, Integer::compare);
Assert.assertArrayEquals(SMALL_TEST, array);
}
@Test
public void testQuickSortSort() {
int[] array = IntArrays.shuffle(SMALL_TEST.clone());
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.quickSort(array);
Assert.assertArrayEquals(SMALL_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, SMALL_TEST));
IntArrays.quickSort(array, Integer::compare);
Assert.assertArrayEquals(SMALL_TEST, array);
}
@Test
public void testParallelMergeSort() {
int[] array = IntArrays.shuffle(LARGE_TEST.clone());
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelMergeSort(array);
Assert.assertArrayEquals(LARGE_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelMergeSort(array, Integer::compare);
Assert.assertArrayEquals(LARGE_TEST, array);
}
@Test
public void testParallelMemFreeMergeSort() {
int[] array = IntArrays.shuffle(LARGE_TEST.clone());
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelMemFreeMergeSort(array);
Assert.assertArrayEquals(LARGE_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelMemFreeMergeSort(array, Integer::compare);
Assert.assertArrayEquals(LARGE_TEST, array);
}
@Test
public void testParallelQuickSort() {
int[] array = IntArrays.shuffle(LARGE_TEST.clone());
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelQuickSort(array);
Assert.assertArrayEquals(LARGE_TEST, array);
IntArrays.shuffle(array);
Assert.assertFalse(Arrays.equals(array, LARGE_TEST));
IntArrays.parallelQuickSort(array, Integer::compare);
Assert.assertArrayEquals(LARGE_TEST, array);
}
}

View File

@ -0,0 +1,17 @@
package speiger.src.collections.tests;
public enum CollectionTest
{
ADD,
ADD_ALL,
CONTAINS,
CONTAINS_ALL,
CONTAINS_ANY,
REMOVE,
REMOVE_IF,
REMOVE_ALL,
RETAIN_ALL,
TO_ARRAY,
CLEAR,
WRAPPER;
}

View File

@ -0,0 +1,10 @@
package speiger.src.collections.tests;
public enum IterableTest
{
FOR_EACH,
ITERATOR_FOR_EACH,
ITERATOR_LOOP,
ITERATOR_REMOVAL,
ITERATOR_SKIP;
}

View File

@ -0,0 +1,23 @@
package speiger.src.collections.tests;
public enum ListTest
{
ADD_INDEX,
ADD_LIST,
ADD_INDEX_COLLECTION,
ADD_INDEX_LIST,
ADD_ELEMENTS,
INDEX,
LAST_INDEX,
GET,
GET_ELEMENTS,
SET,
RE_SIZE,
SORT,
REPLACE,
REMOVE,
REMOVE_ELEMENTS,
EXTRACT_ELEMENTS,
LIST_ITERATOR,
SUB_LIST;
}