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 org.apache.hadoop.hbase.testclassification.SmallTests;
21  import org.junit.Assert;
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  @Category(SmallTests.class)
26  public class TestPositionedByteRange {
27    @Test
28    public void testPosition() {
29      PositionedByteRange r = new SimplePositionedByteRange(new byte[5], 1, 3);
30  
31      // exercise single-byte put
32      r.put(Bytes.toBytes("f")[0])
33       .put(Bytes.toBytes("o")[0])
34       .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"))
43       .put(Bytes.toBytes("o"))
44       .put(Bytes.toBytes("o"));
45      Assert.assertEquals(3, r.getPosition());
46      Assert.assertArrayEquals(
47        new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
48        r.getBytes());
49  
50      // exercise single-byte get
51      r.setPosition(0);
52      Assert.assertEquals(Bytes.toBytes("f")[0], r.get());
53      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
54      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
55  
56      r.setPosition(1);
57      Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
58  
59      // exercise multi-byte get
60      r.setPosition(0);
61      byte[] dst = new byte[3];
62      r.get(dst);
63      Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);
64  
65      // set position to the end of the range; this should not throw.
66      r.setPosition(3);
67    }
68  }