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