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.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.BitSet;
26  import java.util.List;
27  import java.util.SplittableRandom;
28  import java.util.concurrent.ThreadLocalRandom;
29  import java.util.function.IntPredicate;
30  
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.CsvSource;
34  
35  /**
36   * Tests the Filter class.
37   */
38  public class IndexFilterTest {
39  
40      @ParameterizedTest
41      @CsvSource({
42          "1, 64",
43          "2, 64",
44          "3, 64",
45          "7, 357",
46          "7, 17",
47      })
48      void testFilter(final int k, final int m) {
49          final Shape shape = Shape.fromKM(k, m);
50          final BitSet used = new BitSet(m);
51          for (int n = 0; n < 10; n++) {
52              used.clear();
53              final List<Integer> consumer = new ArrayList<>();
54              final IntPredicate filter = IndexFilter.create(shape, consumer::add);
55  
56              // Make random indices; these may be duplicates
57              final long seed = ThreadLocalRandom.current().nextLong();
58              final SplittableRandom rng = new SplittableRandom(seed);
59              for (int i = Math.min(k, m / 2); i-- > 0;) {
60                  final int bit = rng.nextInt(m);
61                  // duplicates should not alter the list size
62                  final int newSize = consumer.size() + (used.get(bit) ? 0 : 1);
63                  assertTrue(filter.test(bit));
64                  assertEquals(newSize, consumer.size(), () -> String.format("Bad filter. Seed=%d, bit=%d", seed, bit));
65                  used.set(bit);
66              }
67  
68              // The list should have unique entries
69              assertArrayEquals(used.stream().toArray(), consumer.stream().mapToInt(i -> (int) i).sorted().toArray());
70              final int size = consumer.size();
71  
72              // Second observations do not change the list size
73              used.stream().forEach(bit -> {
74                  assertTrue(filter.test(bit));
75                  assertEquals(size, consumer.size(), () -> String.format("Bad filter. Seed=%d, bit=%d", seed, bit));
76              });
77  
78              assertThrows(IndexOutOfBoundsException.class, () -> filter.test(m));
79              assertThrows(IndexOutOfBoundsException.class, () -> filter.test(-1));
80          }
81      }
82  
83      @Test
84      public void testFiltering() {
85          final Shape shape = Shape.fromKM(3, 12);
86          final List<Integer> consumer = new ArrayList<>();
87          final IntPredicate filter = IndexFilter.create(shape, consumer::add);
88  
89          for (int i = 0; i < 12; i++) {
90              assertTrue(filter.test(i));
91          }
92          assertEquals(12, consumer.size());
93  
94          for (int i = 0; i < 12; i++) {
95              assertTrue(filter.test(i));
96          }
97          assertEquals(12, consumer.size());
98      }
99  }