From 75c6784ab6bc530ebb306f7cb7fffa4fdad79628 Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 21 Dec 2020 03:05:41 +0100 Subject: [PATCH] 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 --- .../src/collections/utils/SanityChecks.java | 8 +- .../templates/lists/ArrayList.template | 73 ++++--- .../templates/utils/Arrays.template | 23 +- .../ints/base/BaseIntCollectionTest.java | 157 ++++++++++++++ .../ints/base/BaseIntIterableTest.java | 67 ++++++ .../ints/base/BaseIntListTest.java | 196 ++++++++++++++++++ .../collections/ints/base/IIntArrayTest.java | 40 ++++ .../collections/ints/base/IIntStackTests.java | 29 +++ .../ints/lists/IntArrayListTest.java | 45 ++++ .../collections/ints/utils/SortingTests.java | 110 ++++++++++ .../src/collections/tests/CollectionTest.java | 17 ++ .../src/collections/tests/IterableTest.java | 10 + .../src/collections/tests/ListTest.java | 23 ++ 13 files changed, 762 insertions(+), 36 deletions(-) create mode 100644 src/test/java/speiger/src/collections/ints/base/BaseIntCollectionTest.java create mode 100644 src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java create mode 100644 src/test/java/speiger/src/collections/ints/base/BaseIntListTest.java create mode 100644 src/test/java/speiger/src/collections/ints/base/IIntArrayTest.java create mode 100644 src/test/java/speiger/src/collections/ints/base/IIntStackTests.java create mode 100644 src/test/java/speiger/src/collections/ints/lists/IntArrayListTest.java create mode 100644 src/test/java/speiger/src/collections/ints/utils/SortingTests.java create mode 100644 src/test/java/speiger/src/collections/tests/CollectionTest.java create mode 100644 src/test/java/speiger/src/collections/tests/IterableTest.java create mode 100644 src/test/java/speiger/src/collections/tests/ListTest.java diff --git a/src/main/java/speiger/src/collections/utils/SanityChecks.java b/src/main/java/speiger/src/collections/utils/SanityChecks.java index cf0fa8c..ebab615 100644 --- a/src/main/java/speiger/src/collections/utils/SanityChecks.java +++ b/src/main/java/speiger/src/collections/utils/SanityChecks.java @@ -1,5 +1,6 @@ package speiger.src.collections.utils; +import java.util.Random; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; @@ -11,6 +12,7 @@ public class SanityChecks { public static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private static ForkJoinPool WORK_POOL = ForkJoinPool.commonPool(); + private static ThreadLocal RANDOM = ThreadLocal.withInitial(Random::new); /** * 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) { if(offset < 0) throw new IllegalStateException("Offset is negative ("+offset+")"); 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) { WORK_POOL = pool == null ? ForkJoinPool.commonPool() : pool; } + + public static Random getRandom() { + return RANDOM.get(); + } } diff --git a/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template index 87514c1..77a0389 100644 --- a/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/main/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -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); size+=add; ITERATOR KEY_GENERIC_TYPE iter = c.iterator(); - while(add != 0) data[index++] = iter.NEXT(); + while(add-- != 0) data[index++] = iter.NEXT(); return true; } @@ -256,7 +256,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE grow(size + add); if(index != size) System.arraycopy(data, index, data, index+add, size - index); size+=add; - c.getElements(0, data, index, size); + c.getElements(0, data, index, c.size()); 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) { checkRange(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 data[size] = null; #endif - size--; 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 * @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; int j = 0; for(int i = 0;i filter) { - Objects.requireNonNull(filter); boolean modified = false; int j = 0; for(int i = 0;i= 0) 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 public boolean trim(int size) { if(size > size() || size() == data.length) return false; - int value = Math.min(size, size()); + int value = Math.max(size, size()); #if TYPE_OBJECT data = value == 0 ? (KEY_TYPE[])ARRAYS.EMPTY_ARRAY : Arrays.copyOf(data, value); #else diff --git a/src/main/resources/speiger/assets/collections/templates/utils/Arrays.template b/src/main/resources/speiger/assets/collections/templates/utils/Arrays.template index 05fcaf7..9cfcaeb 100644 --- a/src/main/resources/speiger/assets/collections/templates/utils/Arrays.template +++ b/src/main/resources/speiger/assets/collections/templates/utils/Arrays.template @@ -1,6 +1,7 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Arrays; +import java.util.Random; import java.util.concurrent.RecursiveAction; #if !TYPE_OBJECT @@ -105,6 +106,20 @@ public class ARRAYS } #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, * 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++) { 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(b>c) break; @@ -849,7 +864,7 @@ public class ARRAYS for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;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(b>c) break; @@ -998,7 +1013,7 @@ public class ARRAYS for(;b<=c && (comp = COMPARE_TO(array[b], pivot)) <= 0;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(b>c) break; @@ -1041,7 +1056,7 @@ public class ARRAYS for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;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(b>c) break; diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntCollectionTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntCollectionTest.java new file mode 100644 index 0000000..85f23ef --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntCollectionTest.java @@ -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 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); + } +} \ No newline at end of file diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java new file mode 100644 index 0000000..9f1b527 --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java @@ -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 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(); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntListTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntListTest.java new file mode 100644 index 0000000..0cd4bf9 --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntListTest.java @@ -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 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 99 - T); + for(int i = 0;i= 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); + } +} diff --git a/src/test/java/speiger/src/collections/ints/base/IIntStackTests.java b/src/test/java/speiger/src/collections/ints/base/IIntStackTests.java new file mode 100644 index 0000000..3141d3b --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/base/IIntStackTests.java @@ -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()); + } +} diff --git a/src/test/java/speiger/src/collections/ints/lists/IntArrayListTest.java b/src/test/java/speiger/src/collections/ints/lists/IntArrayListTest.java new file mode 100644 index 0000000..d200295 --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/lists/IntArrayListTest.java @@ -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(); + } +} diff --git a/src/test/java/speiger/src/collections/ints/utils/SortingTests.java b/src/test/java/speiger/src/collections/ints/utils/SortingTests.java new file mode 100644 index 0000000..bd1991d --- /dev/null +++ b/src/test/java/speiger/src/collections/ints/utils/SortingTests.java @@ -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); + } + +} diff --git a/src/test/java/speiger/src/collections/tests/CollectionTest.java b/src/test/java/speiger/src/collections/tests/CollectionTest.java new file mode 100644 index 0000000..e81e52a --- /dev/null +++ b/src/test/java/speiger/src/collections/tests/CollectionTest.java @@ -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; +} diff --git a/src/test/java/speiger/src/collections/tests/IterableTest.java b/src/test/java/speiger/src/collections/tests/IterableTest.java new file mode 100644 index 0000000..99d03ad --- /dev/null +++ b/src/test/java/speiger/src/collections/tests/IterableTest.java @@ -0,0 +1,10 @@ +package speiger.src.collections.tests; + +public enum IterableTest +{ + FOR_EACH, + ITERATOR_FOR_EACH, + ITERATOR_LOOP, + ITERATOR_REMOVAL, + ITERATOR_SKIP; +} diff --git a/src/test/java/speiger/src/collections/tests/ListTest.java b/src/test/java/speiger/src/collections/tests/ListTest.java new file mode 100644 index 0000000..c283c1e --- /dev/null +++ b/src/test/java/speiger/src/collections/tests/ListTest.java @@ -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; +}