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