]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/memory/memory-core/src/test/java/org/apache/arrow/memory/util/hash/TestArrowBufHasher.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / memory / memory-core / src / test / java / org / apache / arrow / memory / util / hash / TestArrowBufHasher.java
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
18 package org.apache.arrow.memory.util.hash;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.util.Arrays;
24 import java.util.Collection;
25
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;
34
35 /**
36 * Test cases for {@link ArrowBufHasher} and its subclasses.
37 */
38 @RunWith(Parameterized.class)
39 public class TestArrowBufHasher {
40
41 private final int BUFFER_LENGTH = 1024;
42
43 private BufferAllocator allocator;
44
45 private ArrowBufHasher hasher;
46
47 public TestArrowBufHasher(String name, ArrowBufHasher hasher) {
48 this.hasher = hasher;
49 }
50
51 @Before
52 public void prepare() {
53 allocator = new RootAllocator(1024 * 1024);
54 }
55
56 @After
57 public void shutdown() {
58 allocator.close();
59 }
60
61 @Test
62 public void testHasher() {
63 try (ArrowBuf buf1 = allocator.buffer(BUFFER_LENGTH);
64 ArrowBuf buf2 = allocator.buffer(BUFFER_LENGTH)) {
65 // prepare data
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);
69 }
70
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);
81 }
82 }
83
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);
89 }
90
91 @Test
92 public void testHasherNegative() {
93 try (ArrowBuf buf = allocator.buffer(BUFFER_LENGTH)) {
94 // prepare data
95 for (int i = 0; i < BUFFER_LENGTH / 4; i++) {
96 buf.setFloat(i * 4, i / 10.0f);
97 }
98
99 assertThrows(IllegalArgumentException.class, () -> {
100 hasher.hashCode(buf, 0, -1);
101 });
102
103 assertThrows(IndexOutOfBoundsException.class, () -> {
104 hasher.hashCode(buf, 0, 1028);
105 });
106
107 assertThrows(IndexOutOfBoundsException.class, () -> {
108 hasher.hashCode(buf, 500, 1000);
109 });
110 }
111 }
112
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(),
119 new MurmurHasher()
120 }
121 );
122 }
123 }