]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/test/java/org/rocksdb/DirectSliceTest.java
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rocksdb / java / src / test / java / org / rocksdb / DirectSliceTest.java
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 RocksMemoryResource rocksMemoryResource =
17 new RocksMemoryResource();
18
19 @Test
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
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
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)
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)
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
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
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 }