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 io.netty.buffer; 19 20 import org.apache.arrow.memory.ArrowBuf; 21 import org.apache.arrow.memory.BufferAllocator; 22 import org.apache.arrow.memory.RootAllocator; 23 import org.junit.Assert; 24 import org.junit.Test; 25 26 public class TestExpandableByteBuf { 27 28 @Test testCapacity()29 public void testCapacity() { 30 try (BufferAllocator allocator = new RootAllocator(128); 31 ArrowBuf buf = allocator.buffer(20); 32 ) { 33 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 34 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 35 ByteBuf newByteBuf = expandableByteBuf.capacity(31); 36 int capacity = newByteBuf.capacity(); 37 Assert.assertEquals(32, capacity); 38 } 39 } 40 41 @Test testCapacity1()42 public void testCapacity1() { 43 try (BufferAllocator allocator = new RootAllocator(128); 44 ArrowBuf buf = allocator.buffer(20); 45 ) { 46 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 47 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 48 ByteBuf newByteBuf = expandableByteBuf.capacity(32); 49 int capacity = newByteBuf.capacity(); 50 Assert.assertEquals(32, capacity); 51 } 52 } 53 54 @Test testSetAndGetIntValues()55 public void testSetAndGetIntValues() { 56 try (BufferAllocator allocator = new RootAllocator(128); 57 ArrowBuf buf = allocator.buffer(20); 58 ) { 59 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 60 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 61 int [] intVals = new int[] {Integer.MIN_VALUE, Short.MIN_VALUE - 1, Short.MIN_VALUE, 0 , 62 Short.MAX_VALUE , Short.MAX_VALUE + 1, Integer.MAX_VALUE}; 63 for (int intValue :intVals) { 64 expandableByteBuf.setInt(0, intValue); 65 Assert.assertEquals(expandableByteBuf.getInt(0), intValue); 66 Assert.assertEquals(expandableByteBuf.getIntLE(0), Integer.reverseBytes(intValue)); 67 } 68 } 69 } 70 71 @Test testSetAndGetLongValues()72 public void testSetAndGetLongValues() { 73 try (BufferAllocator allocator = new RootAllocator(128); 74 ArrowBuf buf = allocator.buffer(20); 75 ) { 76 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 77 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 78 long [] longVals = new long[] {Long.MIN_VALUE, 0 , Long.MAX_VALUE}; 79 for (long longValue :longVals) { 80 expandableByteBuf.setLong(0, longValue); 81 Assert.assertEquals(expandableByteBuf.getLong(0), longValue); 82 Assert.assertEquals(expandableByteBuf.getLongLE(0), Long.reverseBytes(longValue)); 83 } 84 } 85 } 86 87 @Test testSetAndGetShortValues()88 public void testSetAndGetShortValues() { 89 try (BufferAllocator allocator = new RootAllocator(128); 90 ArrowBuf buf = allocator.buffer(20); 91 ) { 92 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 93 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 94 short [] shortVals = new short[] {Short.MIN_VALUE, 0 , Short.MAX_VALUE}; 95 for (short shortValue :shortVals) { 96 expandableByteBuf.setShort(0, shortValue); 97 Assert.assertEquals(expandableByteBuf.getShort(0), shortValue); 98 Assert.assertEquals(expandableByteBuf.getShortLE(0), Short.reverseBytes(shortValue)); 99 } 100 } 101 } 102 103 @Test testSetAndGetByteValues()104 public void testSetAndGetByteValues() { 105 try (BufferAllocator allocator = new RootAllocator(128); 106 ArrowBuf buf = allocator.buffer(20); 107 ) { 108 NettyArrowBuf srcByteBuf = NettyArrowBuf.unwrapBuffer(buf); 109 ExpandableByteBuf expandableByteBuf = new ExpandableByteBuf(srcByteBuf, allocator); 110 byte [] byteVals = new byte[] {Byte.MIN_VALUE, 0 , Byte.MAX_VALUE}; 111 for (short byteValue :byteVals) { 112 expandableByteBuf.setByte(0, byteValue); 113 Assert.assertEquals(expandableByteBuf.getByte(0), byteValue); 114 } 115 } 116 } 117 } 118