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.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.Test;
26  
27  public class IndexProducerFromBitmapProducerTest extends AbstractIndexProducerTest {
28  
29      private static final class TestingBitMapProducer implements BitMapProducer {
30          long[] values;
31  
32          TestingBitMapProducer(final long[] values) {
33              this.values = values;
34          }
35  
36          @Override
37          public boolean forEachBitMap(final LongPredicate consumer) {
38              for (final long l : values) {
39                  if (!consumer.test(l)) {
40                      return false;
41                  }
42              }
43              return true;
44          }
45      }
46  
47      @Override
48      protected IndexProducer createEmptyProducer() {
49          final TestingBitMapProducer producer = new TestingBitMapProducer(new long[0]);
50          return IndexProducer.fromBitMapProducer(producer);
51      }
52  
53      @Override
54      protected IndexProducer createProducer() {
55          /* Creates an index producer that produces the values:
56           * 0, 65, 128, and 129
57           @formatter:off
58                  Index2    Index1     Index0
59           bit       128        64          0
60                       |         |          |
61           1L =>       |         |    ...0001
62           2L =>       |   ...0010
63           3L => ...0011
64           @formatter:on
65           */
66          final TestingBitMapProducer producer = new TestingBitMapProducer(new long[] {1L, 2L, 3L});
67          return IndexProducer.fromBitMapProducer(producer);
68      }
69  
70      @Override
71      protected int getAsIndexArrayBehaviour() {
72          // Bit maps will be distinct. Conversion to indices should be ordered.
73          return DISTINCT | ORDERED;
74      }
75  
76      @Override
77      protected int[] getExpectedIndices() {
78          return new int[] {0, 65, 128, 129};
79      }
80  
81      @Test
82      public final void testFromBitMapProducerTest() {
83          IndexProducer underTest = createProducer();
84          List<Integer> lst = new ArrayList<>();
85  
86          underTest.forEachIndex(lst::add);
87          assertEquals(4, lst.size());
88          assertEquals(Integer.valueOf(0), lst.get(0));
89          assertEquals(Integer.valueOf(1 + 64), lst.get(1));
90          assertEquals(Integer.valueOf(0 + 128), lst.get(2));
91          assertEquals(Integer.valueOf(1 + 128), lst.get(3));
92  
93          final BitMapProducer producer = new TestingBitMapProducer(new long[] {0xFFFFFFFFFFFFFFFFL});
94          underTest = IndexProducer.fromBitMapProducer(producer);
95          lst = new ArrayList<>();
96  
97          underTest.forEachIndex(lst::add);
98  
99          assertEquals(64, lst.size());
100         for (int i = 0; i < 64; i++) {
101             assertEquals(Integer.valueOf(i), lst.get(i));
102         }
103     }
104 }