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 java.util.Arrays;
24 import java.util.function.LongPredicate;
25
26 import org.junit.jupiter.api.Test;
27
28 public abstract class AbstractBitMapProducerTest {
29
30
31
32
33 static final LongPredicate FALSE_CONSUMER = arg0 -> false;
34
35
36
37
38 static final LongPredicate TRUE_CONSUMER = arg0 -> true;
39
40
41
42
43
44 protected abstract BitMapProducer createEmptyProducer();
45
46
47
48
49
50 protected abstract BitMapProducer createProducer();
51
52 protected boolean emptyIsZeroLength() {
53 return false;
54 }
55
56 @Test
57 public final void testAsBitMapArray() {
58 long[] array = createEmptyProducer().asBitMapArray();
59 for (int i = 0; i < array.length; i++) {
60 assertEquals(0, array[i], "Wrong value at " + i);
61 }
62
63 array = createProducer().asBitMapArray();
64 assertFalse(array.length == 0);
65 }
66
67 @Test
68 public final void testForEachBitMap() {
69 assertFalse(createProducer().forEachBitMap(FALSE_CONSUMER), "non-empty should be false");
70 if (emptyIsZeroLength()) {
71 assertTrue(createEmptyProducer().forEachBitMap(FALSE_CONSUMER), "empty should be true");
72 } else {
73 assertFalse(createEmptyProducer().forEachBitMap(FALSE_CONSUMER), "empty should be false");
74 }
75
76 assertTrue(createProducer().forEachBitMap(TRUE_CONSUMER), "non-empty should be true");
77 assertTrue(createEmptyProducer().forEachBitMap(TRUE_CONSUMER), "empty should be true");
78 }
79
80 @Test
81 public void testForEachBitMapEarlyExit() {
82 final int[] passes = new int[1];
83 assertFalse(createProducer().forEachBitMap(l -> {
84 passes[0]++;
85 return false;
86 }));
87 assertEquals(1, passes[0]);
88
89 passes[0] = 0;
90 if (emptyIsZeroLength()) {
91 assertTrue(createEmptyProducer().forEachBitMap(l -> {
92 passes[0]++;
93 return false;
94 }));
95 assertEquals(0, passes[0]);
96 } else {
97 assertFalse(createEmptyProducer().forEachBitMap(l -> {
98 passes[0]++;
99 return false;
100 }));
101 assertEquals(1, passes[0]);
102 }
103 }
104
105 @Test
106 public final void testForEachBitMapPair() {
107 final LongBiPredicate func = (x, y) -> x == y;
108 assertTrue(createEmptyProducer().forEachBitMapPair(createEmptyProducer(), func), "empty == empty failed");
109 assertFalse(createEmptyProducer().forEachBitMapPair(createProducer(), func), "empty == not_empty failed");
110 assertFalse(createProducer().forEachBitMapPair(createEmptyProducer(), func), "not_empty == empty passed");
111 assertTrue(createProducer().forEachBitMapPair(createProducer(), func), "not_empty == not_empty failed");
112
113
114 final int[] count = new int[3];
115 final LongBiPredicate lbp = (x, y) -> {
116 if (x == 0) {
117 count[0]++;
118 }
119 if (y == 0) {
120 count[1]++;
121 }
122 count[2]++;
123 return true;
124 };
125 createEmptyProducer().forEachBitMapPair(createProducer(), lbp);
126 assertEquals(count[2], count[0]);
127
128 Arrays.fill(count, 0);
129 createProducer().forEachBitMapPair(createEmptyProducer(), lbp);
130 assertEquals(count[2], count[1]);
131
132
133
134 final int[] limit = new int[1];
135 final LongBiPredicate shortFunc = (x, y) -> {
136 limit[0]++;
137 return limit[0] < 2;
138 };
139 final BitMapProducer shortProducer = l -> true;
140 assertFalse(createProducer().forEachBitMapPair(shortProducer, shortFunc));
141 }
142
143 @Test
144 public void testForEachBitMapPairEarlyExit() {
145
146
147 final int[] count = new int[1];
148 final LongBiPredicate lbp = (x, y) -> {
149 count[0]++;
150 return false;
151 };
152 createProducer().forEachBitMapPair(createEmptyProducer(), lbp);
153 assertEquals(1, count[0]);
154
155 Arrays.fill(count, 0);
156 createEmptyProducer().forEachBitMapPair(createProducer(), lbp);
157 assertEquals(1, count[0]);
158 }
159 }