This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
commit 2a6ec0fca0ae2098bf48306d98575a8ebee6ad9c Author: Alex Herbert <[email protected]> AuthorDate: Fri Sep 9 22:55:14 2022 +0100 Simplify random indices tests Use streams to generate arrays. Use BitSet to create a unique set of indices. --- .../bloomfilter/AbstractBloomFilterTest.java | 14 +++--- .../bloomfilter/DefaultBitMapProducerTest.java | 11 ++--- .../bloomfilter/DefaultIndexProducerTest.java | 50 ++++++++-------------- 3 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java index 91eb763af..cc196a00a 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/AbstractBloomFilterTest.java @@ -24,8 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; +import java.util.BitSet; import java.util.List; -import java.util.SortedSet; import org.junit.jupiter.api.Test; @@ -121,7 +121,7 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> { @Test public void testMergeWithHasher() { - for (int i=0; i<5000; i++) { + for (int i = 0; i < 5; i++) { final BloomFilter f = createEmptyFilter(getTestShape()); int[] expected = DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits()); Hasher hasher = new ArrayHasher(expected); @@ -133,7 +133,7 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> { @Test public void testMergeWithBitMapProducer() { - for (int i=0; i<5000; i++) { + for (int i = 0; i < 5; i++) { long[] values = new long[2]; for (int idx : DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits())) { BitMap.set(values, idx); @@ -161,12 +161,14 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> { @Test public void testMergeWithIndexProducer() { - for (int i=0; i<5000; i++) { + for (int i = 0; i < 5; i++) { int[] values = DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits()); BloomFilter f = createFilter(getTestShape(), IndexProducer.fromIndexArray(values)); - SortedSet<Integer> uniqueValues = DefaultIndexProducerTest.uniqueSet(values); + BitSet uniqueValues = DefaultIndexProducerTest.uniqueSet(values); assertTrue(f.forEachIndex(idx -> { - return uniqueValues.remove(Integer.valueOf(idx)); + final boolean result = uniqueValues.get(idx); + uniqueValues.clear(idx); + return result; })); assertTrue(uniqueValues.isEmpty()); } diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitMapProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitMapProducerTest.java index c14f15212..8a0372258 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitMapProducerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultBitMapProducerTest.java @@ -19,7 +19,7 @@ package org.apache.commons.collections4.bloomfilter; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.LongPredicate; import org.junit.jupiter.api.Test; @@ -66,13 +66,8 @@ public class DefaultBitMapProducerTest extends AbstractBitMapProducerTest { * @param size the number of values to generate * @return the array of random values. */ - public static long[] generateLongArray( int size ) { - Random rnd = new Random(); - long[] expected = new long[size]; - for (int i=0; i<size; i++) { - expected[i] = rnd.nextLong(); - } - return expected; + public static long[] generateLongArray(int size) { + return ThreadLocalRandom.current().longs(size).toArray(); } @Test diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java index a36f545d0..b4558ab9b 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/DefaultIndexProducerTest.java @@ -18,21 +18,20 @@ package org.apache.commons.collections4.bloomfilter; import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import java.util.Random; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; +import java.util.Arrays; +import java.util.BitSet; +import java.util.concurrent.ThreadLocalRandom; import java.util.function.IntPredicate; import org.junit.jupiter.api.Test; public class DefaultIndexProducerTest extends AbstractIndexProducerTest { - private int[] values = generateIntArray( 10, 512 ); + private int[] values = generateIntArray(10, 512); @Override protected IndexProducer createProducer() { - return IndexProducer.fromIndexArray( values ); + return IndexProducer.fromIndexArray(values); } @Override @@ -52,26 +51,19 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest { * @param bound the upper bound (exclusive) of the values in the array. * @return an array of int. */ - public static int[] generateIntArray( int size, int bound ) { - Random rnd = new Random(); - int[] expected = new int[size]; - for (int i=0; i<size; i++) { - expected[i] = rnd.nextInt(bound); - } - return expected; + public static int[] generateIntArray(int size, int bound) { + return ThreadLocalRandom.current().ints(size, 0, bound).toArray(); } /** - * Creates a sorted set of Integers. - * @param ary the array to sort and make unique - * @return the sorted Set. + * Creates a BitSet of indices. + * @param ary the array + * @return the set. */ - public static SortedSet<Integer> uniqueSet(int[] ary) { - SortedSet<Integer> uniq = new TreeSet<Integer>(); - for (int idx : ary) { - uniq.add(idx); - } - return uniq; + public static BitSet uniqueSet(int[] ary) { + final BitSet bs = new BitSet(); + Arrays.stream(ary).forEach(bs::set); + return bs; } /** @@ -80,19 +72,13 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest { * @return the sorted unique array. */ public static int[] unique(int[] ary) { - Set<Integer> uniq = uniqueSet(ary); - int[] result = new int[uniq.size()]; - int i=0; - for (int idx : uniq) { - result[i++] = idx; - } - return result; + return Arrays.stream(ary).distinct().sorted().toArray(); } @Test public void testFromBitMapProducer() { - for (int i=0; i<5000; i++) { - int[] expected = generateIntArray( 7, 256 ); + for (int i = 0; i < 5; i++) { + int[] expected = generateIntArray(7, 256); long[] bits = new long[BitMap.numberOfBitMaps(256)]; for (int bitIndex : expected) { BitMap.set(bits, bitIndex); @@ -104,7 +90,7 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest { @Test public void testFromIndexArray() { - for (int i=0; i<5000; i++) { + for (int i = 0; i < 5; i++) { int[] expected = generateIntArray(10, 256); IndexProducer ip = IndexProducer.fromIndexArray(expected); assertArrayEquals(unique(expected), ip.asIndexArray());
