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 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.ArrayList;
25 import java.util.BitSet;
26 import java.util.List;
27 import java.util.SplittableRandom;
28 import java.util.concurrent.ThreadLocalRandom;
29 import java.util.function.IntPredicate;
30
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.params.ParameterizedTest;
33 import org.junit.jupiter.params.provider.CsvSource;
34
35
36
37
38 public class IndexFilterTest {
39
40 @ParameterizedTest
41 @CsvSource({
42 "1, 64",
43 "2, 64",
44 "3, 64",
45 "7, 357",
46 "7, 17",
47 })
48 void testFilter(final int k, final int m) {
49 final Shape shape = Shape.fromKM(k, m);
50 final BitSet used = new BitSet(m);
51 for (int n = 0; n < 10; n++) {
52 used.clear();
53 final List<Integer> consumer = new ArrayList<>();
54 final IntPredicate filter = IndexFilter.create(shape, consumer::add);
55
56
57 final long seed = ThreadLocalRandom.current().nextLong();
58 final SplittableRandom rng = new SplittableRandom(seed);
59 for (int i = Math.min(k, m / 2); i-- > 0;) {
60 final int bit = rng.nextInt(m);
61
62 final int newSize = consumer.size() + (used.get(bit) ? 0 : 1);
63 assertTrue(filter.test(bit));
64 assertEquals(newSize, consumer.size(), () -> String.format("Bad filter. Seed=%d, bit=%d", seed, bit));
65 used.set(bit);
66 }
67
68
69 assertArrayEquals(used.stream().toArray(), consumer.stream().mapToInt(i -> (int) i).sorted().toArray());
70 final int size = consumer.size();
71
72
73 used.stream().forEach(bit -> {
74 assertTrue(filter.test(bit));
75 assertEquals(size, consumer.size(), () -> String.format("Bad filter. Seed=%d, bit=%d", seed, bit));
76 });
77
78 assertThrows(IndexOutOfBoundsException.class, () -> filter.test(m));
79 assertThrows(IndexOutOfBoundsException.class, () -> filter.test(-1));
80 }
81 }
82
83 @Test
84 public void testFiltering() {
85 final Shape shape = Shape.fromKM(3, 12);
86 final List<Integer> consumer = new ArrayList<>();
87 final IntPredicate filter = IndexFilter.create(shape, consumer::add);
88
89 for (int i = 0; i < 12; i++) {
90 assertTrue(filter.test(i));
91 }
92 assertEquals(12, consumer.size());
93
94 for (int i = 0; i < 12; i++) {
95 assertTrue(filter.test(i));
96 }
97 assertEquals(12, consumer.size());
98 }
99 }