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.assertTrue;
21
22 import java.util.concurrent.ThreadLocalRandom;
23 import java.util.function.LongPredicate;
24
25 import org.junit.jupiter.api.Test;
26
27 public class DefaultBitMapProducerTest extends AbstractBitMapProducerTest {
28
29 class DefaultBitMapProducer implements BitMapProducer {
30 long[] bitMaps;
31
32 DefaultBitMapProducer(final long[] bitMaps) {
33 this.bitMaps = bitMaps;
34 }
35
36 @Override
37 public boolean forEachBitMap(final LongPredicate predicate) {
38 for (final long bitmap : bitMaps) {
39 if (!predicate.test(bitmap)) {
40 return false;
41 }
42 }
43 return true;
44 }
45 }
46
47
48
49
50
51
52 static long[] generateLongArray(final int size) {
53 return ThreadLocalRandom.current().longs(size).toArray();
54 }
55
56 long[] values = generateLongArray(5);
57
58 @Override
59 protected BitMapProducer createEmptyProducer() {
60 return new DefaultBitMapProducer(new long[0]);
61 }
62
63 @Override
64 protected BitMapProducer createProducer() {
65 return new DefaultBitMapProducer(values);
66 }
67
68 @Override
69 protected boolean emptyIsZeroLength() {
70 return true;
71 }
72
73 @Test
74 public void testAsBitMapArrayLargeArray() {
75 final long[] expected = generateLongArray(32);
76 final BitMapProducer producer = predicate -> {
77 for (final long l : expected) {
78 if (!predicate.test(l)) {
79 return false;
80 }
81 }
82 return true;
83 };
84 final long[] ary = producer.asBitMapArray();
85 assertArrayEquals(expected, ary);
86 }
87
88 @Test
89 public void testFromBitMapArray() {
90 final int nOfBitMaps = BitMap.numberOfBitMaps(256);
91 final long[] expected = generateLongArray(nOfBitMaps);
92 final long[] ary = BitMapProducer.fromBitMapArray(expected).asBitMapArray();
93 assertArrayEquals(expected, ary);
94 }
95
96 @Test
97 public void testFromIndexProducer() {
98 final int[] expected = DefaultIndexProducerTest.generateIntArray(10, 256);
99 final IndexProducer ip = IndexProducer.fromIndexArray(expected);
100 final long[] ary = BitMapProducer.fromIndexProducer(ip, 256).asBitMapArray();
101 for (final int idx : expected) {
102 assertTrue(BitMap.contains(ary, idx));
103 }
104 }
105 }