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