1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util.vint;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Random;
25
26 import org.apache.hadoop.hbase.SmallTests;
27 import org.apache.hadoop.hbase.util.vint.UVIntTool;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31
32 @Category(SmallTests.class)
33 public class TestVIntTool {
34
35 @Test
36 public void testNumBytes() {
37 Assert.assertEquals(1, UVIntTool.numBytes(0));
38 Assert.assertEquals(1, UVIntTool.numBytes(1));
39 Assert.assertEquals(1, UVIntTool.numBytes(100));
40 Assert.assertEquals(1, UVIntTool.numBytes(126));
41 Assert.assertEquals(1, UVIntTool.numBytes(127));
42 Assert.assertEquals(2, UVIntTool.numBytes(128));
43 Assert.assertEquals(2, UVIntTool.numBytes(129));
44 Assert.assertEquals(5, UVIntTool.numBytes(Integer.MAX_VALUE));
45 }
46
47 @Test
48 public void testWriteBytes() throws IOException {
49 Assert.assertArrayEquals(new byte[] { 0 }, bytesViaOutputStream(0));
50 Assert.assertArrayEquals(new byte[] { 1 }, bytesViaOutputStream(1));
51 Assert.assertArrayEquals(new byte[] { 63 }, bytesViaOutputStream(63));
52 Assert.assertArrayEquals(new byte[] { 127 }, bytesViaOutputStream(127));
53 Assert.assertArrayEquals(new byte[] { -128, 1 }, bytesViaOutputStream(128));
54 Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, bytesViaOutputStream(155));
55 Assert.assertArrayEquals(UVIntTool.MAX_VALUE_BYTES, bytesViaOutputStream(Integer.MAX_VALUE));
56 }
57
58 private byte[] bytesViaOutputStream(int value) throws IOException {
59 ByteArrayOutputStream os = new ByteArrayOutputStream();
60 UVIntTool.writeBytes(value, os);
61 return os.toByteArray();
62 }
63
64 @Test
65 public void testToBytes() {
66 Assert.assertArrayEquals(new byte[] { 0 }, UVIntTool.getBytes(0));
67 Assert.assertArrayEquals(new byte[] { 1 }, UVIntTool.getBytes(1));
68 Assert.assertArrayEquals(new byte[] { 63 }, UVIntTool.getBytes(63));
69 Assert.assertArrayEquals(new byte[] { 127 }, UVIntTool.getBytes(127));
70 Assert.assertArrayEquals(new byte[] { -128, 1 }, UVIntTool.getBytes(128));
71 Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, UVIntTool.getBytes(155));
72 Assert.assertArrayEquals(UVIntTool.MAX_VALUE_BYTES, UVIntTool.getBytes(Integer.MAX_VALUE));
73 }
74
75 @Test
76 public void testFromBytes() {
77 Assert.assertEquals(Integer.MAX_VALUE, UVIntTool.getInt(UVIntTool.MAX_VALUE_BYTES));
78 }
79
80 @Test
81 public void testRoundTrips() {
82 Random random = new Random();
83 for (int i = 0; i < 10000; ++i) {
84 int value = random.nextInt(Integer.MAX_VALUE);
85 byte[] bytes = UVIntTool.getBytes(value);
86 int roundTripped = UVIntTool.getInt(bytes);
87 Assert.assertEquals(value, roundTripped);
88 }
89 }
90
91 @Test
92 public void testInputStreams() throws IOException {
93 ByteArrayInputStream is;
94 is = new ByteArrayInputStream(new byte[] { 0 });
95 Assert.assertEquals(0, UVIntTool.getInt(is));
96 is = new ByteArrayInputStream(new byte[] { 5 });
97 Assert.assertEquals(5, UVIntTool.getInt(is));
98 is = new ByteArrayInputStream(new byte[] { -128 + 27, 1 });
99 Assert.assertEquals(155, UVIntTool.getInt(is));
100 }
101
102 }