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 org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for the {@link SparseBloomFilter}.
27   */
28  public class SparseBloomFilterTest extends AbstractBloomFilterTest<SparseBloomFilter> {
29      @Override
30      protected SparseBloomFilter createEmptyFilter(final Shape shape) {
31          return new SparseBloomFilter(shape);
32      }
33  
34      @Test
35      public void testBitMapProducerEdgeCases() {
36          int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 65, 66, 67, 68, 69, 70, 71};
37          BloomFilter bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
38  
39          // verify exit early before bitmap boundary
40          final int[] passes = new int[1];
41          assertFalse(bf.forEachBitMap(l -> {
42              passes[0]++;
43              return false;
44          }));
45          assertEquals(1, passes[0]);
46  
47          // verify exit early at bitmap boundary
48          bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
49          passes[0] = 0;
50          assertFalse(bf.forEachBitMap(l -> {
51              final boolean result = passes[0] == 0;
52              if (result) {
53                  passes[0]++;
54              }
55              return result;
56          }));
57          assertEquals(1, passes[0]);
58  
59          // verify add extra if all values in first bitmap
60          values = new int[] {1, 2, 3, 4};
61          bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
62          passes[0] = 0;
63          assertTrue(bf.forEachBitMap(l -> {
64              passes[0]++;
65              return true;
66          }));
67          assertEquals(2, passes[0]);
68  
69          // verify exit early if all values in first bitmap and predicate returns false
70          // on 2nd block
71          values = new int[] {1, 2, 3, 4};
72          bf = createFilter(getTestShape(), IndexProducer.fromIndexArray(values));
73          passes[0] = 0;
74          assertFalse(bf.forEachBitMap(l -> {
75              final boolean result = passes[0] == 0;
76              if (result) {
77                  passes[0]++;
78              }
79              return result;
80          }));
81          assertEquals(1, passes[0]);
82      }
83  
84      @Test
85      public void testBloomFilterBasedMergeEdgeCases() {
86          final BloomFilter bf1 = createEmptyFilter(getTestShape());
87          final BloomFilter bf2 = new SimpleBloomFilter(getTestShape());
88          bf2.merge(TestingHashers.FROM1);
89          bf1.merge(bf2);
90          assertTrue(bf2.forEachBitMapPair(bf1, (x, y) -> x == y));
91      }
92  }