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.Assert.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.function.BiPredicate;
25  
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  public abstract class AbstractBloomFilterProducerTest {
30      private Shape shape = Shape.fromKM(17, 72);
31  
32      BloomFilter one = new SimpleBloomFilter(shape);
33      BloomFilter two = new SimpleBloomFilter(shape);
34      int[] nullCount = { 0, 0 };
35      int[] equalityCount = { 0 };
36      BiPredicate<BloomFilter, BloomFilter> counter = (x, y) -> {
37          if (x == null) {
38              nullCount[0]++;
39          }
40          if (y == null) {
41              nullCount[1]++;
42          }
43          if (x != null && y != null && x.cardinality() == y.cardinality()) {
44              equalityCount[0]++;
45          }
46          return true;
47      };
48  
49      private BloomFilterProducer createUnderTest() {
50          return createUnderTest(one, two);
51      }
52  
53      /**
54       * Creates a BloomFilterProducer that returns the filters (or their copy) in the order presented.
55       * @param filters The filters to return.
56       * @return A BloomFilterProducer that returns the filters in order.
57       */
58      protected abstract BloomFilterProducer createUnderTest(BloomFilter... filters);
59  
60      /**
61       * The shape of the Bloom filters for testing.
62       * <ul>
63       *  <li>Hash functions (k) = 17
64       *  <li>Number of bits (m) = 72
65       * </ul>
66       * @return the testing shape.
67       */
68      protected Shape getTestShape() {
69          return shape;
70      }
71  
72      @BeforeEach
73      public void setup() {
74          one.clear();
75          one.merge(IndexProducer.fromIndexArray(1));
76          two.clear();
77          two.merge(IndexProducer.fromIndexArray(2, 3));
78          nullCount[0] = 0;
79          nullCount[1] = 0;
80          equalityCount[0] = 0;
81      }
82  
83      @Test
84      public void testAsBloomFilterArray() {
85          BloomFilter[] result = createUnderTest().asBloomFilterArray();
86          assertEquals(2, result.length);
87          assertEquals(1, result[0].cardinality());
88          assertEquals(2, result[1].cardinality());
89      }
90  
91      @Test
92      public void testFlatten() {
93          BloomFilter underTest = createUnderTest().flatten();
94          BloomFilter expected = new SimpleBloomFilter(shape);
95          expected.merge(IndexProducer.fromIndexArray(1, 2, 3));
96          assertArrayEquals(expected.asBitMapArray(), underTest.asBitMapArray());
97      }
98  
99      @Test
100     public void testForEachPairArrayTooLong() {
101         assertTrue(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one, two, one),
102                 counter));
103         assertEquals(1, nullCount[0]);
104         assertEquals(0, nullCount[1]);
105         assertEquals(2, equalityCount[0]);
106     }
107 
108     @Test
109     public void testForEachPairArrayTooShort() {
110         assertTrue(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one), counter));
111         assertEquals(0, nullCount[0]);
112         assertEquals(1, nullCount[1]);
113         assertEquals(1, equalityCount[0]);
114     }
115 
116     @Test
117     public void testForEachPairCompleteMatch() {
118         assertTrue(createUnderTest().forEachBloomFilterPair(createUnderTest(), counter));
119         assertArrayEquals(new int[] { 0, 0 }, nullCount);
120         assertEquals(2, equalityCount[0]);
121     }
122 
123     @Test
124     public void testForEachPairReturnFalseEarly() {
125         assertFalse(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one, two, one),
126                 (x, y) -> false));
127     }
128 
129     @Test
130     public void testForEachPairReturnFalseLate() {
131         assertFalse(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one, two, one),
132                 counter.and((x, y) -> x != null && y != null)));
133         assertEquals(1, nullCount[0]);
134         assertEquals(0, nullCount[1]);
135         assertEquals(2, equalityCount[0]);
136     }
137 
138     @Test
139     public void testForEachPairReturnFalseLateShortArray() {
140         assertFalse(createUnderTest().forEachBloomFilterPair(BloomFilterProducer.fromBloomFilterArray(one),
141                 counter.and((x, y) -> x != null && y != null)));
142         assertEquals(0, nullCount[0]);
143         assertEquals(1, nullCount[1]);
144         assertEquals(1, equalityCount[0]);
145     }
146 }