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 TestSimpleMutableByteRange {
27  
28    @Test
29    public void testEmpty(){
30      Assert.assertTrue(SimpleMutableByteRange.isEmpty(null));
31      ByteRange r = new SimpleMutableByteRange();
32      Assert.assertTrue(SimpleMutableByteRange.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 SimpleMutableByteRange(new byte[0], 0, 0)));
40      Assert.assertEquals(0, r.hashCode());
41    }
42  
43    @Test
44    public void testBasics() {
45      ByteRange r = new SimpleMutableByteRange(new byte[] { 1, 3, 2 });
46      Assert.assertFalse(SimpleMutableByteRange.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    
72    @Test
73    public void testPutandGetPrimitiveTypes() throws Exception {
74      ByteRange r = new SimpleMutableByteRange(100);
75      int offset = 0;
76      int i1 = 18, i2 = 2;
77      short s1 = 0;
78      long l1 = 1234L, l2 = 0;
79      r.putInt(offset, i1);
80      offset += Bytes.SIZEOF_INT;
81      r.putInt(offset, i2);
82      offset += Bytes.SIZEOF_INT;
83      r.putShort(offset, s1);
84      offset += Bytes.SIZEOF_SHORT;
85      r.putLong(offset, l1);
86      offset += Bytes.SIZEOF_LONG;
87      int len = r.putVLong(offset, l1);
88      offset += len;
89      len = r.putVLong(offset, l2);
90      offset += len;
91      len = r.putVLong(offset, Long.MAX_VALUE);
92      offset += len;
93      len = r.putVLong(offset, Long.MIN_VALUE);
94  
95      offset = 0;
96      Assert.assertEquals(i1, r.getInt(offset));
97      offset += Bytes.SIZEOF_INT;
98      Assert.assertEquals(i2, r.getInt(offset));
99      offset += Bytes.SIZEOF_INT;
100     Assert.assertEquals(s1, r.getShort(offset));
101     offset += Bytes.SIZEOF_SHORT;
102     Assert.assertEquals(l1, r.getLong(offset));
103     offset += Bytes.SIZEOF_LONG;
104     Assert.assertEquals(l1, r.getVLong(offset));
105     offset += SimpleByteRange.getVLongSize(l1);
106     Assert.assertEquals(l2, r.getVLong(offset));
107     offset += SimpleByteRange.getVLongSize(l2);
108     Assert.assertEquals(Long.MAX_VALUE, r.getVLong(offset));
109     offset += SimpleByteRange.getVLongSize(Long.MAX_VALUE);
110     Assert.assertEquals(Long.MIN_VALUE, r.getVLong(offset));
111   }
112 }