1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.bloomfilter;
18
19 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20
21 import java.util.Arrays;
22 import java.util.BitSet;
23 import java.util.Objects;
24 import java.util.concurrent.ThreadLocalRandom;
25 import java.util.stream.IntStream;
26
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.params.ParameterizedTest;
29 import org.junit.jupiter.params.provider.ValueSource;
30
31 public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
32
33
34
35
36
37
38
39 public static int[] generateIntArray(final int size, final int bound) {
40 return ThreadLocalRandom.current().ints(size, 0, bound).toArray();
41 }
42
43
44
45
46
47
48 public static int[] unique(final int[] ary) {
49 return Arrays.stream(ary).distinct().sorted().toArray();
50 }
51
52
53
54
55
56
57 public static BitSet uniqueSet(final int[] ary) {
58 final BitSet bs = new BitSet();
59 Arrays.stream(ary).forEach(bs::set);
60 return bs;
61 }
62
63
64 private final int[] values = {10, 1, 10, 1};
65
66 @Override
67 protected IndexProducer createEmptyProducer() {
68 return predicate -> {
69 Objects.requireNonNull(predicate);
70 return true;
71 };
72 }
73
74 @Override
75 protected IndexProducer createProducer() {
76 return predicate -> {
77 Objects.requireNonNull(predicate);
78 for (final int i : values) {
79 if (!predicate.test(i)) {
80 return false;
81 }
82 }
83 return true;
84 };
85 }
86
87 @Override
88 protected int getAsIndexArrayBehaviour() {
89 return 0;
90 }
91
92 @Override
93 protected int[] getExpectedIndices() {
94 return values;
95 }
96
97 @Override
98 protected int getForEachIndexBehaviour() {
99
100 return 0;
101 }
102
103 @ParameterizedTest
104 @ValueSource(ints = {32, 33})
105 public void testEntries(final int size) {
106 final int[] values = IntStream.range(0, size).toArray();
107 final IndexProducer producer = predicate -> {
108 Objects.requireNonNull(predicate);
109 for (final int i : values) {
110 if (!predicate.test(i)) {
111 return false;
112 }
113 }
114 return true;
115 };
116 final int[] other = producer.asIndexArray();
117 assertArrayEquals(values, other);
118 }
119
120 @Test
121 public void testFromBitMapProducer() {
122 for (int i = 0; i < 5; i++) {
123 final int[] expected = generateIntArray(7, 256);
124 final long[] bits = new long[BitMap.numberOfBitMaps(256)];
125 for (final int bitIndex : expected) {
126 BitMap.set(bits, bitIndex);
127 }
128 final IndexProducer ip = IndexProducer.fromBitMapProducer(BitMapProducer.fromBitMapArray(bits));
129 assertArrayEquals(unique(expected), ip.asIndexArray());
130 }
131 }
132
133 @Test
134 public void testFromIndexArray() {
135 for (int i = 0; i < 5; i++) {
136 final int[] expected = generateIntArray(10, 256);
137 final IndexProducer ip = IndexProducer.fromIndexArray(expected);
138 assertArrayEquals(expected, ip.asIndexArray());
139 }
140 }
141 }