Simplify random indices tests

Use streams to generate arrays.

Use BitSet to create a unique set of indices.
This commit is contained in:
Alex Herbert 2022-09-09 22:55:14 +01:00
parent 5920f32cd8
commit 2a6ec0fca0
3 changed files with 29 additions and 46 deletions

View File

@ -24,8 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet;
import java.util.List; import java.util.List;
import java.util.SortedSet;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -121,7 +121,7 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> {
@Test @Test
public void testMergeWithHasher() { public void testMergeWithHasher() {
for (int i=0; i<5000; i++) { for (int i = 0; i < 5; i++) {
final BloomFilter f = createEmptyFilter(getTestShape()); final BloomFilter f = createEmptyFilter(getTestShape());
int[] expected = DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits()); int[] expected = DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits());
Hasher hasher = new ArrayHasher(expected); Hasher hasher = new ArrayHasher(expected);
@ -133,7 +133,7 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> {
@Test @Test
public void testMergeWithBitMapProducer() { public void testMergeWithBitMapProducer() {
for (int i=0; i<5000; i++) { for (int i = 0; i < 5; i++) {
long[] values = new long[2]; long[] values = new long[2];
for (int idx : DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits())) { for (int idx : DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits())) {
BitMap.set(values, idx); BitMap.set(values, idx);
@ -161,12 +161,14 @@ public abstract class AbstractBloomFilterTest<T extends BloomFilter> {
@Test @Test
public void testMergeWithIndexProducer() { 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()); int[] values = DefaultIndexProducerTest.generateIntArray(getTestShape().getNumberOfHashFunctions(), getTestShape().getNumberOfBits());
BloomFilter f = createFilter(getTestShape(), IndexProducer.fromIndexArray(values)); BloomFilter f = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
SortedSet<Integer> uniqueValues = DefaultIndexProducerTest.uniqueSet(values); BitSet uniqueValues = DefaultIndexProducerTest.uniqueSet(values);
assertTrue(f.forEachIndex(idx -> { assertTrue(f.forEachIndex(idx -> {
return uniqueValues.remove(Integer.valueOf(idx)); final boolean result = uniqueValues.get(idx);
uniqueValues.clear(idx);
return result;
})); }));
assertTrue(uniqueValues.isEmpty()); assertTrue(uniqueValues.isEmpty());
} }

View File

@ -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.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Random; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -66,13 +66,8 @@ public class DefaultBitMapProducerTest extends AbstractBitMapProducerTest {
* @param size the number of values to generate * @param size the number of values to generate
* @return the array of random values. * @return the array of random values.
*/ */
public static long[] generateLongArray( int size ) { public static long[] generateLongArray(int size) {
Random rnd = new Random(); return ThreadLocalRandom.current().longs(size).toArray();
long[] expected = new long[size];
for (int i=0; i<size; i++) {
expected[i] = rnd.nextLong();
}
return expected;
} }
@Test @Test

View File

@ -18,21 +18,20 @@ package org.apache.commons.collections4.bloomfilter;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.Random; import java.util.Arrays;
import java.util.Set; import java.util.BitSet;
import java.util.SortedSet; import java.util.concurrent.ThreadLocalRandom;
import java.util.TreeSet;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class DefaultIndexProducerTest extends AbstractIndexProducerTest { public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
private int[] values = generateIntArray( 10, 512 ); private int[] values = generateIntArray(10, 512);
@Override @Override
protected IndexProducer createProducer() { protected IndexProducer createProducer() {
return IndexProducer.fromIndexArray( values ); return IndexProducer.fromIndexArray(values);
} }
@Override @Override
@ -52,26 +51,19 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
* @param bound the upper bound (exclusive) of the values in the array. * @param bound the upper bound (exclusive) of the values in the array.
* @return an array of int. * @return an array of int.
*/ */
public static int[] generateIntArray( int size, int bound ) { public static int[] generateIntArray(int size, int bound) {
Random rnd = new Random(); return ThreadLocalRandom.current().ints(size, 0, bound).toArray();
int[] expected = new int[size];
for (int i=0; i<size; i++) {
expected[i] = rnd.nextInt(bound);
}
return expected;
} }
/** /**
* Creates a sorted set of Integers. * Creates a BitSet of indices.
* @param ary the array to sort and make unique * @param ary the array
* @return the sorted Set. * @return the set.
*/ */
public static SortedSet<Integer> uniqueSet(int[] ary) { public static BitSet uniqueSet(int[] ary) {
SortedSet<Integer> uniq = new TreeSet<Integer>(); final BitSet bs = new BitSet();
for (int idx : ary) { Arrays.stream(ary).forEach(bs::set);
uniq.add(idx); return bs;
}
return uniq;
} }
/** /**
@ -80,19 +72,13 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
* @return the sorted unique array. * @return the sorted unique array.
*/ */
public static int[] unique(int[] ary) { public static int[] unique(int[] ary) {
Set<Integer> uniq = uniqueSet(ary); return Arrays.stream(ary).distinct().sorted().toArray();
int[] result = new int[uniq.size()];
int i=0;
for (int idx : uniq) {
result[i++] = idx;
}
return result;
} }
@Test @Test
public void testFromBitMapProducer() { public void testFromBitMapProducer() {
for (int i=0; i<5000; i++) { for (int i = 0; i < 5; i++) {
int[] expected = generateIntArray( 7, 256 ); int[] expected = generateIntArray(7, 256);
long[] bits = new long[BitMap.numberOfBitMaps(256)]; long[] bits = new long[BitMap.numberOfBitMaps(256)];
for (int bitIndex : expected) { for (int bitIndex : expected) {
BitMap.set(bits, bitIndex); BitMap.set(bits, bitIndex);
@ -104,7 +90,7 @@ public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
@Test @Test
public void testFromIndexArray() { public void testFromIndexArray() {
for (int i=0; i<5000; i++) { for (int i = 0; i < 5; i++) {
int[] expected = generateIntArray(10, 256); int[] expected = generateIntArray(10, 256);
IndexProducer ip = IndexProducer.fromIndexArray(expected); IndexProducer ip = IndexProducer.fromIndexArray(expected);
assertArrayEquals(unique(expected), ip.asIndexArray()); assertArrayEquals(unique(expected), ip.asIndexArray());