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 org.apache.hadoop.hbase.testclassification.SmallTests;
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.junit.experimental.categories.Category;
24
25 @Category(SmallTests.class)
26 public class TestSimpleMutableByteRange {
27
28 @Test
29 public void testEmpty(){
30 Assert.assertTrue(SimpleMutableByteRange.isEmpty(null));
31 ByteRange r = new SimpleMutableByteRange();
32 Assert.assertTrue(SimpleMutableByteRange.isEmpty(r));
33 Assert.assertTrue(r.isEmpty());
34 r.set(new byte[0]);
35 Assert.assertEquals(0, r.getBytes().length);
36 Assert.assertEquals(0, r.getOffset());
37 Assert.assertEquals(0, r.getLength());
38 Assert.assertTrue(Bytes.equals(new byte[0], r.deepCopyToNewArray()));
39 Assert.assertEquals(0, r.compareTo(new SimpleMutableByteRange(new byte[0], 0, 0)));
40 Assert.assertEquals(0, r.hashCode());
41 }
42
43 @Test
44 public void testBasics() {
45 ByteRange r = new SimpleMutableByteRange(new byte[] { 1, 3, 2 });
46 Assert.assertFalse(SimpleMutableByteRange.isEmpty(r));
47 Assert.assertNotNull(r.getBytes());
48 Assert.assertEquals(3, r.getBytes().length);
49 Assert.assertEquals(0, r.getOffset());
50 Assert.assertEquals(3, r.getLength());
51
52
53 Assert.assertTrue(Bytes.equals(new byte[]{1, 3, 2}, r.deepCopyToNewArray()));
54 Assert.assertNotSame(r.getBytes(), r.deepCopyToNewArray());
55
56
57 Assert.assertTrue(r.hashCode() > 0);
58 Assert.assertEquals(r.hashCode(), r.deepCopy().hashCode());
59
60
61 byte[] destination = new byte[]{-59};
62 r.deepCopySubRangeTo(2, 1, destination, 0);
63 Assert.assertTrue(Bytes.equals(new byte[]{2}, destination));
64
65
66 r.setLength(1);
67 Assert.assertTrue(Bytes.equals(new byte[]{1}, r.deepCopyToNewArray()));
68 r.setLength(2);
69 Assert.assertTrue(Bytes.equals(new byte[]{1, 3}, r.deepCopyToNewArray()));
70 }
71
72 @Test
73 public void testPutandGetPrimitiveTypes() throws Exception {
74 ByteRange r = new SimpleMutableByteRange(100);
75 int offset = 0;
76 int i1 = 18, i2 = 2;
77 short s1 = 0;
78 long l1 = 1234L, l2 = 0;
79 r.putInt(offset, i1);
80 offset += Bytes.SIZEOF_INT;
81 r.putInt(offset, i2);
82 offset += Bytes.SIZEOF_INT;
83 r.putShort(offset, s1);
84 offset += Bytes.SIZEOF_SHORT;
85 r.putLong(offset, l1);
86 offset += Bytes.SIZEOF_LONG;
87 int len = r.putVLong(offset, l1);
88 offset += len;
89 len = r.putVLong(offset, l2);
90 offset += len;
91 len = r.putVLong(offset, Long.MAX_VALUE);
92 offset += len;
93 len = r.putVLong(offset, Long.MIN_VALUE);
94
95 offset = 0;
96 Assert.assertEquals(i1, r.getInt(offset));
97 offset += Bytes.SIZEOF_INT;
98 Assert.assertEquals(i2, r.getInt(offset));
99 offset += Bytes.SIZEOF_INT;
100 Assert.assertEquals(s1, r.getShort(offset));
101 offset += Bytes.SIZEOF_SHORT;
102 Assert.assertEquals(l1, r.getLong(offset));
103 offset += Bytes.SIZEOF_LONG;
104 Assert.assertEquals(l1, r.getVLong(offset));
105 offset += SimpleByteRange.getVLongSize(l1);
106 Assert.assertEquals(l2, r.getVLong(offset));
107 offset += SimpleByteRange.getVLongSize(l2);
108 Assert.assertEquals(Long.MAX_VALUE, r.getVLong(offset));
109 offset += SimpleByteRange.getVLongSize(Long.MAX_VALUE);
110 Assert.assertEquals(Long.MIN_VALUE, r.getVLong(offset));
111 }
112 }