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  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests the {@link EnhancedDoubleHasher}.
26   */
27  public class EnhancedDoubleHasherTest extends AbstractHasherTest {
28      int[] expected = {1, 0, 71, 71, 1, 6, 15, 29, 49, 4, 39, 11, 65, 58, 63, 9, 41};
29  
30      @Override
31      protected Hasher createEmptyHasher() {
32          return NullHasher.INSTANCE;
33      }
34  
35      @Override
36      protected Hasher createHasher() {
37          return new EnhancedDoubleHasher(1, 1);
38      }
39  
40      @Override
41      protected int getAsIndexArrayBehaviour() {
42          // Allows duplicates and may be unordered
43          return 0;
44      }
45  
46      @Override
47      protected int[] getExpectedIndices() {
48          return expected;
49      }
50  
51      @Override
52      protected int getHasherSize(final Hasher hasher) {
53          return 1;
54      }
55  
56      @Test
57      public void testByteConstructor() {
58          // single value become increment.
59          EnhancedDoubleHasher hasher = new EnhancedDoubleHasher(new byte[] {1});
60          assertEquals(0, hasher.getInitial());
61          assertEquals(0x01_00_00_00_00_00_00_00L, hasher.getIncrement());
62  
63          // 2 bytes become initial and increment.
64          hasher = new EnhancedDoubleHasher(new byte[] {1, 2});
65          assertEquals(0x01_00_00_00_00_00_00_00L, hasher.getInitial());
66          assertEquals(0x02_00_00_00_00_00_00_00L, hasher.getIncrement());
67  
68          // odd values place extra byte in increment.
69          hasher = new EnhancedDoubleHasher(new byte[] {1, 2, 3});
70          assertEquals(0x01_00_00_00_00_00_00_00L, hasher.getInitial());
71          assertEquals(0x02_03_00_00_00_00_00_00L, hasher.getIncrement());
72  
73          // even short split
74          hasher = new EnhancedDoubleHasher(new byte[] {0, 1, 0, 2});
75          assertEquals(0x01_00_00_00_00_00_00L, hasher.getInitial());
76          assertEquals(0x02_00_00_00_00_00_00L, hasher.getIncrement());
77  
78          // longs are parse correctly
79          hasher = new EnhancedDoubleHasher(new byte[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2});
80          assertEquals(1, hasher.getInitial());
81          assertEquals(2, hasher.getIncrement());
82  
83          // excess bytes are ignored before mid point and at end
84          hasher = new EnhancedDoubleHasher(new byte[] {0, 0, 0, 0, 0, 0, 0, 1, 5, 5, 0, 0, 0, 0, 0, 0, 0, 2, 5, 5});
85          assertEquals(1, hasher.getInitial());
86          assertEquals(2, hasher.getIncrement());
87  
88          // odd extra bytes are accounted for correctly
89          hasher = new EnhancedDoubleHasher(new byte[] {0, 0, 0, 0, 0, 0, 0, 1, 5, 1, 0, 0, 0, 0, 0, 0, 2, 5, 5});
90          assertEquals(1, hasher.getInitial());
91          assertEquals(0x01_00_00_00_00_00_00_02L, hasher.getIncrement());
92  
93          // test empty buffer
94          assertThrows(IllegalArgumentException.class, () -> new EnhancedDoubleHasher(new byte[0]));
95      }
96  }