From c6afdb8762eb45d9146c52f383137a899ceeee27 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 10 Dec 2021 03:23:28 +0100 Subject: [PATCH] New Tests & Fixes - Added: Tests for the new Stream replace functions to ensure no bugs are left. - Fixed: Custom HashSet reduce function with a default value was checking incorrectly for present keys. --- Changelog.md | 2 + build.gradle | 2 +- .../templates/lists/ArrayList.template | 5 + .../maps/impl/misc/ArrayMap.template | 1 + .../templates/maps/interfaces/Map.template | 40 +++---- .../templates/sets/OpenCustomHashSet.template | 2 +- .../ints/base/BaseIntIterableTest.java | 106 ++++++++++++++++++ .../src/collections/tests/IterableTest.java | 12 +- 8 files changed, 147 insertions(+), 23 deletions(-) diff --git a/Changelog.md b/Changelog.md index ee39165c..47f40b81 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,8 @@ - Added: Unmodifiable and Synchronized Wrapper Collections can now be cloned. They clone the underlying map which doesn't break functionality. (Had a usecase for it) - Added: A boxed putAll array variant. - Fixed: EnumMaps didn't keep track of their size and now got proper care and implementations as needed. There might be more work required but at least the core functionality is now up to date. +- Added: Tests for the new Stream replace functions to ensure no bugs are left. +- Fixed: Custom HashSet reduce function with a default value was checking incorrectly for present keys. ### Version 0.4.5 - Added: removeAll/retainAll(Collection c, Consumer r) which receives all the elements that got deleted from the collection diff --git a/build.gradle b/build.gradle index 0570b827..2e2cb66e 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { } archivesBaseName = 'Primitive Collections' -version = '0.4.5'; +version = '0.4.6'; sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index 17c533af..1c03df26 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -1023,7 +1023,12 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE public E[] toArray(E[] a) { if(a == null) a = (E[])new Object[size]; else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size); +#if TYPE_OBJECT System.arraycopy(data, 0, a, 0, size); +#else + for(int i = 0;i=0;i--) { - if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; state = operator.APPLY_VALUE(state, keys[i]); } return state; diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java index d9a2e2e3..e29bf9c4 100644 --- a/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntIterableTest.java @@ -1,5 +1,6 @@ package speiger.src.collections.ints.base; +import java.util.Arrays; import java.util.EnumSet; import java.util.stream.IntStream; @@ -8,6 +9,11 @@ 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") @@ -15,6 +21,7 @@ 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); @@ -65,4 +72,103 @@ public abstract class BaseIntIterableTest } } } + + @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::new).toArray(Integer[]::new); + Integer[] actual = ObjectIterators.pour(create(TEST_ARRAY).map(Integer::new).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); + } + } } \ No newline at end of file diff --git a/src/test/java/speiger/src/collections/tests/IterableTest.java b/src/test/java/speiger/src/collections/tests/IterableTest.java index b4d19a10..305f436f 100644 --- a/src/test/java/speiger/src/collections/tests/IterableTest.java +++ b/src/test/java/speiger/src/collections/tests/IterableTest.java @@ -7,5 +7,15 @@ public enum IterableTest ITERATOR_FOR_EACH, ITERATOR_LOOP, ITERATOR_REMOVAL, - ITERATOR_SKIP; + ITERATOR_SKIP, + STREAM_COUNT, + STREAM_FILTER, + STREAM_FIND_FIRST, + STREAM_DISTINCT, + STREAM_LIMIT, + STREAM_MAP, + STREAM_MATCHES, + STREAM_PEEK, + STREAM_POUR, + STREAM_REDUCE; }