001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.testclassification.MiscTests;
022import org.apache.hadoop.hbase.testclassification.SmallTests;
023import org.junit.Assert;
024import org.junit.ClassRule;
025import org.junit.Test;
026import org.junit.experimental.categories.Category;
027
028@Category({MiscTests.class, SmallTests.class})
029public class TestSimpleMutableByteRange {
030
031  @ClassRule
032  public static final HBaseClassTestRule CLASS_RULE =
033      HBaseClassTestRule.forClass(TestSimpleMutableByteRange.class);
034
035  @Test
036  public void testEmpty(){
037    Assert.assertTrue(SimpleMutableByteRange.isEmpty(null));
038    ByteRange r = new SimpleMutableByteRange();
039    Assert.assertTrue(SimpleMutableByteRange.isEmpty(r));
040    Assert.assertTrue(r.isEmpty());
041    r.set(new byte[0]);
042    Assert.assertEquals(0, r.getBytes().length);
043    Assert.assertEquals(0, r.getOffset());
044    Assert.assertEquals(0, r.getLength());
045    Assert.assertTrue(Bytes.equals(new byte[0], r.deepCopyToNewArray()));
046    Assert.assertEquals(0, r.compareTo(new SimpleMutableByteRange(new byte[0], 0, 0)));
047    Assert.assertEquals(0, r.hashCode());
048  }
049
050  @Test
051  public void testBasics() {
052    ByteRange r = new SimpleMutableByteRange(new byte[] { 1, 3, 2 });
053    Assert.assertFalse(SimpleMutableByteRange.isEmpty(r));
054    Assert.assertNotNull(r.getBytes());//should be empty byte[], but could change this behavior
055    Assert.assertEquals(3, r.getBytes().length);
056    Assert.assertEquals(0, r.getOffset());
057    Assert.assertEquals(3, r.getLength());
058
059    //cloning (deep copying)
060    Assert.assertTrue(Bytes.equals(new byte[]{1, 3, 2}, r.deepCopyToNewArray()));
061    Assert.assertNotSame(r.getBytes(), r.deepCopyToNewArray());
062
063    //hash code
064    Assert.assertTrue(r.hashCode() > 0);
065    Assert.assertEquals(r.hashCode(), r.deepCopy().hashCode());
066
067    //copying to arrays
068    byte[] destination = new byte[]{-59};//junk
069    r.deepCopySubRangeTo(2, 1, destination, 0);
070    Assert.assertTrue(Bytes.equals(new byte[]{2}, destination));
071
072    //set length
073    r.setLength(1);
074    Assert.assertTrue(Bytes.equals(new byte[]{1}, r.deepCopyToNewArray()));
075    r.setLength(2);//verify we retained the 2nd byte, but dangerous in real code
076    Assert.assertTrue(Bytes.equals(new byte[]{1, 3}, r.deepCopyToNewArray()));
077  }
078
079  @Test
080  public void testPutandGetPrimitiveTypes() throws Exception {
081    ByteRange r = new SimpleMutableByteRange(100);
082    int offset = 0;
083    int i1 = 18, i2 = 2;
084    short s1 = 0;
085    long l1 = 1234L, l2 = 0;
086    r.putInt(offset, i1);
087    offset += Bytes.SIZEOF_INT;
088    r.putInt(offset, i2);
089    offset += Bytes.SIZEOF_INT;
090    r.putShort(offset, s1);
091    offset += Bytes.SIZEOF_SHORT;
092    r.putLong(offset, l1);
093    offset += Bytes.SIZEOF_LONG;
094    int len = r.putVLong(offset, l1);
095    offset += len;
096    len = r.putVLong(offset, l2);
097    offset += len;
098    len = r.putVLong(offset, Long.MAX_VALUE);
099    offset += len;
100    len = r.putVLong(offset, Long.MIN_VALUE);
101
102    offset = 0;
103    Assert.assertEquals(i1, r.getInt(offset));
104    offset += Bytes.SIZEOF_INT;
105    Assert.assertEquals(i2, r.getInt(offset));
106    offset += Bytes.SIZEOF_INT;
107    Assert.assertEquals(s1, r.getShort(offset));
108    offset += Bytes.SIZEOF_SHORT;
109    Assert.assertEquals(l1, r.getLong(offset));
110    offset += Bytes.SIZEOF_LONG;
111    Assert.assertEquals(l1, r.getVLong(offset));
112    offset += SimpleByteRange.getVLongSize(l1);
113    Assert.assertEquals(l2, r.getVLong(offset));
114    offset += SimpleByteRange.getVLongSize(l2);
115    Assert.assertEquals(Long.MAX_VALUE, r.getVLong(offset));
116    offset += SimpleByteRange.getVLongSize(Long.MAX_VALUE);
117    Assert.assertEquals(Long.MIN_VALUE, r.getVLong(offset));
118  }
119}