New Features.
-Added: List.indexedIterator which allows you to create a iterator with a customized iteration indecies. Useful if you want to transform lists output. -Added: PriorityQueue.contains is now a function -Added: Iterators/Async Builders now support MapToPrimitiveType function on the object variant. So more processing can be done. (Will be expanded upon later versions) -Updated: SimpleCodeGenerator 1.3.0 is now being used which allows for iterative code support.
This commit is contained in:
@@ -1,174 +1,174 @@
|
||||
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;
|
||||
|
||||
import speiger.src.collections.ints.collections.IntIterable;
|
||||
import speiger.src.collections.ints.collections.IntIterator;
|
||||
import speiger.src.collections.ints.lists.IntArrayList;
|
||||
import speiger.src.collections.ints.utils.IntArrays;
|
||||
import speiger.src.collections.ints.utils.IntIterators;
|
||||
import speiger.src.collections.objects.utils.ObjectArrays;
|
||||
import speiger.src.collections.objects.utils.ObjectIterators;
|
||||
import speiger.src.collections.tests.IterableTest;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public abstract class BaseIntIterableTest
|
||||
{
|
||||
protected static final int[] EMPTY_ARRAY = new int[0];
|
||||
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
|
||||
protected static final int[] DISTINCT_ARRAY = IntStream.range(0, 100).flatMap(T -> Arrays.stream(new int[]{T, T})).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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamCount() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_COUNT)) {
|
||||
long expected = IntStream.of(TEST_ARRAY).filter(T -> T % 2 == 0).count();
|
||||
int size = create(TEST_ARRAY).count(T -> T % 2 == 0);
|
||||
Assert.assertEquals(expected, size);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamFilter() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_FILTER)) {
|
||||
int[] expected = IntStream.of(TEST_ARRAY).filter(T -> T % 2 == 0).toArray();
|
||||
int[] actual = IntIterators.pour(create(TEST_ARRAY).filter(T -> T % 2 == 0).iterator()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamFindFirst() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_FIND_FIRST)) {
|
||||
int expected = IntStream.of(TEST_ARRAY).filter(T -> T / 50 > 0).findFirst().getAsInt();
|
||||
int actual = create(TEST_ARRAY).findFirst(T -> T / 50 > 0);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void teastStreamDistinct() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_DISTINCT)) {
|
||||
int[] expected = IntStream.of(DISTINCT_ARRAY).distinct().toArray();
|
||||
int[] actual = IntIterators.pour(create(DISTINCT_ARRAY).distinct().iterator()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamLimit() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_LIMIT)) {
|
||||
int[] expected = IntStream.of(TEST_ARRAY).limit(25).toArray();
|
||||
int[] actual = IntIterators.pour(create(TEST_ARRAY).limit(25).iterator()).toIntArray();
|
||||
Assert.assertEquals(expected.length, actual.length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamMap() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_MAP)) {
|
||||
Integer[] expected = IntStream.of(TEST_ARRAY).mapToObj(Integer::valueOf).toArray(Integer[]::new);
|
||||
Integer[] actual = ObjectIterators.pour(create(TEST_ARRAY).map(Integer::valueOf).iterator()).toArray(Integer[]::new);
|
||||
ObjectArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamMatches() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_MATCHES)) {
|
||||
IntIterable iterable = create(TEST_ARRAY);
|
||||
Assert.assertTrue(iterable.matchesAll(T -> T >= 0));
|
||||
Assert.assertFalse(iterable.matchesAll(T -> T < 0));
|
||||
Assert.assertTrue(iterable.matchesAny(T -> T % 2 != 0));
|
||||
Assert.assertFalse(iterable.matchesAny(T -> T % 2 >= 2));
|
||||
Assert.assertTrue(iterable.matchesNone(T -> T % 2 >= 2));
|
||||
Assert.assertFalse(iterable.matchesNone(T -> T % 2 != 0));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamPeek() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_PEEK)) {
|
||||
int[] peekCount = new int[2];
|
||||
create(TEST_ARRAY).peek(T -> peekCount[0]++).forEach(T -> peekCount[1]++);
|
||||
Assert.assertEquals(TEST_ARRAY.length, peekCount[0]);
|
||||
Assert.assertEquals(TEST_ARRAY.length, peekCount[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamPour() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_POUR)) {
|
||||
int[] expected = TEST_ARRAY;
|
||||
int[] actual = create(TEST_ARRAY).pour(new IntArrayList()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamReduce() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_REDUCE)) {
|
||||
int expected = IntStream.of(TEST_ARRAY).reduce(0, Integer::sum);
|
||||
int actual = create(TEST_ARRAY).reduce(0, Integer::sum);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
import speiger.src.collections.ints.collections.IntIterable;
|
||||
import speiger.src.collections.ints.collections.IntIterator;
|
||||
import speiger.src.collections.ints.lists.IntArrayList;
|
||||
import speiger.src.collections.ints.utils.IntArrays;
|
||||
import speiger.src.collections.ints.utils.IntIterators;
|
||||
import speiger.src.collections.objects.utils.ObjectArrays;
|
||||
import speiger.src.collections.objects.utils.ObjectIterators;
|
||||
import speiger.src.collections.tests.IterableTest;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public abstract class BaseIntIterableTest
|
||||
{
|
||||
protected static final int[] EMPTY_ARRAY = new int[0];
|
||||
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
|
||||
protected static final int[] DISTINCT_ARRAY = IntStream.range(0, 100).flatMap(T -> Arrays.stream(new int[]{T, T})).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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamCount() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_COUNT)) {
|
||||
long expected = IntStream.of(TEST_ARRAY).filter(T -> T % 2 == 0).count();
|
||||
int size = create(TEST_ARRAY).count(T -> T % 2 == 0);
|
||||
Assert.assertEquals(expected, size);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamFilter() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_FILTER)) {
|
||||
int[] expected = IntStream.of(TEST_ARRAY).filter(T -> T % 2 == 0).toArray();
|
||||
int[] actual = IntIterators.pour(create(TEST_ARRAY).filter(T -> T % 2 == 0).iterator()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamFindFirst() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_FIND_FIRST)) {
|
||||
int expected = IntStream.of(TEST_ARRAY).filter(T -> T / 50 > 0).findFirst().getAsInt();
|
||||
int actual = create(TEST_ARRAY).findFirst(T -> T / 50 > 0);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void teastStreamDistinct() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_DISTINCT)) {
|
||||
int[] expected = IntStream.of(DISTINCT_ARRAY).distinct().toArray();
|
||||
int[] actual = IntIterators.pour(create(DISTINCT_ARRAY).distinct().iterator()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamLimit() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_LIMIT)) {
|
||||
int[] expected = IntStream.of(TEST_ARRAY).limit(25).toArray();
|
||||
int[] actual = IntIterators.pour(create(TEST_ARRAY).limit(25).iterator()).toIntArray();
|
||||
Assert.assertEquals(expected.length, actual.length);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamMap() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_MAP)) {
|
||||
Integer[] expected = IntStream.of(TEST_ARRAY).mapToObj(Integer::valueOf).toArray(Integer[]::new);
|
||||
Integer[] actual = ObjectIterators.pour(create(TEST_ARRAY).map(Integer::valueOf).iterator()).toArray(Integer[]::new);
|
||||
ObjectArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamMatches() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_MATCHES)) {
|
||||
IntIterable iterable = create(TEST_ARRAY);
|
||||
Assert.assertTrue(iterable.matchesAll(T -> T >= 0));
|
||||
Assert.assertFalse(iterable.matchesAll(T -> T < 0));
|
||||
Assert.assertTrue(iterable.matchesAny(T -> T % 2 != 0));
|
||||
Assert.assertFalse(iterable.matchesAny(T -> T % 2 >= 2));
|
||||
Assert.assertTrue(iterable.matchesNone(T -> T % 2 >= 2));
|
||||
Assert.assertFalse(iterable.matchesNone(T -> T % 2 != 0));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamPeek() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_PEEK)) {
|
||||
int[] peekCount = new int[2];
|
||||
create(TEST_ARRAY).peek(T -> peekCount[0]++).forEach(T -> peekCount[1]++);
|
||||
Assert.assertEquals(TEST_ARRAY.length, peekCount[0]);
|
||||
Assert.assertEquals(TEST_ARRAY.length, peekCount[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamPour() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_POUR)) {
|
||||
int[] expected = TEST_ARRAY;
|
||||
int[] actual = create(TEST_ARRAY).pour(new IntArrayList()).toIntArray();
|
||||
IntArrays.stableSort(actual);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStreamReduce() {
|
||||
if(getValidIterableTests().contains(IterableTest.STREAM_REDUCE)) {
|
||||
int expected = IntStream.of(TEST_ARRAY).reduce(0, Integer::sum);
|
||||
int actual = create(TEST_ARRAY).reduce(0, Integer::sum);
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,150 +1,162 @@
|
||||
package speiger.src.collections.ints.base;
|
||||
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import speiger.src.collections.ints.queues.IntPriorityQueue;
|
||||
import speiger.src.collections.ints.utils.IntArrays;
|
||||
import speiger.src.collections.tests.IterableTest;
|
||||
import speiger.src.collections.tests.PriorityQueueTest;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
|
||||
{
|
||||
@Override
|
||||
protected abstract IntPriorityQueue create(int[] data);
|
||||
@Override
|
||||
protected EnumSet<IterableTest> getValidIterableTests() { return EnumSet.of(IterableTest.FOR_EACH, IterableTest.ITERATOR_FOR_EACH, IterableTest.ITERATOR_LOOP, IterableTest.ITERATOR_SKIP); }
|
||||
protected EnumSet<PriorityQueueTest> getValidPriorityQueueTests() { return EnumSet.allOf(PriorityQueueTest.class); }
|
||||
protected boolean isUnsortedRead() { return true; }
|
||||
|
||||
@Test
|
||||
public void testEnqueue() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.IN_OUT)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
queue = create(TEST_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeek() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.PEEK)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
if(isUnsortedRead()) {
|
||||
for(int i = 0;i<100;i++) {
|
||||
int value = queue.peek(i);
|
||||
Assert.assertTrue(value >= 0 && value < 100);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.peek(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.REMOVE)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
queue.removeFirst(40);
|
||||
for(int i = 0;i<99;i++) {
|
||||
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
|
||||
else Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
queue.removeLast(40);
|
||||
for(int i = 0;i<99;i++) {
|
||||
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
|
||||
else Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnorderedPeek() {
|
||||
EnumSet<PriorityQueueTest> valid = getValidPriorityQueueTests();
|
||||
if(valid.contains(PriorityQueueTest.IN_OUT) && valid.contains(PriorityQueueTest.PEEK) && !isUnsortedRead()) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<25;i++) {
|
||||
queue.dequeue();
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals((i+25) % 100, queue.peek(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testToArray() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.TO_ARRAY)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
if(isUnsortedRead()) {
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
int[] array = queue.toIntArray();
|
||||
IntArrays.stableSort(array);
|
||||
Assert.assertArrayEquals(TEST_ARRAY, array);
|
||||
int[] data = queue.toIntArray(new int[100]);
|
||||
int[] nonData = queue.toIntArray(new int[0]);
|
||||
IntArrays.stableSort(data);
|
||||
IntArrays.stableSort(nonData);
|
||||
Assert.assertArrayEquals(array, data);
|
||||
Assert.assertArrayEquals(array, nonData);
|
||||
}
|
||||
else {
|
||||
int[] shiftPrimArray = new int[100];
|
||||
for(int i = 0; i < 100; i++) {
|
||||
queue.enqueue(i);
|
||||
shiftPrimArray[(i+80) % 100] = i;
|
||||
}
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray());
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(new int[100]));
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(null));
|
||||
IntPriorityQueue other = create(queue.toIntArray());
|
||||
for(int i = 0;i<20;i++) {
|
||||
other.dequeue();
|
||||
other.enqueue(i);
|
||||
}
|
||||
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray());
|
||||
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray(new int[100]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopy() {
|
||||
if(!getValidPriorityQueueTests().contains(PriorityQueueTest.COPY)) return;
|
||||
IntPriorityQueue queue = create(TEST_ARRAY);
|
||||
IntPriorityQueue copy = queue.copy();
|
||||
Assert.assertFalse(queue == copy);
|
||||
Assert.assertEquals(queue, copy);
|
||||
}
|
||||
}
|
||||
package speiger.src.collections.ints.base;
|
||||
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import speiger.src.collections.ints.queues.IntPriorityQueue;
|
||||
import speiger.src.collections.ints.utils.IntArrays;
|
||||
import speiger.src.collections.tests.IterableTest;
|
||||
import speiger.src.collections.tests.PriorityQueueTest;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public abstract class BaseIntPriorityQueueTest extends BaseIntIterableTest
|
||||
{
|
||||
@Override
|
||||
protected abstract IntPriorityQueue create(int[] data);
|
||||
@Override
|
||||
protected EnumSet<IterableTest> getValidIterableTests() { return EnumSet.of(IterableTest.FOR_EACH, IterableTest.ITERATOR_FOR_EACH, IterableTest.ITERATOR_LOOP, IterableTest.ITERATOR_SKIP); }
|
||||
protected EnumSet<PriorityQueueTest> getValidPriorityQueueTests() { return EnumSet.allOf(PriorityQueueTest.class); }
|
||||
protected boolean isUnsortedRead() { return true; }
|
||||
|
||||
@Test
|
||||
public void testEnqueue() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.IN_OUT)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
queue = create(TEST_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeek() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.PEEK)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
if(isUnsortedRead()) {
|
||||
for(int i = 0;i<100;i++) {
|
||||
int value = queue.peek(i);
|
||||
Assert.assertTrue(value >= 0 && value < 100);
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals(i, queue.peek(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.CONTAINS)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
Assert.assertEquals(true, queue.contains(40));
|
||||
Assert.assertEquals(false, queue.contains(140));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.REMOVE)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
queue.removeFirst(40);
|
||||
for(int i = 0;i<99;i++) {
|
||||
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
|
||||
else Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
queue.removeLast(40);
|
||||
for(int i = 0;i<99;i++) {
|
||||
if(i >= 40) Assert.assertEquals(i + 1, queue.dequeue());
|
||||
else Assert.assertEquals(i, queue.dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnorderedPeek() {
|
||||
EnumSet<PriorityQueueTest> valid = getValidPriorityQueueTests();
|
||||
if(valid.contains(PriorityQueueTest.IN_OUT) && valid.contains(PriorityQueueTest.PEEK) && !isUnsortedRead()) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<25;i++) {
|
||||
queue.dequeue();
|
||||
queue.enqueue(i);
|
||||
}
|
||||
for(int i = 0;i<100;i++) {
|
||||
Assert.assertEquals((i+25) % 100, queue.peek(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testToArray() {
|
||||
if(getValidPriorityQueueTests().contains(PriorityQueueTest.TO_ARRAY)) {
|
||||
IntPriorityQueue queue = create(EMPTY_ARRAY);
|
||||
if(isUnsortedRead()) {
|
||||
for(int i = 0;i<100;i++) {
|
||||
queue.enqueue(i);
|
||||
}
|
||||
int[] array = queue.toIntArray();
|
||||
IntArrays.stableSort(array);
|
||||
Assert.assertArrayEquals(TEST_ARRAY, array);
|
||||
int[] data = queue.toIntArray(new int[100]);
|
||||
int[] nonData = queue.toIntArray(new int[0]);
|
||||
IntArrays.stableSort(data);
|
||||
IntArrays.stableSort(nonData);
|
||||
Assert.assertArrayEquals(array, data);
|
||||
Assert.assertArrayEquals(array, nonData);
|
||||
}
|
||||
else {
|
||||
int[] shiftPrimArray = new int[100];
|
||||
for(int i = 0; i < 100; i++) {
|
||||
queue.enqueue(i);
|
||||
shiftPrimArray[(i+80) % 100] = i;
|
||||
}
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray());
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(new int[100]));
|
||||
Assert.assertArrayEquals(TEST_ARRAY, queue.toIntArray(null));
|
||||
IntPriorityQueue other = create(queue.toIntArray());
|
||||
for(int i = 0;i<20;i++) {
|
||||
other.dequeue();
|
||||
other.enqueue(i);
|
||||
}
|
||||
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray());
|
||||
Assert.assertArrayEquals(shiftPrimArray, other.toIntArray(new int[100]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopy() {
|
||||
if(!getValidPriorityQueueTests().contains(PriorityQueueTest.COPY)) return;
|
||||
IntPriorityQueue queue = create(TEST_ARRAY);
|
||||
IntPriorityQueue copy = queue.copy();
|
||||
Assert.assertFalse(queue == copy);
|
||||
Assert.assertEquals(queue, copy);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package speiger.src.collections.tests;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public enum PriorityQueueTest
|
||||
{
|
||||
IN_OUT,
|
||||
PEEK,
|
||||
REMOVE,
|
||||
TO_ARRAY,
|
||||
COPY;
|
||||
package speiger.src.collections.tests;
|
||||
|
||||
@SuppressWarnings("javadoc")
|
||||
public enum PriorityQueueTest
|
||||
{
|
||||
IN_OUT,
|
||||
PEEK,
|
||||
CONTAINS,
|
||||
REMOVE,
|
||||
TO_ARRAY,
|
||||
COPY;
|
||||
}
|
||||
Reference in New Issue
Block a user