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 static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertNotSame;
024import static org.junit.jupiter.api.Assertions.assertTrue;
025
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031@Tag(MiscTests.TAG)
032@Tag(SmallTests.TAG)
033public class TestSimpleMutableByteRange {
034
035  @Test
036  public void testEmpty() {
037    assertTrue(SimpleMutableByteRange.isEmpty(null));
038    ByteRange r = new SimpleMutableByteRange();
039    assertTrue(SimpleMutableByteRange.isEmpty(r));
040    assertTrue(r.isEmpty());
041    r.set(new byte[0]);
042    assertEquals(0, r.getBytes().length);
043    assertEquals(0, r.getOffset());
044    assertEquals(0, r.getLength());
045    assertTrue(Bytes.equals(new byte[0], r.deepCopyToNewArray()));
046    assertEquals(0, r.compareTo(new SimpleMutableByteRange(new byte[0], 0, 0)));
047    assertEquals(0, r.hashCode());
048  }
049
050  @Test
051  public void testBasics() {
052    ByteRange r = new SimpleMutableByteRange(new byte[] { 1, 3, 2 });
053    assertFalse(SimpleMutableByteRange.isEmpty(r));
054    assertNotNull(r.getBytes());// should be empty byte[], but could change this behavior
055    assertEquals(3, r.getBytes().length);
056    assertEquals(0, r.getOffset());
057    assertEquals(3, r.getLength());
058
059    // cloning (deep copying)
060    assertTrue(Bytes.equals(new byte[] { 1, 3, 2 }, r.deepCopyToNewArray()));
061    assertNotSame(r.getBytes(), r.deepCopyToNewArray());
062
063    // hash code
064    assertTrue(r.hashCode() > 0);
065    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    assertTrue(Bytes.equals(new byte[] { 2 }, destination));
071
072    // set length
073    r.setLength(1);
074    assertTrue(Bytes.equals(new byte[] { 1 }, r.deepCopyToNewArray()));
075    r.setLength(2);// verify we retained the 2nd byte, but dangerous in real code
076    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    r.putVLong(offset, Long.MIN_VALUE);
101
102    offset = 0;
103    assertEquals(i1, r.getInt(offset));
104    offset += Bytes.SIZEOF_INT;
105    assertEquals(i2, r.getInt(offset));
106    offset += Bytes.SIZEOF_INT;
107    assertEquals(s1, r.getShort(offset));
108    offset += Bytes.SIZEOF_SHORT;
109    assertEquals(l1, r.getLong(offset));
110    offset += Bytes.SIZEOF_LONG;
111    assertEquals(l1, r.getVLong(offset));
112    offset += SimpleByteRange.getVLongSize(l1);
113    assertEquals(l2, r.getVLong(offset));
114    offset += SimpleByteRange.getVLongSize(l2);
115    assertEquals(Long.MAX_VALUE, r.getVLong(offset));
116    offset += SimpleByteRange.getVLongSize(Long.MAX_VALUE);
117    assertEquals(Long.MIN_VALUE, r.getVLong(offset));
118  }
119}