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.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       * A testing consumer that always returns false.
32       */
33      static final LongPredicate FALSE_CONSUMER = arg0 -> false;
34  
35      /**
36       * A testing consumer that always returns true.
37       */
38      static final LongPredicate TRUE_CONSUMER = arg0 -> true;
39  
40      /**
41       * Creates an producer without data.
42       * @return a producer that has no data.
43       */
44      protected abstract BitMapProducer createEmptyProducer();
45  
46      /**
47       * Creates a producer with some data.
48       * @return a producer with some data
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         // test BitMapProducers of different length send 0 for missing values.
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         // test where the created producer does not process all records because the predicate function
133         // returns false before the processing is completed.
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         // test BitMapProducers of different length send 0 for missing values.
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 }