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.assertTrue;
21  
22  import java.util.concurrent.ThreadLocalRandom;
23  import java.util.function.LongPredicate;
24  
25  import org.junit.jupiter.api.Test;
26  
27  public class DefaultBitMapProducerTest extends AbstractBitMapProducerTest {
28  
29      class DefaultBitMapProducer implements BitMapProducer {
30          long[] bitMaps;
31  
32          DefaultBitMapProducer(final long[] bitMaps) {
33              this.bitMaps = bitMaps;
34          }
35  
36          @Override
37          public boolean forEachBitMap(final LongPredicate predicate) {
38              for (final long bitmap : bitMaps) {
39                  if (!predicate.test(bitmap)) {
40                      return false;
41                  }
42              }
43              return true;
44          }
45      }
46  
47      /**
48       * Generates an array of random long values.
49       * @param size the number of values to generate
50       * @return the array of random values.
51       */
52      static long[] generateLongArray(final int size) {
53          return ThreadLocalRandom.current().longs(size).toArray();
54      }
55  
56      long[] values = generateLongArray(5);
57  
58      @Override
59      protected BitMapProducer createEmptyProducer() {
60          return new DefaultBitMapProducer(new long[0]);
61      }
62  
63      @Override
64      protected BitMapProducer createProducer() {
65          return new DefaultBitMapProducer(values);
66      }
67  
68      @Override
69      protected boolean emptyIsZeroLength() {
70          return true;
71      }
72  
73      @Test
74      public void testAsBitMapArrayLargeArray() {
75          final long[] expected = generateLongArray(32);
76          final BitMapProducer producer = predicate -> {
77              for (final long l : expected) {
78                  if (!predicate.test(l)) {
79                      return false;
80                  }
81              }
82              return true;
83          };
84          final long[] ary = producer.asBitMapArray();
85          assertArrayEquals(expected, ary);
86      }
87  
88      @Test
89      public void testFromBitMapArray() {
90          final int nOfBitMaps = BitMap.numberOfBitMaps(256);
91          final long[] expected = generateLongArray(nOfBitMaps);
92          final long[] ary = BitMapProducer.fromBitMapArray(expected).asBitMapArray();
93          assertArrayEquals(expected, ary);
94      }
95  
96      @Test
97      public void testFromIndexProducer() {
98          final int[] expected = DefaultIndexProducerTest.generateIntArray(10, 256);
99          final IndexProducer ip = IndexProducer.fromIndexArray(expected);
100         final long[] ary = BitMapProducer.fromIndexProducer(ip, 256).asBitMapArray();
101         for (final int idx : expected) {
102             assertTrue(BitMap.contains(ary, idx));
103         }
104     }
105 }