]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
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.c; | |
19 | ||
20 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; | |
21 | import static org.junit.jupiter.api.Assertions.assertEquals; | |
22 | ||
23 | import java.nio.ByteBuffer; | |
24 | import java.nio.ByteOrder; | |
25 | ||
26 | import org.apache.arrow.c.NativeUtil; | |
27 | import org.apache.arrow.memory.ArrowBuf; | |
28 | import org.apache.arrow.memory.RootAllocator; | |
29 | import org.apache.arrow.memory.util.LargeMemoryUtil; | |
30 | import org.apache.arrow.memory.util.MemoryUtil; | |
31 | import org.junit.jupiter.api.AfterEach; | |
32 | import org.junit.jupiter.api.BeforeEach; | |
33 | import org.junit.jupiter.api.Test; | |
34 | ||
35 | public class NativeUtilTest { | |
36 | ||
37 | private RootAllocator allocator = null; | |
38 | ||
39 | @BeforeEach | |
40 | public void setUp() { | |
41 | allocator = new RootAllocator(Long.MAX_VALUE); | |
42 | } | |
43 | ||
44 | @AfterEach | |
45 | public void tearDown() { | |
46 | allocator.close(); | |
47 | } | |
48 | ||
49 | @Test | |
50 | public void testString() { | |
51 | String javaString = "abc"; | |
52 | byte[] nativeString = new byte[] { 97, 98, 99, 0 }; | |
53 | try (ArrowBuf buffer = NativeUtil.toNativeString(allocator, javaString)) { | |
54 | int totalSize = LargeMemoryUtil.checkedCastToInt(buffer.readableBytes()); | |
55 | ByteBuffer reader = MemoryUtil.directBuffer(buffer.memoryAddress(), totalSize).order(ByteOrder.nativeOrder()); | |
56 | byte[] result = new byte[totalSize]; | |
57 | reader.get(result); | |
58 | assertArrayEquals(nativeString, result); | |
59 | ||
60 | assertEquals(javaString, NativeUtil.toJavaString(buffer.memoryAddress())); | |
61 | } | |
62 | } | |
63 | ||
64 | @Test | |
65 | public void testToJavaArray() { | |
66 | long[] nativeArray = new long[] { 1, 2, 3 }; | |
67 | try (ArrowBuf buffer = allocator.buffer(Long.BYTES * nativeArray.length, null)) { | |
68 | for (long value : nativeArray) { | |
69 | buffer.writeLong(value); | |
70 | } | |
71 | long[] actual = NativeUtil.toJavaArray(buffer.memoryAddress(), nativeArray.length); | |
72 | assertArrayEquals(nativeArray, actual); | |
73 | } | |
74 | } | |
75 | ||
76 | @Test | |
77 | public void testToZeroJavaArray() { | |
78 | long[] actual = NativeUtil.toJavaArray(0xDEADBEEF, 0); | |
79 | assertEquals(0, actual.length); | |
80 | } | |
81 | ||
82 | } |