1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.nio.ByteBuffer;
21
22 import org.apache.hadoop.hbase.testclassification.SmallTests;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.junit.experimental.categories.Category;
26
27 @Category(SmallTests.class)
28 public class TestSimplePositionedMutableByteRange {
29 @Test
30 public void testPosition() {
31 PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3);
32
33
34 r.put(Bytes.toBytes("f")[0])
35 .put(Bytes.toBytes("o")[0])
36 .put(Bytes.toBytes("o")[0]);
37 Assert.assertEquals(3, r.getPosition());
38 Assert.assertArrayEquals(
39 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
40 r.getBytes());
41
42
43 r.setPosition(0);
44 r.put(Bytes.toBytes("f"))
45 .put(Bytes.toBytes("o"))
46 .put(Bytes.toBytes("o"));
47 Assert.assertEquals(3, r.getPosition());
48 Assert.assertArrayEquals(
49 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
50 r.getBytes());
51
52
53 r.setPosition(0);
54 Assert.assertEquals(Bytes.toBytes("f")[0], r.get());
55 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
56 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
57
58 r.setPosition(1);
59 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
60
61
62 r.setPosition(0);
63 byte[] dst = new byte[3];
64 r.get(dst);
65 Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);
66
67
68 r.setPosition(3);
69 }
70
71 @Test
72 public void testPutAndGetPrimitiveTypes() throws Exception {
73 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
74 int i1 = 18, i2 = 2;
75 short s1 = 0;
76 long l1 = 1234L;
77 pbr.putInt(i1);
78 pbr.putInt(i2);
79 pbr.putShort(s1);
80 pbr.putLong(l1);
81 pbr.putVLong(0);
82 pbr.putVLong(l1);
83 pbr.putVLong(Long.MAX_VALUE);
84 pbr.putVLong(Long.MIN_VALUE);
85
86 pbr.setPosition(0);
87 Assert.assertEquals(i1, pbr.getInt());
88 Assert.assertEquals(i2, pbr.getInt());
89 Assert.assertEquals(s1, pbr.getShort());
90 Assert.assertEquals(l1, pbr.getLong());
91 Assert.assertEquals(0, pbr.getVLong());
92 Assert.assertEquals(l1, pbr.getVLong());
93 Assert.assertEquals(Long.MAX_VALUE, pbr.getVLong());
94 Assert.assertEquals(Long.MIN_VALUE, pbr.getVLong());
95 }
96
97 @Test
98 public void testPutGetAPIsCompareWithBBAPIs() throws Exception {
99
100 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
101 int i1 = -234, i2 = 2;
102 short s1 = 0;
103 long l1 = 1234L;
104 pbr.putInt(i1);
105 pbr.putShort(s1);
106 pbr.putInt(i2);
107 pbr.putLong(l1);
108
109 pbr.setPosition(0);
110 Assert.assertEquals(i1, pbr.getInt());
111 Assert.assertEquals(s1, pbr.getShort());
112 Assert.assertEquals(i2, pbr.getInt());
113 Assert.assertEquals(l1, pbr.getLong());
114
115 ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes());
116 Assert.assertEquals(i1, bb.getInt());
117 Assert.assertEquals(s1, bb.getShort());
118 Assert.assertEquals(i2, bb.getInt());
119 Assert.assertEquals(l1, bb.getLong());
120 }
121 }