Debian Codegraph @

src/test/java/io/airlift/slice/TestSlices.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import org.testng.annotations.Test;
17
18 import java.nio.ByteBuffer;
19
20 import static org.testng.Assert.assertEquals;
21
22 public class TestSlices
23 {
24 @Test
25 public void testWrapDirectBuffer()
26 throws Exception
27 {
28 testWrapping(ByteBuffer.allocateDirect(50));
29 }
30
31 @Test
32 public void testWrapHeapBuffer()
33 throws Exception
34 {
35 testWrapping(ByteBuffer.allocate(50));
36 }
37
38 private static void testWrapping(ByteBuffer buffer)
39 {
40 // initialize buffer
41 for (int i = 0; i < 50; i++) {
42 buffer.put((byte) i);
43 }
44
45 // test full buffer
46 buffer.rewind();
47 Slice slice = Slices.wrappedBuffer(buffer);
48 assertEquals(slice.length(), 50);
49 for (int i = 0; i < 50; i++) {
50 assertEquals(slice.getByte(i), i);
51 }
52
53 // test limited buffer
54 buffer.position(10).limit(30);
55 slice = Slices.wrappedBuffer(buffer);
56 assertEquals(slice.length(), 20);
57 for (int i = 0; i < 20; i++) {
58 assertEquals(slice.getByte(i), i + 10);
59 }
60
61 // test limited buffer after slicing
62 buffer = buffer.slice();
63 slice = Slices.wrappedBuffer(buffer);
64 assertEquals(slice.length(), 20);
65 for (int i = 0; i < 20; i++) {
66 assertEquals(slice.getByte(i), i + 10);
67 }
68 }
69 }