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  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.function.LongPredicate;
24  
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.ValueSource;
29  
30  public class IndexProducerTest {
31  
32      private static final class TestingBitMapProducer implements BitMapProducer {
33          long[] values;
34  
35          TestingBitMapProducer(final long[] values) {
36              this.values = values;
37          }
38  
39          @Override
40          public boolean forEachBitMap(final LongPredicate consumer) {
41              for (final long l : values) {
42                  if (!consumer.test(l)) {
43                      return false;
44                  }
45              }
46              return true;
47          }
48      }
49  
50      @Test
51      public void fromBitMapProducerTest() {
52          TestingBitMapProducer producer = new TestingBitMapProducer(new long[] {1L, 2L, 3L});
53          IndexProducer underTest = IndexProducer.fromBitMapProducer(producer);
54          List<Integer> lst = new ArrayList<>();
55  
56          underTest.forEachIndex(lst::add);
57          assertEquals(4, lst.size());
58          assertEquals(Integer.valueOf(0), lst.get(0));
59          assertEquals(Integer.valueOf(1 + 64), lst.get(1));
60          assertEquals(Integer.valueOf(0 + 128), lst.get(2));
61          assertEquals(Integer.valueOf(1 + 128), lst.get(3));
62  
63          producer = new TestingBitMapProducer(new long[] {0xFFFFFFFFFFFFFFFFL});
64          underTest = IndexProducer.fromBitMapProducer(producer);
65          lst = new ArrayList<>();
66  
67          underTest.forEachIndex(lst::add);
68  
69          assertEquals(64, lst.size());
70          for (int i = 0; i < 64; i++) {
71              assertEquals(Integer.valueOf(i), lst.get(i));
72          }
73      }
74  
75      @ParameterizedTest
76      @ValueSource(ints = {32, 33})
77      void testAsIndexArray(final int n) {
78          final IndexProducer ip = i -> {
79              for (int j = 0; j < n; j++) {
80                  // Always test index zero
81                  i.test(0);
82              }
83              return true;
84          };
85          Assertions.assertArrayEquals(new int[n], ip.asIndexArray());
86      }
87  }