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  
21  import java.util.Arrays;
22  import java.util.BitSet;
23  import java.util.Objects;
24  import java.util.concurrent.ThreadLocalRandom;
25  import java.util.stream.IntStream;
26  
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.params.ParameterizedTest;
29  import org.junit.jupiter.params.provider.ValueSource;
30  
31  public class DefaultIndexProducerTest extends AbstractIndexProducerTest {
32  
33      /**
34       * Generates an array of integers.
35       * @param size the size of the array
36       * @param bound the upper bound (exclusive) of the values in the array.
37       * @return an array of int.
38       */
39      public static int[] generateIntArray(final int size, final int bound) {
40          return ThreadLocalRandom.current().ints(size, 0, bound).toArray();
41      }
42  
43      /**
44       * Creates a sorted unique array of ints.
45       * @param ary the array to sort and make unique
46       * @return the sorted unique array.
47       */
48      public static int[] unique(final int[] ary) {
49          return Arrays.stream(ary).distinct().sorted().toArray();
50      }
51  
52      /**
53       * Creates a BitSet of indices.
54       * @param ary the array
55       * @return the set.
56       */
57      public static BitSet uniqueSet(final int[] ary) {
58          final BitSet bs = new BitSet();
59          Arrays.stream(ary).forEach(bs::set);
60          return bs;
61      }
62  
63      /** Make forEachIndex unordered and contain duplicates. */
64      private final int[] values = {10, 1, 10, 1};
65  
66      @Override
67      protected IndexProducer createEmptyProducer() {
68          return predicate -> {
69              Objects.requireNonNull(predicate);
70              return true;
71          };
72      }
73  
74      @Override
75      protected IndexProducer createProducer() {
76          return predicate -> {
77              Objects.requireNonNull(predicate);
78              for (final int i : values) {
79                  if (!predicate.test(i)) {
80                      return false;
81                  }
82              }
83              return true;
84          };
85      }
86  
87      @Override
88      protected int getAsIndexArrayBehaviour() {
89          return 0;
90      }
91  
92      @Override
93      protected int[] getExpectedIndices() {
94          return values;
95      }
96  
97      @Override
98      protected int getForEachIndexBehaviour() {
99          // the forEachIndex implementation returns unordered duplicates.
100         return 0;
101     }
102 
103     @ParameterizedTest
104     @ValueSource(ints = {32, 33})
105     public void testEntries(final int size) {
106         final int[] values = IntStream.range(0, size).toArray();
107         final IndexProducer producer =  predicate -> {
108             Objects.requireNonNull(predicate);
109             for (final int i : values) {
110                 if (!predicate.test(i)) {
111                     return false;
112                 }
113             }
114             return true;
115         };
116         final int[] other = producer.asIndexArray();
117         assertArrayEquals(values, other);
118     }
119 
120     @Test
121     public void testFromBitMapProducer() {
122         for (int i = 0; i < 5; i++) {
123             final int[] expected = generateIntArray(7, 256);
124             final long[] bits = new long[BitMap.numberOfBitMaps(256)];
125             for (final int bitIndex : expected) {
126                 BitMap.set(bits, bitIndex);
127             }
128             final IndexProducer ip = IndexProducer.fromBitMapProducer(BitMapProducer.fromBitMapArray(bits));
129             assertArrayEquals(unique(expected), ip.asIndexArray());
130         }
131     }
132 
133     @Test
134     public void testFromIndexArray() {
135         for (int i = 0; i < 5; i++) {
136             final int[] expected = generateIntArray(10, 256);
137             final IndexProducer ip = IndexProducer.fromIndexArray(expected);
138             assertArrayEquals(expected, ip.asIndexArray());
139         }
140     }
141 }