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.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 public class SparseBloomFilterTest extends AbstractBloomFilterTest<SparseBloomFilter> {
29 @Override
30 protected SparseBloomFilter createEmptyFilter(final Shape shape) {
31 return new SparseBloomFilter(shape);
32 }
33
34 @Test
35 public void testBitMapProducerEdgeCases() {
36 int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 65, 66, 67, 68, 69, 70, 71};
37 BloomFilter bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
38
39
40 final int[] passes = new int[1];
41 assertFalse(bf.forEachBitMap(l -> {
42 passes[0]++;
43 return false;
44 }));
45 assertEquals(1, passes[0]);
46
47
48 bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
49 passes[0] = 0;
50 assertFalse(bf.forEachBitMap(l -> {
51 final boolean result = passes[0] == 0;
52 if (result) {
53 passes[0]++;
54 }
55 return result;
56 }));
57 assertEquals(1, passes[0]);
58
59
60 values = new int[] {1, 2, 3, 4};
61 bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
62 passes[0] = 0;
63 assertTrue(bf.forEachBitMap(l -> {
64 passes[0]++;
65 return true;
66 }));
67 assertEquals(2, passes[0]);
68
69
70
71 values = new int[] {1, 2, 3, 4};
72 bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
73 passes[0] = 0;
74 assertFalse(bf.forEachBitMap(l -> {
75 final boolean result = passes[0] == 0;
76 if (result) {
77 passes[0]++;
78 }
79 return result;
80 }));
81 assertEquals(1, passes[0]);
82 }
83
84 @Test
85 public void testBloomFilterBasedMergeEdgeCases() {
86 final BloomFilter bf1 = createEmptyFilter(getTestShape());
87 final BloomFilter bf2 = new SimpleBloomFilter(getTestShape());
88 bf2.merge(TestingHashers.FROM1);
89 bf1.merge(bf2);
90 assertTrue(bf2.forEachBitMapPair(bf1, (x, y) -> x == y));
91 }
92 }