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.ByteArrayOutputStream;
22 import java.io.IOException;
23
24 import org.apache.hadoop.hbase.testclassification.SmallTests;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.junit.experimental.categories.Category;
28
29
30
31 @Category(SmallTests.class)
32 public class TestFIntTool {
33 @Test
34 public void testLeadingZeros() {
35 Assert.assertEquals(64, Long.numberOfLeadingZeros(0));
36 Assert.assertEquals(63, Long.numberOfLeadingZeros(1));
37 Assert.assertEquals(0, Long.numberOfLeadingZeros(Long.MIN_VALUE));
38 Assert.assertEquals(0, Long.numberOfLeadingZeros(-1));
39 Assert.assertEquals(1, Long.numberOfLeadingZeros(Long.MAX_VALUE));
40 Assert.assertEquals(1, Long.numberOfLeadingZeros(Long.MAX_VALUE - 1));
41 }
42
43 @Test
44 public void testMaxValueForNumBytes() {
45 Assert.assertEquals(255, UFIntTool.maxValueForNumBytes(1));
46 Assert.assertEquals(65535, UFIntTool.maxValueForNumBytes(2));
47 Assert.assertEquals(0xffffff, UFIntTool.maxValueForNumBytes(3));
48 Assert.assertEquals(0xffffffffffffffL, UFIntTool.maxValueForNumBytes(7));
49 }
50
51 @Test
52 public void testNumBytes() {
53 Assert.assertEquals(1, UFIntTool.numBytes(0));
54 Assert.assertEquals(1, UFIntTool.numBytes(1));
55 Assert.assertEquals(1, UFIntTool.numBytes(255));
56 Assert.assertEquals(2, UFIntTool.numBytes(256));
57 Assert.assertEquals(2, UFIntTool.numBytes(65535));
58 Assert.assertEquals(3, UFIntTool.numBytes(65536));
59 Assert.assertEquals(4, UFIntTool.numBytes(0xffffffffL));
60 Assert.assertEquals(5, UFIntTool.numBytes(0x100000000L));
61 Assert.assertEquals(4, UFIntTool.numBytes(Integer.MAX_VALUE));
62 Assert.assertEquals(8, UFIntTool.numBytes(Long.MAX_VALUE));
63 Assert.assertEquals(8, UFIntTool.numBytes(Long.MAX_VALUE - 1));
64 }
65
66 @Test
67 public void testGetBytes() {
68 Assert.assertArrayEquals(new byte[] { 0 }, UFIntTool.getBytes(1, 0));
69 Assert.assertArrayEquals(new byte[] { 1 }, UFIntTool.getBytes(1, 1));
70 Assert.assertArrayEquals(new byte[] { -1 }, UFIntTool.getBytes(1, 255));
71 Assert.assertArrayEquals(new byte[] { 1, 0 }, UFIntTool.getBytes(2, 256));
72 Assert.assertArrayEquals(new byte[] { 1, 3 }, UFIntTool.getBytes(2, 256 + 3));
73 Assert.assertArrayEquals(new byte[] { 1, -128 }, UFIntTool.getBytes(2, 256 + 128));
74 Assert.assertArrayEquals(new byte[] { 1, -1 }, UFIntTool.getBytes(2, 256 + 255));
75 Assert.assertArrayEquals(new byte[] { 127, -1, -1, -1 },
76 UFIntTool.getBytes(4, Integer.MAX_VALUE));
77 Assert.assertArrayEquals(new byte[] { 127, -1, -1, -1, -1, -1, -1, -1 },
78 UFIntTool.getBytes(8, Long.MAX_VALUE));
79 }
80
81 @Test
82 public void testFromBytes() {
83 Assert.assertEquals(0, UFIntTool.fromBytes(new byte[] { 0 }));
84 Assert.assertEquals(1, UFIntTool.fromBytes(new byte[] { 1 }));
85 Assert.assertEquals(255, UFIntTool.fromBytes(new byte[] { -1 }));
86 Assert.assertEquals(256, UFIntTool.fromBytes(new byte[] { 1, 0 }));
87 Assert.assertEquals(256 + 3, UFIntTool.fromBytes(new byte[] { 1, 3 }));
88 Assert.assertEquals(256 + 128, UFIntTool.fromBytes(new byte[] { 1, -128 }));
89 Assert.assertEquals(256 + 255, UFIntTool.fromBytes(new byte[] { 1, -1 }));
90 Assert.assertEquals(Integer.MAX_VALUE, UFIntTool.fromBytes(new byte[] { 127, -1, -1, -1 }));
91 Assert.assertEquals(Long.MAX_VALUE,
92 UFIntTool.fromBytes(new byte[] { 127, -1, -1, -1, -1, -1, -1, -1 }));
93 }
94
95 @Test
96 public void testRoundTrips() {
97 long[] values = new long[] { 0, 1, 2, 255, 256, 31123, 65535, 65536, 65537, 0xfffffeL,
98 0xffffffL, 0x1000000L, 0x1000001L, Integer.MAX_VALUE - 1, Integer.MAX_VALUE,
99 (long) Integer.MAX_VALUE + 1, Long.MAX_VALUE - 1, Long.MAX_VALUE };
100 for (int i = 0; i < values.length; ++i) {
101 Assert.assertEquals(values[i], UFIntTool.fromBytes(UFIntTool.getBytes(8, values[i])));
102 }
103 }
104
105 @Test
106 public void testWriteBytes() throws IOException {
107 Assert.assertArrayEquals(new byte[] { 0 }, bytesViaOutputStream(1, 0));
108 Assert.assertArrayEquals(new byte[] { 1 }, bytesViaOutputStream(1, 1));
109 Assert.assertArrayEquals(new byte[] { -1 }, bytesViaOutputStream(1, 255));
110 Assert.assertArrayEquals(new byte[] { 1, 0 }, bytesViaOutputStream(2, 256));
111 Assert.assertArrayEquals(new byte[] { 1, 3 }, bytesViaOutputStream(2, 256 + 3));
112 Assert.assertArrayEquals(new byte[] { 1, -128 }, bytesViaOutputStream(2, 256 + 128));
113 Assert.assertArrayEquals(new byte[] { 1, -1 }, bytesViaOutputStream(2, 256 + 255));
114 Assert.assertArrayEquals(new byte[] { 127, -1, -1, -1 },
115 bytesViaOutputStream(4, Integer.MAX_VALUE));
116 Assert.assertArrayEquals(new byte[] { 127, -1, -1, -1, -1, -1, -1, -1 },
117 bytesViaOutputStream(8, Long.MAX_VALUE));
118 }
119
120 private byte[] bytesViaOutputStream(int outputWidth, long value) throws IOException {
121 ByteArrayOutputStream os = new ByteArrayOutputStream();
122 UFIntTool.writeBytes(outputWidth, value, os);
123 return os.toByteArray();
124 }
125 }