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.apache.hadoop.hbase.util.Order.ASCENDING;
021import static org.apache.hadoop.hbase.util.Order.DESCENDING;
022import static org.junit.jupiter.api.Assertions.assertArrayEquals;
023
024import java.util.Arrays;
025import java.util.Collections;
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 TestOrder {
034
035  byte[][] VALS = { Bytes.toBytes("foo"), Bytes.toBytes("bar"), Bytes.toBytes("baz") };
036
037  @Test
038  public void testApplyAscending() {
039    byte[][] vals = new byte[VALS.length][];
040    byte[][] ordered = new byte[VALS.length][];
041    for (int i = 0; i < VALS.length; i++) {
042      vals[i] = Arrays.copyOf(VALS[i], VALS[i].length);
043      ordered[i] = Arrays.copyOf(VALS[i], VALS[i].length);
044      ASCENDING.apply(ordered[i]);
045    }
046
047    Arrays.sort(vals, Bytes.BYTES_COMPARATOR);
048    Arrays.sort(ordered, Bytes.BYTES_COMPARATOR);
049
050    for (int i = 0; i < vals.length; i++) {
051      assertArrayEquals(vals[i], ordered[i]);
052    }
053
054    byte[] rangeApply = Arrays.copyOf(VALS[0], VALS[0].length);
055    ASCENDING.apply(rangeApply, 1, 1);
056    assertArrayEquals(VALS[0], rangeApply);
057  }
058
059  @Test
060  public void testApplyDescending() {
061    byte[][] vals = new byte[VALS.length][];
062    byte[][] ordered = new byte[VALS.length][];
063    for (int i = 0; i < VALS.length; i++) {
064      vals[i] = Arrays.copyOf(VALS[i], VALS[i].length);
065      ordered[i] = Arrays.copyOf(VALS[i], VALS[i].length);
066      DESCENDING.apply(ordered[i]);
067    }
068
069    Arrays.sort(vals, Collections.reverseOrder(Bytes.BYTES_COMPARATOR));
070    Arrays.sort(ordered, Bytes.BYTES_COMPARATOR);
071
072    for (int i = 0; i < vals.length; i++) {
073      DESCENDING.apply(ordered[i]);
074      assertArrayEquals(vals[i], ordered[i]);
075    }
076
077    byte[] expected = new byte[] { VALS[0][0], DESCENDING.apply(VALS[0][1]), VALS[0][2] };
078    byte[] rangeApply = Arrays.copyOf(VALS[0], VALS[0].length);
079    DESCENDING.apply(rangeApply, 1, 1);
080    assertArrayEquals(expected, rangeApply);
081  }
082}