View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.util.vint;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.util.Random;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.hbase.util.number.RandomNumberUtils;
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 TestVLongTool {
33  
34    @Test
35    public void testNumBytes() {
36      Assert.assertEquals(1, UVLongTool.numBytes(0));
37      Assert.assertEquals(1, UVLongTool.numBytes(1));
38      Assert.assertEquals(1, UVLongTool.numBytes(100));
39      Assert.assertEquals(1, UVLongTool.numBytes(126));
40      Assert.assertEquals(1, UVLongTool.numBytes(127));
41      Assert.assertEquals(2, UVLongTool.numBytes(128));
42      Assert.assertEquals(2, UVLongTool.numBytes(129));
43      Assert.assertEquals(9, UVLongTool.numBytes(Long.MAX_VALUE));
44    }
45  
46    @Test
47    public void testToBytes() {
48      Assert.assertArrayEquals(new byte[] { 0 }, UVLongTool.getBytes(0));
49      Assert.assertArrayEquals(new byte[] { 1 }, UVLongTool.getBytes(1));
50      Assert.assertArrayEquals(new byte[] { 63 }, UVLongTool.getBytes(63));
51      Assert.assertArrayEquals(new byte[] { 127 }, UVLongTool.getBytes(127));
52      Assert.assertArrayEquals(new byte[] { -128, 1 }, UVLongTool.getBytes(128));
53      Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, UVLongTool.getBytes(155));
54      Assert.assertArrayEquals(UVLongTool.MAX_VALUE_BYTES, UVLongTool.getBytes(Long.MAX_VALUE));
55    }
56  
57    @Test
58    public void testFromBytes() {
59      Assert.assertEquals(Long.MAX_VALUE, UVLongTool.getLong(UVLongTool.MAX_VALUE_BYTES));
60    }
61  
62    @Test
63    public void testFromBytesOffset() {
64      Assert.assertEquals(Long.MAX_VALUE, UVLongTool.getLong(UVLongTool.MAX_VALUE_BYTES, 0));
65  
66      long ms = 1318966363481L;
67  //    System.out.println(ms);
68      byte[] bytes = UVLongTool.getBytes(ms);
69  //    System.out.println(Arrays.toString(bytes));
70      long roundTripped = UVLongTool.getLong(bytes, 0);
71      Assert.assertEquals(ms, roundTripped);
72  
73      int calculatedNumBytes = UVLongTool.numBytes(ms);
74      int actualNumBytes = bytes.length;
75      Assert.assertEquals(actualNumBytes, calculatedNumBytes);
76  
77      byte[] shiftedBytes = new byte[1000];
78      int shift = 33;
79      System.arraycopy(bytes, 0, shiftedBytes, shift, bytes.length);
80      long shiftedRoundTrip = UVLongTool.getLong(shiftedBytes, shift);
81      Assert.assertEquals(ms, shiftedRoundTrip);
82    }
83  
84    @Test
85    public void testRoundTrips() {
86      Random random = new Random();
87      for (int i = 0; i < 10000; ++i) {
88        long value = RandomNumberUtils.nextPositiveLong(random);
89        byte[] bytes = UVLongTool.getBytes(value);
90        long roundTripped = UVLongTool.getLong(bytes);
91        Assert.assertEquals(value, roundTripped);
92        int calculatedNumBytes = UVLongTool.numBytes(value);
93        int actualNumBytes = bytes.length;
94        Assert.assertEquals(actualNumBytes, calculatedNumBytes);
95      }
96    }
97  
98    @Test
99    public void testInputStreams() throws IOException {
100     ByteArrayInputStream is;
101     is = new ByteArrayInputStream(new byte[] { 0 });
102     Assert.assertEquals(0, UVLongTool.getLong(is));
103     is = new ByteArrayInputStream(new byte[] { 5 });
104     Assert.assertEquals(5, UVLongTool.getLong(is));
105     is = new ByteArrayInputStream(new byte[] { -128 + 27, 1 });
106     Assert.assertEquals(155, UVLongTool.getLong(is));
107   }
108 }