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