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]).put(Bytes.toBytes("o")[0]).put(Bytes.toBytes("o")[0]);
35 Assert.assertEquals(3, r.getPosition());
36 Assert.assertArrayEquals(
37 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
38 r.getBytes());
39
40
41 r.setPosition(0);
42 r.put(Bytes.toBytes("f")).put(Bytes.toBytes("o")).put(Bytes.toBytes("o"));
43 Assert.assertEquals(3, r.getPosition());
44 Assert.assertArrayEquals(
45 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
46 r.getBytes());
47
48
49 r.setPosition(0);
50 Assert.assertEquals(Bytes.toBytes("f")[0], r.get());
51 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
52 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
53
54 r.setPosition(1);
55 Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
56
57
58 r.setPosition(0);
59 byte[] dst = new byte[3];
60 r.get(dst);
61 Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);
62
63
64 r.setPosition(3);
65 }
66
67 @Test
68 public void testPutAndGetPrimitiveTypes() {
69 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
70 int i1 = 18, i2 = 2;
71 short s1 = 0;
72 long l1 = 1234L;
73 pbr.putInt(i1);
74 pbr.putInt(i2);
75 pbr.putShort(s1);
76 pbr.putLong(l1);
77 pbr.putVLong(0);
78 pbr.putVLong(l1);
79 pbr.putVLong(Long.MAX_VALUE);
80 pbr.putVLong(Long.MIN_VALUE);
81
82 pbr.setPosition(0);
83 Assert.assertEquals(i1, pbr.getInt());
84 Assert.assertEquals(i2, pbr.getInt());
85 Assert.assertEquals(s1, pbr.getShort());
86 Assert.assertEquals(l1, pbr.getLong());
87 Assert.assertEquals(0, pbr.getVLong());
88 Assert.assertEquals(l1, pbr.getVLong());
89 Assert.assertEquals(Long.MAX_VALUE, pbr.getVLong());
90 Assert.assertEquals(Long.MIN_VALUE, pbr.getVLong());
91 }
92
93 @Test
94 public void testPutGetAPIsCompareWithBBAPIs() {
95
96 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
97 int i1 = -234, i2 = 2;
98 short s1 = 0;
99 long l1 = 1234L;
100 pbr.putInt(i1);
101 pbr.putShort(s1);
102 pbr.putInt(i2);
103 pbr.putLong(l1);
104
105 pbr.setPosition(0);
106 Assert.assertEquals(i1, pbr.getInt());
107 Assert.assertEquals(s1, pbr.getShort());
108 Assert.assertEquals(i2, pbr.getInt());
109 Assert.assertEquals(l1, pbr.getLong());
110
111 ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes());
112 Assert.assertEquals(i1, bb.getInt());
113 Assert.assertEquals(s1, bb.getShort());
114 Assert.assertEquals(i2, bb.getInt());
115 Assert.assertEquals(l1, bb.getLong());
116 }
117 }