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