1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root directory). 5 package org.rocksdb; 6 7 import org.junit.ClassRule; 8 import org.junit.Test; 9 10 import java.nio.ByteBuffer; 11 12 import static org.assertj.core.api.Assertions.assertThat; 13 14 public class DirectSliceTest { 15 @ClassRule 16 public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE = 17 new RocksNativeLibraryResource(); 18 19 @Test directSlice()20 public void directSlice() { 21 try(final DirectSlice directSlice = new DirectSlice("abc"); 22 final DirectSlice otherSlice = new DirectSlice("abc")) { 23 assertThat(directSlice.toString()).isEqualTo("abc"); 24 // clear first slice 25 directSlice.clear(); 26 assertThat(directSlice.toString()).isEmpty(); 27 // get first char in otherslice 28 assertThat(otherSlice.get(0)).isEqualTo("a".getBytes()[0]); 29 // remove prefix 30 otherSlice.removePrefix(1); 31 assertThat(otherSlice.toString()).isEqualTo("bc"); 32 } 33 } 34 35 @Test directSliceWithByteBuffer()36 public void directSliceWithByteBuffer() { 37 final byte[] data = "Some text".getBytes(); 38 final ByteBuffer buffer = ByteBuffer.allocateDirect(data.length + 1); 39 buffer.put(data); 40 buffer.put(data.length, (byte)0); 41 42 try(final DirectSlice directSlice = new DirectSlice(buffer)) { 43 assertThat(directSlice.toString()).isEqualTo("Some text"); 44 } 45 } 46 47 @Test directSliceWithByteBufferAndLength()48 public void directSliceWithByteBufferAndLength() { 49 final byte[] data = "Some text".getBytes(); 50 final ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); 51 buffer.put(data); 52 try(final DirectSlice directSlice = new DirectSlice(buffer, 4)) { 53 assertThat(directSlice.toString()).isEqualTo("Some"); 54 } 55 } 56 57 @Test(expected = IllegalArgumentException.class) directSliceInitWithoutDirectAllocation()58 public void directSliceInitWithoutDirectAllocation() { 59 final byte[] data = "Some text".getBytes(); 60 final ByteBuffer buffer = ByteBuffer.wrap(data); 61 try(final DirectSlice directSlice = new DirectSlice(buffer)) { 62 //no-op 63 } 64 } 65 66 @Test(expected = IllegalArgumentException.class) directSlicePrefixInitWithoutDirectAllocation()67 public void directSlicePrefixInitWithoutDirectAllocation() { 68 final byte[] data = "Some text".getBytes(); 69 final ByteBuffer buffer = ByteBuffer.wrap(data); 70 try(final DirectSlice directSlice = new DirectSlice(buffer, 4)) { 71 //no-op 72 } 73 } 74 75 @Test directSliceClear()76 public void directSliceClear() { 77 try(final DirectSlice directSlice = new DirectSlice("abc")) { 78 assertThat(directSlice.toString()).isEqualTo("abc"); 79 directSlice.clear(); 80 assertThat(directSlice.toString()).isEmpty(); 81 directSlice.clear(); // make sure we don't double-free 82 } 83 } 84 85 @Test directSliceRemovePrefix()86 public void directSliceRemovePrefix() { 87 try(final DirectSlice directSlice = new DirectSlice("abc")) { 88 assertThat(directSlice.toString()).isEqualTo("abc"); 89 directSlice.removePrefix(1); 90 assertThat(directSlice.toString()).isEqualTo("bc"); 91 } 92 } 93 } 94