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