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.util.vint.UVIntTool;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30 public class TestVIntTool {
31
32 @Test
33 public void testNumBytes() {
34 Assert.assertEquals(1, UVIntTool.numBytes(0));
35 Assert.assertEquals(1, UVIntTool.numBytes(1));
36 Assert.assertEquals(1, UVIntTool.numBytes(100));
37 Assert.assertEquals(1, UVIntTool.numBytes(126));
38 Assert.assertEquals(1, UVIntTool.numBytes(127));
39 Assert.assertEquals(2, UVIntTool.numBytes(128));
40 Assert.assertEquals(2, UVIntTool.numBytes(129));
41 Assert.assertEquals(5, UVIntTool.numBytes(Integer.MAX_VALUE));
42 }
43
44 @Test
45 public void testWriteBytes() throws IOException {
46 Assert.assertArrayEquals(new byte[] { 0 }, bytesViaOutputStream(0));
47 Assert.assertArrayEquals(new byte[] { 1 }, bytesViaOutputStream(1));
48 Assert.assertArrayEquals(new byte[] { 63 }, bytesViaOutputStream(63));
49 Assert.assertArrayEquals(new byte[] { 127 }, bytesViaOutputStream(127));
50 Assert.assertArrayEquals(new byte[] { -128, 1 }, bytesViaOutputStream(128));
51 Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, bytesViaOutputStream(155));
52 Assert.assertArrayEquals(UVIntTool.MAX_VALUE_BYTES, bytesViaOutputStream(Integer.MAX_VALUE));
53 }
54
55 private byte[] bytesViaOutputStream(int value) throws IOException {
56 ByteArrayOutputStream os = new ByteArrayOutputStream();
57 UVIntTool.writeBytes(value, os);
58 return os.toByteArray();
59 }
60
61 @Test
62 public void testToBytes() {
63 Assert.assertArrayEquals(new byte[] { 0 }, UVIntTool.getBytes(0));
64 Assert.assertArrayEquals(new byte[] { 1 }, UVIntTool.getBytes(1));
65 Assert.assertArrayEquals(new byte[] { 63 }, UVIntTool.getBytes(63));
66 Assert.assertArrayEquals(new byte[] { 127 }, UVIntTool.getBytes(127));
67 Assert.assertArrayEquals(new byte[] { -128, 1 }, UVIntTool.getBytes(128));
68 Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, UVIntTool.getBytes(155));
69 Assert.assertArrayEquals(UVIntTool.MAX_VALUE_BYTES, UVIntTool.getBytes(Integer.MAX_VALUE));
70 }
71
72 @Test
73 public void testFromBytes() {
74 Assert.assertEquals(Integer.MAX_VALUE, UVIntTool.getInt(UVIntTool.MAX_VALUE_BYTES));
75 }
76
77 @Test
78 public void testRoundTrips() {
79 Random random = new Random();
80 for (int i = 0; i < 10000; ++i) {
81 int value = random.nextInt(Integer.MAX_VALUE);
82 byte[] bytes = UVIntTool.getBytes(value);
83 int roundTripped = UVIntTool.getInt(bytes);
84 Assert.assertEquals(value, roundTripped);
85 }
86 }
87
88 @Test
89 public void testInputStreams() throws IOException {
90 ByteArrayInputStream is;
91 is = new ByteArrayInputStream(new byte[] { 0 });
92 Assert.assertEquals(0, UVIntTool.getInt(is));
93 is = new ByteArrayInputStream(new byte[] { 5 });
94 Assert.assertEquals(5, UVIntTool.getInt(is));
95 is = new ByteArrayInputStream(new byte[] { -128 + 27, 1 });
96 Assert.assertEquals(155, UVIntTool.getInt(is));
97 }
98
99 }