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.Assert.assertTrue;
021
022import java.nio.ByteBuffer;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.nio.ByteBuff;
025import org.apache.hadoop.hbase.nio.MultiByteBuff;
026import org.apache.hadoop.hbase.nio.SingleByteBuff;
027import org.apache.hadoop.hbase.testclassification.MiscTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MiscTests.class, SmallTests.class })
034public class TestByteBuffUtils {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestByteBuffUtils.class);
039
040  @Test
041  public void testCopyAndCompare() throws Exception {
042    ByteBuffer bb1 = ByteBuffer.allocate(50);
043    ByteBuffer bb2 = ByteBuffer.allocate(50);
044    MultiByteBuff src = new MultiByteBuff(bb1, bb2);
045    for (int i = 0; i < 7; i++) {
046      src.putLong(8L);
047    }
048    src.put((byte) 1);
049    src.put((byte) 1);
050    ByteBuffer bb3 = ByteBuffer.allocate(50);
051    ByteBuffer bb4 = ByteBuffer.allocate(50);
052    MultiByteBuff mbbDst = new MultiByteBuff(bb3, bb4);
053    // copy from MBB to MBB
054    mbbDst.put(0, src, 0, 100);
055    int compareTo = ByteBuff.compareTo(src, 0, 100, mbbDst, 0, 100);
056    assertTrue(compareTo == 0);
057    // Copy from MBB to SBB
058    bb3 = ByteBuffer.allocate(100);
059    SingleByteBuff sbbDst = new SingleByteBuff(bb3);
060    src.rewind();
061    sbbDst.put(0, src, 0, 100);
062    compareTo = ByteBuff.compareTo(src, 0, 100, sbbDst, 0, 100);
063    assertTrue(compareTo == 0);
064    // Copy from SBB to SBB
065    bb3 = ByteBuffer.allocate(100);
066    SingleByteBuff sbb = new SingleByteBuff(bb3);
067    for (int i = 0; i < 7; i++) {
068      sbb.putLong(8L);
069    }
070    sbb.put((byte) 1);
071    sbb.put((byte) 1);
072    bb4 = ByteBuffer.allocate(100);
073    sbbDst = new SingleByteBuff(bb4);
074    sbbDst.put(0, sbb, 0, 100);
075    compareTo = ByteBuff.compareTo(sbb, 0, 100, sbbDst, 0, 100);
076    assertTrue(compareTo == 0);
077    // copy from SBB to MBB
078    sbb.rewind();
079    mbbDst = new MultiByteBuff(bb3, bb4);
080    mbbDst.rewind();
081    mbbDst.put(0, sbb, 0, 100);
082    compareTo = ByteBuff.compareTo(sbb, 0, 100, mbbDst, 0, 100);
083    assertTrue(compareTo == 0);
084  }
085}