|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.common.bytes; |
| 10 | + |
| 11 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 12 | + |
| 13 | +import org.hamcrest.Matchers; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.nio.ByteBuffer; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +public class ByteBuffersBytesReferenceTests extends AbstractBytesReferenceTestCase { |
| 22 | + @ParametersFactory |
| 23 | + public static Collection<Object[]> allocator() { |
| 24 | + return Arrays.asList( |
| 25 | + new Object[] { (Function<Integer, ByteBuffer>) ByteBuffer::allocateDirect }, |
| 26 | + new Object[] { (Function<Integer, ByteBuffer>) ByteBuffer::allocate } |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + private final Function<Integer, ByteBuffer> allocator; |
| 31 | + |
| 32 | + public ByteBuffersBytesReferenceTests(Function<Integer, ByteBuffer> allocator) { |
| 33 | + this.allocator = allocator; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected BytesReference newBytesReference(int length) throws IOException { |
| 38 | + return newBytesReference(length, randomInt(length)); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected BytesReference newBytesReferenceWithOffsetOfZero(int length) throws IOException { |
| 43 | + return newBytesReference(length, 0); |
| 44 | + } |
| 45 | + |
| 46 | + private BytesReference newBytesReference(int length, int offset) throws IOException { |
| 47 | + // we know bytes stream output always creates a paged bytes reference, we use it to create randomized content |
| 48 | + final ByteBuffer buffer = allocator.apply(length + offset); |
| 49 | + for (int i = 0; i < length + offset; i++) { |
| 50 | + buffer.put((byte) random().nextInt(1 << 8)); |
| 51 | + } |
| 52 | + assertEquals(length + offset, buffer.limit()); |
| 53 | + buffer.flip().position(offset); |
| 54 | + |
| 55 | + BytesReference ref = BytesReference.fromByteBuffer(buffer); |
| 56 | + assertEquals(length, ref.length()); |
| 57 | + assertTrue(ref instanceof BytesArray); |
| 58 | + assertThat(ref.length(), Matchers.equalTo(length)); |
| 59 | + return ref; |
| 60 | + } |
| 61 | + |
| 62 | + public void testArray() throws IOException { |
| 63 | + int[] sizes = { 0, randomInt(PAGE_SIZE), PAGE_SIZE, randomIntBetween(2, PAGE_SIZE * randomIntBetween(2, 5)) }; |
| 64 | + |
| 65 | + for (int i = 0; i < sizes.length; i++) { |
| 66 | + BytesArray pbr = (BytesArray) newBytesReference(sizes[i]); |
| 67 | + byte[] array = pbr.array(); |
| 68 | + assertNotNull(array); |
| 69 | + assertEquals(sizes[i], array.length - pbr.offset()); |
| 70 | + assertSame(array, pbr.array()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public void testArrayOffset() throws IOException { |
| 75 | + int length = randomInt(PAGE_SIZE * randomIntBetween(2, 5)); |
| 76 | + BytesArray pbr = (BytesArray) newBytesReferenceWithOffsetOfZero(length); |
| 77 | + assertEquals(0, pbr.offset()); |
| 78 | + } |
| 79 | +} |
0 commit comments