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.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022
023import java.util.ArrayList;
024import java.util.Arrays;
025
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({SmallTests.class})
033public class TestByteRangeUtils {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037          HBaseClassTestRule.forClass(TestByteRangeUtils.class);
038
039  @Test
040  public void testNumEqualPrefixBytes() {
041    assertEquals(0, ByteRangeUtils.numEqualPrefixBytes(
042            new SimpleByteRange(new byte[]{1, 2, 3}),
043            new SimpleByteRange(new byte[]{4, 5, 6}), 1));
044    assertEquals(2, ByteRangeUtils.numEqualPrefixBytes(
045            new SimpleByteRange(new byte[]{1, 2, 3}),
046            new SimpleByteRange(new byte[]{0, 1, 2}), 1));
047  }
048
049  @Test
050  public void testCopyToNewArrays() {
051    assertEquals(new ArrayList<>(),
052            ByteRangeUtils.copyToNewArrays(null));
053    assertArrayEquals(new byte[]{1, 2, 3},
054            ByteRangeUtils.copyToNewArrays(new ArrayList<>(Arrays.asList(
055                    new SimpleByteRange(new byte[]{1, 2, 3}),
056                    new SimpleByteRange(new byte[]{4, 5, 6})))).get(0));
057    assertArrayEquals(new byte[]{4, 5, 6},
058            ByteRangeUtils.copyToNewArrays(new ArrayList<>(Arrays.asList(
059                    new SimpleByteRange(new byte[]{1, 2, 3}),
060                    new SimpleByteRange(new byte[]{4, 5, 6})))).get(1));
061  }
062
063  @Test
064  public void testFromArrays() {
065    assertEquals(new ArrayList<>(), ByteRangeUtils.fromArrays(null));
066    assertEquals(new ArrayList<>(Arrays.asList(
067            new SimpleMutableByteRange(new byte[]{1, 2, 3}),
068            new SimpleMutableByteRange(new byte[]{4, 5, 6}))),
069            ByteRangeUtils.fromArrays(new ArrayList<>(
070                    Arrays.asList(new byte[]{1, 2, 3}, new byte[]{4, 5, 6}))));
071  }
072}