View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    * 
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.filter;
20  
21  import org.apache.hadoop.hbase.testclassification.SmallTests;
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  import static org.junit.Assert.assertEquals;
26  
27  /**
28   * Tests for the bit comparator
29   */
30  @Category(SmallTests.class)
31  public class TestBitComparator {
32  
33    private static byte[] zeros = new byte[]{0, 0, 0, 0, 0, 0};
34    private static byte[] ones = new byte[]{1, 1, 1, 1, 1, 1};
35    private static byte[] data0 = new byte[]{0, 1, 2, 4, 8, 15};
36    private static byte[] data1 = new byte[]{15, 0, 0, 0, 0, 0};
37    private static byte[] data2 = new byte[]{0, 0, 0, 0, 0, 15};
38    private static byte[] data3 = new byte[]{15, 15, 15, 15, 15};
39    
40    // data for testing compareTo method with offset and length parameters
41    private static byte[] data1_2 = new byte[]{15, 15, 0, 0, 0, 0, 0, 15};
42    private static byte[] data2_2 = new byte[]{15, 0, 0, 0, 0, 0, 15, 15};
43    
44    private final int Equal = 0;
45    private final int NotEqual = 1;
46  
47    @Test
48    public void testANDOperation() {
49      testOperation(zeros, ones, BitComparator.BitwiseOp.AND, NotEqual);
50      testOperation(data1, ones, BitComparator.BitwiseOp.AND, Equal);
51      testOperation(data1, data0, BitComparator.BitwiseOp.AND, NotEqual);
52      testOperation(data2, data1, BitComparator.BitwiseOp.AND, NotEqual);
53      testOperation(ones, data0, BitComparator.BitwiseOp.AND, Equal);
54      testOperation(ones, data3, BitComparator.BitwiseOp.AND, NotEqual);
55    }
56  
57    @Test
58    public void testOROperation() {
59      testOperation(ones, zeros, BitComparator.BitwiseOp.OR, Equal);
60      testOperation(zeros, zeros, BitComparator.BitwiseOp.OR, NotEqual);
61      testOperation(data1, zeros, BitComparator.BitwiseOp.OR, Equal);
62      testOperation(data2, data1, BitComparator.BitwiseOp.OR, Equal);
63      testOperation(ones, data3, BitComparator.BitwiseOp.OR, NotEqual);
64    }
65  
66    @Test
67    public void testXOROperation() {
68      testOperation(ones, zeros, BitComparator.BitwiseOp.XOR, Equal);
69      testOperation(zeros, zeros, BitComparator.BitwiseOp.XOR, NotEqual);
70      testOperation(ones, ones, BitComparator.BitwiseOp.XOR, NotEqual);
71      testOperation(data2, data1, BitComparator.BitwiseOp.XOR, Equal);
72      testOperation(ones, data3, BitComparator.BitwiseOp.XOR, NotEqual);
73    }
74  
75    private void testOperation(byte[] data, byte[] comparatorBytes, BitComparator.BitwiseOp operator, int expected) {
76      BitComparator comparator = new BitComparator(comparatorBytes, operator);
77      assertEquals(comparator.compareTo(data), expected);
78    }
79  
80    @Test
81    public void testANDOperationWithOffset() {
82      testOperationWithOffset(data1_2, ones, BitComparator.BitwiseOp.AND, Equal);
83      testOperationWithOffset(data1_2, data0, BitComparator.BitwiseOp.AND, NotEqual);
84      testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.AND, NotEqual);
85    }
86  
87    @Test
88    public void testOROperationWithOffset() {
89      testOperationWithOffset(data1_2, zeros, BitComparator.BitwiseOp.OR, Equal);
90      testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.OR, Equal);
91    }
92  
93    @Test
94    public void testXOROperationWithOffset() {
95      testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.XOR, Equal);
96    }
97  
98    private void testOperationWithOffset(byte[] data, byte[] comparatorBytes, BitComparator.BitwiseOp operator, int expected) {
99      BitComparator comparator = new BitComparator(comparatorBytes, operator);
100     assertEquals(comparator.compareTo(data, 1, comparatorBytes.length), expected);
101   }
102 }
103