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