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.util.number.RandomNumberUtils;
26  import org.apache.hadoop.hbase.util.vint.UVLongTool;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  public class TestVLongTool {
31  
32    @Test
33    public void testNumBytes() {
34      Assert.assertEquals(1, UVLongTool.numBytes(0));
35      Assert.assertEquals(1, UVLongTool.numBytes(1));
36      Assert.assertEquals(1, UVLongTool.numBytes(100));
37      Assert.assertEquals(1, UVLongTool.numBytes(126));
38      Assert.assertEquals(1, UVLongTool.numBytes(127));
39      Assert.assertEquals(2, UVLongTool.numBytes(128));
40      Assert.assertEquals(2, UVLongTool.numBytes(129));
41      Assert.assertEquals(9, UVLongTool.numBytes(Long.MAX_VALUE));
42    }
43  
44    @Test
45    public void testToBytes() {
46      Assert.assertArrayEquals(new byte[] { 0 }, UVLongTool.getBytes(0));
47      Assert.assertArrayEquals(new byte[] { 1 }, UVLongTool.getBytes(1));
48      Assert.assertArrayEquals(new byte[] { 63 }, UVLongTool.getBytes(63));
49      Assert.assertArrayEquals(new byte[] { 127 }, UVLongTool.getBytes(127));
50      Assert.assertArrayEquals(new byte[] { -128, 1 }, UVLongTool.getBytes(128));
51      Assert.assertArrayEquals(new byte[] { -128 + 27, 1 }, UVLongTool.getBytes(155));
52      Assert.assertArrayEquals(UVLongTool.MAX_VALUE_BYTES, UVLongTool.getBytes(Long.MAX_VALUE));
53    }
54  
55    @Test
56    public void testFromBytes() {
57      Assert.assertEquals(Long.MAX_VALUE, UVLongTool.getLong(UVLongTool.MAX_VALUE_BYTES));
58    }
59  
60    @Test
61    public void testFromBytesOffset() {
62      Assert.assertEquals(Long.MAX_VALUE, UVLongTool.getLong(UVLongTool.MAX_VALUE_BYTES, 0));
63  
64      long ms = 1318966363481L;
65  //    System.out.println(ms);
66      byte[] bytes = UVLongTool.getBytes(ms);
67  //    System.out.println(Arrays.toString(bytes));
68      long roundTripped = UVLongTool.getLong(bytes, 0);
69      Assert.assertEquals(ms, roundTripped);
70  
71      int calculatedNumBytes = UVLongTool.numBytes(ms);
72      int actualNumBytes = bytes.length;
73      Assert.assertEquals(actualNumBytes, calculatedNumBytes);
74  
75      byte[] shiftedBytes = new byte[1000];
76      int shift = 33;
77      System.arraycopy(bytes, 0, shiftedBytes, shift, bytes.length);
78      long shiftedRoundTrip = UVLongTool.getLong(shiftedBytes, shift);
79      Assert.assertEquals(ms, shiftedRoundTrip);
80    }
81  
82    @Test
83    public void testRoundTrips() {
84      Random random = new Random();
85      for (int i = 0; i < 10000; ++i) {
86        long value = RandomNumberUtils.nextPositiveLong(random);
87        byte[] bytes = UVLongTool.getBytes(value);
88        long roundTripped = UVLongTool.getLong(bytes);
89        Assert.assertEquals(value, roundTripped);
90        int calculatedNumBytes = UVLongTool.numBytes(value);
91        int actualNumBytes = bytes.length;
92        Assert.assertEquals(actualNumBytes, calculatedNumBytes);
93      }
94    }
95  
96    @Test
97    public void testInputStreams() throws IOException {
98      ByteArrayInputStream is;
99      is = new ByteArrayInputStream(new byte[] { 0 });
100     Assert.assertEquals(0, UVLongTool.getLong(is));
101     is = new ByteArrayInputStream(new byte[] { 5 });
102     Assert.assertEquals(5, UVLongTool.getLong(is));
103     is = new ByteArrayInputStream(new byte[] { -128 + 27, 1 });
104     Assert.assertEquals(155, UVLongTool.getLong(is));
105   }
106 }