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  package org.apache.hadoop.hbase.util;
19  
20  import java.nio.ByteBuffer;
21  
22  import org.apache.hadoop.hbase.testclassification.SmallTests;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.experimental.categories.Category;
26  
27  @Category(SmallTests.class)
28  public class TestSimplePositionedMutableByteRange {
29    @Test
30    public void testPosition() {
31      PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3);
32  
33      // exercise single-byte put
34      r.put(Bytes.toBytes("f")[0])
35       .put(Bytes.toBytes("o")[0])
36       .put(Bytes.toBytes("o")[0]);
37      Assert.assertEquals(3, r.getPosition());
38      Assert.assertArrayEquals(
39        new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
40        r.getBytes());
41  
42      // exercise multi-byte put
43      r.setPosition(0);
44      r.put(Bytes.toBytes("f"))
45       .put(Bytes.toBytes("o"))
46       .put(Bytes.toBytes("o"));
47      Assert.assertEquals(3, r.getPosition());
48      Assert.assertArrayEquals(
49        new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
50        r.getBytes());
51  
52      // exercise single-byte get
53      r.setPosition(0);
54      Assert.assertEquals(Bytes.toBytes("f")[0], r.get());
55      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
56      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
57  
58      r.setPosition(1);
59      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
60  
61      // exercise multi-byte get
62      r.setPosition(0);
63      byte[] dst = new byte[3];
64      r.get(dst);
65      Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);
66  
67      // set position to the end of the range; this should not throw.
68      r.setPosition(3);
69    }
70  
71    @Test
72    public void testPutAndGetPrimitiveTypes() throws Exception {
73      PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
74      int i1 = 18, i2 = 2;
75      short s1 = 0;
76      long l1 = 1234L;
77      pbr.putInt(i1);
78      pbr.putInt(i2);
79      pbr.putShort(s1);
80      pbr.putLong(l1);
81      pbr.putVLong(0);
82      pbr.putVLong(l1);
83      pbr.putVLong(Long.MAX_VALUE);
84      pbr.putVLong(Long.MIN_VALUE);
85      // rewind
86      pbr.setPosition(0);
87      Assert.assertEquals(i1, pbr.getInt());
88      Assert.assertEquals(i2, pbr.getInt());
89      Assert.assertEquals(s1, pbr.getShort());
90      Assert.assertEquals(l1, pbr.getLong());
91      Assert.assertEquals(0, pbr.getVLong());
92      Assert.assertEquals(l1, pbr.getVLong());
93      Assert.assertEquals(Long.MAX_VALUE, pbr.getVLong());
94      Assert.assertEquals(Long.MIN_VALUE, pbr.getVLong());
95    }
96  
97    @Test
98    public void testPutGetAPIsCompareWithBBAPIs() throws Exception {
99      // confirm that the long/int/short writing is same as BBs
100     PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
101     int i1 = -234, i2 = 2;
102     short s1 = 0;
103     long l1 = 1234L;
104     pbr.putInt(i1);
105     pbr.putShort(s1);
106     pbr.putInt(i2);
107     pbr.putLong(l1);
108     // rewind
109     pbr.setPosition(0);
110     Assert.assertEquals(i1, pbr.getInt());
111     Assert.assertEquals(s1, pbr.getShort());
112     Assert.assertEquals(i2, pbr.getInt());
113     Assert.assertEquals(l1, pbr.getLong());
114     // Read back using BB APIs
115     ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes());
116     Assert.assertEquals(i1, bb.getInt());
117     Assert.assertEquals(s1, bb.getShort());
118     Assert.assertEquals(i2, bb.getInt());
119     Assert.assertEquals(l1, bb.getLong());
120   }
121 }