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.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022
023import java.nio.ByteBuffer;
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.jupiter.api.Tag;
027import org.junit.jupiter.api.Test;
028
029@Tag(MiscTests.TAG)
030@Tag(SmallTests.TAG)
031public class TestSimplePositionedMutableByteRange {
032
033  @Test
034  public void testPosition() {
035    PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3);
036
037    // exercise single-byte put
038    r.put(Bytes.toBytes("f")[0]).put(Bytes.toBytes("o")[0]).put(Bytes.toBytes("o")[0]);
039    assertEquals(3, r.getPosition());
040    assertArrayEquals(
041      new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
042      r.getBytes());
043
044    // exercise multi-byte put
045    r.setPosition(0);
046    r.put(Bytes.toBytes("f")).put(Bytes.toBytes("o")).put(Bytes.toBytes("o"));
047    assertEquals(3, r.getPosition());
048    assertArrayEquals(
049      new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 },
050      r.getBytes());
051
052    // exercise single-byte get
053    r.setPosition(0);
054    assertEquals(Bytes.toBytes("f")[0], r.get());
055    assertEquals(Bytes.toBytes("o")[0], r.get());
056    assertEquals(Bytes.toBytes("o")[0], r.get());
057
058    r.setPosition(1);
059    assertEquals(Bytes.toBytes("o")[0], r.get());
060
061    // exercise multi-byte get
062    r.setPosition(0);
063    byte[] dst = new byte[3];
064    r.get(dst);
065    assertArrayEquals(Bytes.toBytes("foo"), dst);
066
067    // set position to the end of the range; this should not throw.
068    r.setPosition(3);
069  }
070
071  @Test
072  public void testPutAndGetPrimitiveTypes() {
073    PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
074    int i1 = 18, i2 = 2;
075    short s1 = 0;
076    long l1 = 1234L;
077    pbr.putInt(i1);
078    pbr.putInt(i2);
079    pbr.putShort(s1);
080    pbr.putLong(l1);
081    pbr.putVLong(0);
082    pbr.putVLong(l1);
083    pbr.putVLong(Long.MAX_VALUE);
084    pbr.putVLong(Long.MIN_VALUE);
085    // rewind
086    pbr.setPosition(0);
087    assertEquals(i1, pbr.getInt());
088    assertEquals(i2, pbr.getInt());
089    assertEquals(s1, pbr.getShort());
090    assertEquals(l1, pbr.getLong());
091    assertEquals(0, pbr.getVLong());
092    assertEquals(l1, pbr.getVLong());
093    assertEquals(Long.MAX_VALUE, pbr.getVLong());
094    assertEquals(Long.MIN_VALUE, pbr.getVLong());
095  }
096
097  @Test
098  public void testPutGetAPIsCompareWithBBAPIs() {
099    // confirm that the long/int/short writing is same as BBs
100    PositionedByteRange pbr = new SimplePositionedMutableByteRange(100);
101    int i1 = -234, i2 = 2;
102    short s1 = 0;
103    long l1 = 1234L;
104    pbr.putInt(i1);
105    pbr.putShort(s1);
106    pbr.putInt(i2);
107    pbr.putLong(l1);
108    // rewind
109    pbr.setPosition(0);
110    assertEquals(i1, pbr.getInt());
111    assertEquals(s1, pbr.getShort());
112    assertEquals(i2, pbr.getInt());
113    assertEquals(l1, pbr.getLong());
114    // Read back using BB APIs
115    ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes());
116    assertEquals(i1, bb.getInt());
117    assertEquals(s1, bb.getShort());
118    assertEquals(i2, bb.getInt());
119    assertEquals(l1, bb.getLong());
120  }
121}