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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.memory
.util
.hash
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.jupiter
.api
.Assertions
.assertThrows
;
23 import java
.util
.Arrays
;
24 import java
.util
.Collection
;
26 import org
.apache
.arrow
.memory
.ArrowBuf
;
27 import org
.apache
.arrow
.memory
.BufferAllocator
;
28 import org
.apache
.arrow
.memory
.RootAllocator
;
29 import org
.junit
.After
;
30 import org
.junit
.Before
;
31 import org
.junit
.Test
;
32 import org
.junit
.runner
.RunWith
;
33 import org
.junit
.runners
.Parameterized
;
36 * Test cases for {@link ArrowBufHasher} and its subclasses.
38 @RunWith(Parameterized
.class)
39 public class TestArrowBufHasher
{
41 private final int BUFFER_LENGTH
= 1024;
43 private BufferAllocator allocator
;
45 private ArrowBufHasher hasher
;
47 public TestArrowBufHasher(String name
, ArrowBufHasher hasher
) {
52 public void prepare() {
53 allocator
= new RootAllocator(1024 * 1024);
57 public void shutdown() {
62 public void testHasher() {
63 try (ArrowBuf buf1
= allocator
.buffer(BUFFER_LENGTH
);
64 ArrowBuf buf2
= allocator
.buffer(BUFFER_LENGTH
)) {
66 for (int i
= 0; i
< BUFFER_LENGTH
/ 4; i
++) {
67 buf1
.setFloat(i
* 4, i
/ 10.0f
);
68 buf2
.setFloat(i
* 4, i
/ 10.0f
);
71 verifyHashCodesEqual(buf1
, 0, 100, buf2
, 0, 100);
72 verifyHashCodesEqual(buf1
, 1, 5, buf2
, 1, 5);
73 verifyHashCodesEqual(buf1
, 10, 17, buf2
, 10, 17);
74 verifyHashCodesEqual(buf1
, 33, 25, buf2
, 33, 25);
75 verifyHashCodesEqual(buf1
, 22, 22, buf2
, 22, 22);
76 verifyHashCodesEqual(buf1
, 123, 333, buf2
, 123, 333);
77 verifyHashCodesEqual(buf1
, 374, 1, buf2
, 374, 1);
78 verifyHashCodesEqual(buf1
, 11, 0, buf2
, 11, 0);
79 verifyHashCodesEqual(buf1
, 75, 25, buf2
, 75, 25);
80 verifyHashCodesEqual(buf1
, 0, 1024, buf2
, 0, 1024);
84 private void verifyHashCodesEqual(ArrowBuf buf1
, int offset1
, int length1
,
85 ArrowBuf buf2
, int offset2
, int length2
) {
86 int hashCode1
= hasher
.hashCode(buf1
, offset1
, length1
);
87 int hashCode2
= hasher
.hashCode(buf2
, offset2
, length2
);
88 assertEquals(hashCode1
, hashCode2
);
92 public void testHasherNegative() {
93 try (ArrowBuf buf
= allocator
.buffer(BUFFER_LENGTH
)) {
95 for (int i
= 0; i
< BUFFER_LENGTH
/ 4; i
++) {
96 buf
.setFloat(i
* 4, i
/ 10.0f
);
99 assertThrows(IllegalArgumentException
.class, () -> {
100 hasher
.hashCode(buf
, 0, -1);
103 assertThrows(IndexOutOfBoundsException
.class, () -> {
104 hasher
.hashCode(buf
, 0, 1028);
107 assertThrows(IndexOutOfBoundsException
.class, () -> {
108 hasher
.hashCode(buf
, 500, 1000);
113 @Parameterized.Parameters(name
= "hasher = {0}")
114 public static Collection
<Object
[]> getHasher() {
115 return Arrays
.asList(
116 new Object
[] {SimpleHasher
.class.getSimpleName(),
117 SimpleHasher
.INSTANCE
},
118 new Object
[] {MurmurHasher
.class.getSimpleName(),