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.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.junit.jupiter.api.Assertions.fail;
24
25 import java.util.Arrays;
26 import java.util.BitSet;
27
28 import org.apache.commons.collections4.bloomfilter.CellProducer.CellConsumer;
29 import org.junit.jupiter.api.Test;
30
31 public abstract class AbstractCellProducerTest extends AbstractIndexProducerTest {
32
33
34
35
36 private static final CellConsumer TRUE_CONSUMER = (i, j) -> true;
37
38
39
40 private static final CellConsumer FALSE_CONSUMER = (i, j) -> false;
41
42
43
44
45
46 @Override
47 protected abstract CellProducer createEmptyProducer();
48
49
50
51
52
53 @Override
54 protected abstract CellProducer createProducer();
55
56 @Override
57 protected final int getAsIndexArrayBehaviour() {
58 return ORDERED | DISTINCT;
59 }
60
61
62
63
64
65
66 protected abstract int[] getExpectedValues();
67
68
69
70
71
72
73 @Test
74 public final void testBehaviourForEachCell() {
75 final IntList list = new IntList();
76 createProducer().forEachCell((i, j) -> list.add(i));
77 final int[] actual = list.toArray();
78
79 final int[] expected = Arrays.stream(actual).sorted().toArray();
80 assertArrayEquals(expected, actual);
81
82 final long count = Arrays.stream(actual).distinct().count();
83 assertEquals(count, actual.length);
84 }
85
86 @Test
87 public final void testEmptyCellProducer() {
88 final CellProducer empty = createEmptyProducer();
89 final int[] ary = empty.asIndexArray();
90 assertEquals(0, ary.length);
91 assertTrue(empty.forEachCell((i, j) -> {
92 fail("forEachCell consumer should not be called");
93 return false;
94 }));
95 }
96
97 @Test
98 public void testForEachCellEarlyExit() {
99 final int[] passes = new int[1];
100 assertTrue(createEmptyProducer().forEachCell((i, j) -> {
101 passes[0]++;
102 return false;
103 }));
104 assertEquals(0, passes[0]);
105
106 assertFalse(createProducer().forEachCell((i, j) -> {
107 passes[0]++;
108 return false;
109 }));
110 assertEquals(1, passes[0]);
111 }
112
113 @Test
114 public final void testForEachCellPredicates() {
115 final CellProducer populated = createProducer();
116 final CellProducer empty = createEmptyProducer();
117
118 assertFalse(populated.forEachCell(FALSE_CONSUMER), "non-empty should be false");
119 assertTrue(empty.forEachCell(FALSE_CONSUMER), "empty should be true");
120
121 assertTrue(populated.forEachCell(TRUE_CONSUMER), "non-empty should be true");
122 assertTrue(empty.forEachCell(TRUE_CONSUMER), "empty should be true");
123 }
124
125 @Test
126 public void testForEachCellValues() {
127 final int[] expectedIdx = getExpectedIndices();
128 final int[] expectedValue = getExpectedValues();
129 assertEquals(expectedIdx.length, expectedValue.length, "expected index length and value length do not match");
130 final int[] idx = {0};
131 createProducer().forEachCell((i, j) -> {
132 assertEquals(expectedIdx[idx[0]], i, "bad index at " + idx[0]);
133 assertEquals(expectedValue[idx[0]], j, "bad value at " + idx[0]);
134 idx[0]++;
135 return true;
136 });
137 }
138
139 @Test
140 public final void testIndexConsistency() {
141 final CellProducer producer = createProducer();
142 final BitSet bs1 = new BitSet();
143 final BitSet bs2 = new BitSet();
144 producer.forEachIndex(i -> {
145 bs1.set(i);
146 return true;
147 });
148 producer.forEachCell((i, j) -> {
149 bs2.set(i);
150 return true;
151 });
152 assertEquals(bs1, bs2);
153 }
154 }
155