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).
7 import org
.junit
.ClassRule
;
10 import java
.nio
.ByteBuffer
;
12 import static org
.assertj
.core
.api
.Assertions
.assertThat
;
14 public class DirectSliceTest
{
16 public static final RocksMemoryResource rocksMemoryResource
=
17 new RocksMemoryResource();
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");
26 assertThat(directSlice
.toString()).isEmpty();
27 // get first char in otherslice
28 assertThat(otherSlice
.get(0)).isEqualTo("a".getBytes()[0]);
30 otherSlice
.removePrefix(1);
31 assertThat(otherSlice
.toString()).isEqualTo("bc");
36 public void directSliceWithByteBuffer() {
37 final byte[] data
= "Some text".getBytes();
38 final ByteBuffer buffer
= ByteBuffer
.allocateDirect(data
.length
+ 1);
40 buffer
.put(data
.length
, (byte)0);
42 try(final DirectSlice directSlice
= new DirectSlice(buffer
)) {
43 assertThat(directSlice
.toString()).isEqualTo("Some text");
48 public void directSliceWithByteBufferAndLength() {
49 final byte[] data
= "Some text".getBytes();
50 final ByteBuffer buffer
= ByteBuffer
.allocateDirect(data
.length
);
52 try(final DirectSlice directSlice
= new DirectSlice(buffer
, 4)) {
53 assertThat(directSlice
.toString()).isEqualTo("Some");
57 @Test(expected
= IllegalArgumentException
.class)
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
)) {
66 @Test(expected
= IllegalArgumentException
.class)
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)) {
76 public void directSliceClear() {
77 try(final DirectSlice directSlice
= new DirectSlice("abc")) {
78 assertThat(directSlice
.toString()).isEqualTo("abc");
80 assertThat(directSlice
.toString()).isEmpty();
81 directSlice
.clear(); // make sure we don't double-free
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");