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.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 TestSimpleByteRange {
27  
28    @Test
29    public void testEmpty(){
30      Assert.assertTrue(SimpleByteRange.isEmpty(null));
31      ByteRange r = new SimpleByteRange();
32      Assert.assertTrue(SimpleByteRange.isEmpty(r));
33      Assert.assertTrue(r.isEmpty());
34      r.set(new byte[0]);
35      Assert.assertEquals(0, r.getBytes().length);
36      Assert.assertEquals(0, r.getOffset());
37      Assert.assertEquals(0, r.getLength());
38      Assert.assertTrue(Bytes.equals(new byte[0], r.deepCopyToNewArray()));
39      Assert.assertEquals(0, r.compareTo(new SimpleByteRange(new byte[0], 0, 0)));
40      Assert.assertEquals(0, r.hashCode());
41    }
42  
43    @Test
44    public void testBasics() {
45      ByteRange r = new SimpleByteRange(new byte[] { 1, 3, 2 });
46      Assert.assertFalse(SimpleByteRange.isEmpty(r));
47      Assert.assertNotNull(r.getBytes());//should be empty byte[], but could change this behavior
48      Assert.assertEquals(3, r.getBytes().length);
49      Assert.assertEquals(0, r.getOffset());
50      Assert.assertEquals(3, r.getLength());
51  
52      //cloning (deep copying)
53      Assert.assertTrue(Bytes.equals(new byte[]{1, 3, 2}, r.deepCopyToNewArray()));
54      Assert.assertNotSame(r.getBytes(), r.deepCopyToNewArray());
55  
56      //hash code
57      Assert.assertTrue(r.hashCode() > 0);
58      Assert.assertEquals(r.hashCode(), r.deepCopy().hashCode());
59  
60      //copying to arrays
61      byte[] destination = new byte[]{-59};//junk
62      r.deepCopySubRangeTo(2, 1, destination, 0);
63      Assert.assertTrue(Bytes.equals(new byte[]{2}, destination));
64  
65      //set length
66      r.setLength(1);
67      Assert.assertTrue(Bytes.equals(new byte[]{1}, r.deepCopyToNewArray()));
68      r.setLength(2);//verify we retained the 2nd byte, but dangerous in real code
69      Assert.assertTrue(Bytes.equals(new byte[]{1, 3}, r.deepCopyToNewArray()));
70    }
71  }