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.filter;
019
020import static org.junit.Assert.assertEquals;
021
022import java.nio.ByteBuffer;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.FilterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030/**
031 * Tests for the bit comparator
032 */
033@Category({ FilterTests.class, SmallTests.class })
034public class TestBitComparator {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestBitComparator.class);
039
040  private static byte[] zeros = new byte[] { 0, 0, 0, 0, 0, 0 };
041  private static ByteBuffer zeros_bb = ByteBuffer.wrap(zeros);
042  private static byte[] ones = new byte[] { 1, 1, 1, 1, 1, 1 };
043  private static ByteBuffer ones_bb = ByteBuffer.wrap(ones);
044  private static byte[] data0 = new byte[] { 0, 1, 2, 4, 8, 15 };
045  private static byte[] data1 = new byte[] { 15, 0, 0, 0, 0, 0 };
046  private static ByteBuffer data1_bb = ByteBuffer.wrap(data1);
047  private static byte[] data2 = new byte[] { 0, 0, 0, 0, 0, 15 };
048  private static ByteBuffer data2_bb = ByteBuffer.wrap(data2);
049  private static byte[] data3 = new byte[] { 15, 15, 15, 15, 15 };
050
051  // data for testing compareTo method with offset and length parameters
052  private static byte[] data1_2 = new byte[] { 15, 15, 0, 0, 0, 0, 0, 15 };
053  private static ByteBuffer data1_2_bb = ByteBuffer.wrap(data1_2);
054  private static byte[] data2_2 = new byte[] { 15, 0, 0, 0, 0, 0, 15, 15 };
055  private static ByteBuffer data2_2_bb = ByteBuffer.wrap(data2_2);
056
057  private final int Equal = 0;
058  private final int NotEqual = 1;
059
060  @Test
061  public void testANDOperation() {
062    testOperation(zeros, ones, BitComparator.BitwiseOp.AND, NotEqual);
063    testOperation(data1, ones, BitComparator.BitwiseOp.AND, Equal);
064    testOperation(data1, data0, BitComparator.BitwiseOp.AND, NotEqual);
065    testOperation(data2, data1, BitComparator.BitwiseOp.AND, NotEqual);
066    testOperation(ones, data0, BitComparator.BitwiseOp.AND, Equal);
067    testOperation(ones, data3, BitComparator.BitwiseOp.AND, NotEqual);
068
069    testOperation(zeros_bb, ones, BitComparator.BitwiseOp.AND, NotEqual);
070    testOperation(data1_bb, ones, BitComparator.BitwiseOp.AND, Equal);
071    testOperation(data1_bb, data0, BitComparator.BitwiseOp.AND, NotEqual);
072    testOperation(data2_bb, data1, BitComparator.BitwiseOp.AND, NotEqual);
073    testOperation(ones_bb, data0, BitComparator.BitwiseOp.AND, Equal);
074    testOperation(ones_bb, data3, BitComparator.BitwiseOp.AND, NotEqual);
075  }
076
077  @Test
078  public void testOROperation() {
079    testOperation(ones, zeros, BitComparator.BitwiseOp.OR, Equal);
080    testOperation(zeros, zeros, BitComparator.BitwiseOp.OR, NotEqual);
081    testOperation(data1, zeros, BitComparator.BitwiseOp.OR, Equal);
082    testOperation(data2, data1, BitComparator.BitwiseOp.OR, Equal);
083    testOperation(ones, data3, BitComparator.BitwiseOp.OR, NotEqual);
084
085    testOperation(ones_bb, zeros, BitComparator.BitwiseOp.OR, Equal);
086    testOperation(zeros_bb, zeros, BitComparator.BitwiseOp.OR, NotEqual);
087    testOperation(data1_bb, zeros, BitComparator.BitwiseOp.OR, Equal);
088    testOperation(data2_bb, data1, BitComparator.BitwiseOp.OR, Equal);
089    testOperation(ones_bb, data3, BitComparator.BitwiseOp.OR, NotEqual);
090  }
091
092  @Test
093  public void testXOROperation() {
094    testOperation(ones, zeros, BitComparator.BitwiseOp.XOR, Equal);
095    testOperation(zeros, zeros, BitComparator.BitwiseOp.XOR, NotEqual);
096    testOperation(ones, ones, BitComparator.BitwiseOp.XOR, NotEqual);
097    testOperation(data2, data1, BitComparator.BitwiseOp.XOR, Equal);
098    testOperation(ones, data3, BitComparator.BitwiseOp.XOR, NotEqual);
099
100    testOperation(ones_bb, zeros, BitComparator.BitwiseOp.XOR, Equal);
101    testOperation(zeros_bb, zeros, BitComparator.BitwiseOp.XOR, NotEqual);
102    testOperation(ones_bb, ones, BitComparator.BitwiseOp.XOR, NotEqual);
103    testOperation(data2_bb, data1, BitComparator.BitwiseOp.XOR, Equal);
104    testOperation(ones_bb, data3, BitComparator.BitwiseOp.XOR, NotEqual);
105  }
106
107  private void testOperation(byte[] data, byte[] comparatorBytes, BitComparator.BitwiseOp operator,
108    int expected) {
109    BitComparator comparator = new BitComparator(comparatorBytes, operator);
110    assertEquals(expected, comparator.compareTo(data));
111  }
112
113  private void testOperation(ByteBuffer data, byte[] comparatorBytes,
114    BitComparator.BitwiseOp operator, int expected) {
115    BitComparator comparator = new BitComparator(comparatorBytes, operator);
116    assertEquals(expected, comparator.compareTo(data, 0, data.capacity()));
117  }
118
119  @Test
120  public void testANDOperationWithOffset() {
121    testOperationWithOffset(data1_2, ones, BitComparator.BitwiseOp.AND, Equal);
122    testOperationWithOffset(data1_2, data0, BitComparator.BitwiseOp.AND, NotEqual);
123    testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.AND, NotEqual);
124
125    testOperationWithOffset(data1_2_bb, ones, BitComparator.BitwiseOp.AND, Equal);
126    testOperationWithOffset(data1_2_bb, data0, BitComparator.BitwiseOp.AND, NotEqual);
127    testOperationWithOffset(data2_2_bb, data1, BitComparator.BitwiseOp.AND, NotEqual);
128  }
129
130  @Test
131  public void testOROperationWithOffset() {
132    testOperationWithOffset(data1_2, zeros, BitComparator.BitwiseOp.OR, Equal);
133    testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.OR, Equal);
134
135    testOperationWithOffset(data1_2_bb, zeros, BitComparator.BitwiseOp.OR, Equal);
136    testOperationWithOffset(data2_2_bb, data1, BitComparator.BitwiseOp.OR, Equal);
137  }
138
139  @Test
140  public void testXOROperationWithOffset() {
141    testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.XOR, Equal);
142
143    testOperationWithOffset(data2_2_bb, data1, BitComparator.BitwiseOp.XOR, Equal);
144  }
145
146  private void testOperationWithOffset(byte[] data, byte[] comparatorBytes,
147    BitComparator.BitwiseOp operator, int expected) {
148    BitComparator comparator = new BitComparator(comparatorBytes, operator);
149    assertEquals(expected, comparator.compareTo(data, 1, comparatorBytes.length));
150  }
151
152  private void testOperationWithOffset(ByteBuffer data, byte[] comparatorBytes,
153    BitComparator.BitwiseOp operator, int expected) {
154    BitComparator comparator = new BitComparator(comparatorBytes, operator);
155    assertEquals(expected, comparator.compareTo(data, 1, comparatorBytes.length));
156  }
157}