View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * A testing CellConsumer that always returns true.
35       */
36      private static final CellConsumer TRUE_CONSUMER = (i, j) -> true;
37      /**
38       * A testing CellConsumer that always returns false.
39       */
40      private static final CellConsumer FALSE_CONSUMER = (i, j) -> false;
41  
42      /**
43       * Creates a producer without data.
44       * @return a producer that has no data.
45       */
46      @Override
47      protected abstract CellProducer createEmptyProducer();
48  
49      /**
50       * Creates a producer with some data.
51       * @return a producer with some data
52       */
53      @Override
54      protected abstract CellProducer createProducer();
55  
56      @Override
57      protected final int getAsIndexArrayBehaviour() {
58          return ORDERED | DISTINCT;
59      }
60  
61      /**
62       * Creates an array of expected values that aligns with the expected indices entries.
63       * @return an array of expected values.
64       * @see AbstractIndexProducerTest#getExpectedIndices()
65       */
66      protected abstract int[] getExpectedValues();
67  
68      /**
69       * Test the behavior of {@link CellProducer#forEachCell(CellConsumer)} with respect
70       * to ordered and distinct indices. Currently the behavior is assumed to be the same as
71       * {@link IndexProducer#forEachIndex(java.util.function.IntPredicate)}.
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          // check order
79          final int[] expected = Arrays.stream(actual).sorted().toArray();
80          assertArrayEquals(expected, actual);
81          // check distinct
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