1   /**
2    *  Copyright 2010 The Apache Software Foundation
3    * 
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    * 
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *  under the License.
16   */
17  package org.apache.hadoop.hbase.filter;
18  
19  import junit.framework.TestCase;
20  
21  /**
22   * Tests for the bit comparator
23   */
24  public class TestBitComparator extends TestCase {
25  
26    private static byte[] zeros = new byte[]{0, 0, 0, 0, 0, 0};
27    private static byte[] ones = new byte[]{1, 1, 1, 1, 1, 1};
28    private static byte[] data0 = new byte[]{0, 1, 2, 4, 8, 15};
29    private static byte[] data1 = new byte[]{15, 0, 0, 0, 0, 0};
30    private static byte[] data2 = new byte[]{0, 0, 0, 0, 0, 15};
31    private static byte[] data3 = new byte[]{15, 15, 15, 15, 15};
32    private final int Equal = 0;
33    private final int NotEqual = 1;
34  
35    public void testANDOperation() {
36      testOperation(zeros, ones, BitComparator.BitwiseOp.AND, NotEqual);
37      testOperation(data1, ones, BitComparator.BitwiseOp.AND, Equal);
38      testOperation(data1, data0, BitComparator.BitwiseOp.AND, NotEqual);
39      testOperation(data2, data1, BitComparator.BitwiseOp.AND, NotEqual);
40      testOperation(ones, data0, BitComparator.BitwiseOp.AND, Equal);
41      testOperation(ones, data3, BitComparator.BitwiseOp.AND, NotEqual);
42    }
43  
44    public void testOROperation() {
45      testOperation(ones, zeros, BitComparator.BitwiseOp.OR, Equal);
46      testOperation(zeros, zeros, BitComparator.BitwiseOp.OR, NotEqual);
47      testOperation(data1, zeros, BitComparator.BitwiseOp.OR, Equal);
48      testOperation(data2, data1, BitComparator.BitwiseOp.OR, Equal);
49      testOperation(ones, data3, BitComparator.BitwiseOp.OR, NotEqual);
50    }
51  
52    public void testXOROperation() {
53      testOperation(ones, zeros, BitComparator.BitwiseOp.XOR, Equal);
54      testOperation(zeros, zeros, BitComparator.BitwiseOp.XOR, NotEqual);
55      testOperation(ones, ones, BitComparator.BitwiseOp.XOR, NotEqual);
56      testOperation(data2, data1, BitComparator.BitwiseOp.XOR, Equal);
57      testOperation(ones, data3, BitComparator.BitwiseOp.XOR, NotEqual);
58    }
59  
60    private void testOperation(byte[] data, byte[] comparatorBytes, BitComparator.BitwiseOp operator, int expected) {
61      BitComparator comparator = new BitComparator(comparatorBytes, operator);
62      assertEquals(comparator.compareTo(data), expected);
63    }
64  }