Debian Codegraph @

src/test/java/io/airlift/slice/TestByteArrays.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.ByteOrder;
19
20 import static java.lang.Double.longBitsToDouble;
21 import static java.lang.Float.intBitsToFloat;
22 import static org.testng.Assert.assertEquals;
23
24 public class TestByteArrays
25 {
26 @Test
27 public void testReading()
28 {
29 assertEquals(ByteOrder.nativeOrder(), ByteOrder.LITTLE_ENDIAN);
30
31 byte[] bytes = new byte[10];
32 Slice slice = Slices.wrappedBuffer(bytes);
33 slice.setInt(0, 0xDEADBEEF);
34 slice.setInt(4, 0xCAFEBABE);
35
36 // little endian memory layout: EF BE AD DE BE BA FE CA 00 00
37 assertEquals(slice.getBytes(), new byte[] {
38 (byte) 0xEF, (byte) 0xBE, (byte) 0xAD, (byte) 0xDE,
39 (byte) 0xBE, (byte) 0xBA, (byte) 0xFE, (byte) 0xCA,
40 0x00, 0x00});
41
42 assertEquals(ByteArrays.getShort(bytes, 0), (short) 0xBEEF);
43 assertEquals(ByteArrays.getShort(bytes, 1), (short) 0xADBE);
44 assertEquals(ByteArrays.getShort(bytes, 2), (short) 0xDEAD);
45 assertEquals(ByteArrays.getShort(bytes, 3), (short) 0xBEDE);
46
47 assertEquals(ByteArrays.getInt(bytes, 0), 0xDEADBEEF);
48 assertEquals(ByteArrays.getInt(bytes, 1), 0xBEDEADBE);
49 assertEquals(ByteArrays.getInt(bytes, 2), 0xBABEDEAD);
50 assertEquals(ByteArrays.getInt(bytes, 3), 0xFEBABEDE);
51 assertEquals(ByteArrays.getInt(bytes, 4), 0xCAFEBABE);
52
53 assertEquals(ByteArrays.getLong(bytes, 0), 0xCAFEBABE_DEADBEEFL);
54 assertEquals(ByteArrays.getLong(bytes, 1), 0x00CAFEBA_BEDEADBEL);
55 assertEquals(ByteArrays.getLong(bytes, 2), 0x0000CAFE_BABEDEADL);
56
57 assertEquals(ByteArrays.getFloat(bytes, 0), intBitsToFloat(0xDEADBEEF));
58 assertEquals(ByteArrays.getFloat(bytes, 1), intBitsToFloat(0xBEDEADBE));
59 assertEquals(ByteArrays.getFloat(bytes, 2), intBitsToFloat(0xBABEDEAD));
60 assertEquals(ByteArrays.getFloat(bytes, 3), intBitsToFloat(0xFEBABEDE));
61 assertEquals(ByteArrays.getFloat(bytes, 4), intBitsToFloat(0xCAFEBABE));
62
63 assertEquals(ByteArrays.getDouble(bytes, 0), longBitsToDouble(0xCAFEBABE_DEADBEEFL));
64 assertEquals(ByteArrays.getDouble(bytes, 1), longBitsToDouble(0x00CAFEBA_BEDEADBEL));
65 assertEquals(ByteArrays.getDouble(bytes, 2), longBitsToDouble(0x0000CAFE_BABEDEADL));
66 }
67
68 @Test(expectedExceptions = IndexOutOfBoundsException.class)
69 public void testReadingShortBounds()
70 {
71 ByteArrays.getShort(new byte[3], 2);
72 }
73
74 @Test(expectedExceptions = IndexOutOfBoundsException.class)
75 public void testReadingIntBounds()
76 {
77 ByteArrays.getInt(new byte[5], 2);
78 }
79
80 @Test(expectedExceptions = IndexOutOfBoundsException.class)
81 public void testReadingLongBounds()
82 {
83 ByteArrays.getLong(new byte[9], 2);
84 }
85
86 @Test(expectedExceptions = IndexOutOfBoundsException.class)
87 public void testReadingFloatBounds()
88 {
89 ByteArrays.getFloat(new byte[5], 2);
90 }
91
92 @Test(expectedExceptions = IndexOutOfBoundsException.class)
93 public void testReadingDoubleBounds()
94 {
95 ByteArrays.getDouble(new byte[9], 2);
96 }
97 }