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 static org.assertj.core.api.Assertions.assertThat;
11 
12 public class SliceTest {
13 
14   @ClassRule
15   public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
16       new RocksNativeLibraryResource();
17 
18   @Test
slice()19   public void slice() {
20     try (final Slice slice = new Slice("testSlice")) {
21       assertThat(slice.empty()).isFalse();
22       assertThat(slice.size()).isEqualTo(9);
23       assertThat(slice.data()).isEqualTo("testSlice".getBytes());
24     }
25 
26     try (final Slice otherSlice = new Slice("otherSlice".getBytes())) {
27       assertThat(otherSlice.data()).isEqualTo("otherSlice".getBytes());
28     }
29 
30     try (final Slice thirdSlice = new Slice("otherSlice".getBytes(), 5)) {
31       assertThat(thirdSlice.data()).isEqualTo("Slice".getBytes());
32     }
33   }
34 
35   @Test
sliceClear()36   public void sliceClear() {
37     try (final Slice slice = new Slice("abc")) {
38       assertThat(slice.toString()).isEqualTo("abc");
39       slice.clear();
40       assertThat(slice.toString()).isEmpty();
41       slice.clear();  // make sure we don't double-free
42     }
43   }
44 
45   @Test
sliceRemovePrefix()46   public void sliceRemovePrefix() {
47     try (final Slice slice = new Slice("abc")) {
48       assertThat(slice.toString()).isEqualTo("abc");
49       slice.removePrefix(1);
50       assertThat(slice.toString()).isEqualTo("bc");
51     }
52   }
53 
54   @Test
sliceEquals()55   public void sliceEquals() {
56     try (final Slice slice = new Slice("abc");
57          final Slice slice2 = new Slice("abc")) {
58       assertThat(slice.equals(slice2)).isTrue();
59       assertThat(slice.hashCode() == slice2.hashCode()).isTrue();
60     }
61   }
62 
63   @Test
sliceStartWith()64   public void sliceStartWith() {
65     try (final Slice slice = new Slice("matchpoint");
66          final Slice match = new Slice("mat");
67          final Slice noMatch = new Slice("nomatch")) {
68       assertThat(slice.startsWith(match)).isTrue();
69       assertThat(slice.startsWith(noMatch)).isFalse();
70     }
71   }
72 
73   @Test
sliceToString()74   public void sliceToString() {
75     try (final Slice slice = new Slice("stringTest")) {
76       assertThat(slice.toString()).isEqualTo("stringTest");
77       assertThat(slice.toString(true)).isNotEqualTo("");
78     }
79   }
80 }
81