1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.io;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.junit.experimental.categories.Category;
28  
29  import java.io.ByteArrayOutputStream;
30  import java.io.DataOutputStream;
31  import java.io.IOException;
32  
33  @Category(SmallTests.class)
34  public class TestImmutableBytesWritable extends TestCase {
35    public void testHash() throws Exception {
36      assertEquals(
37        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
38        new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
39      assertEquals(
40        new ImmutableBytesWritable(Bytes.toBytes("xxabcd"), 2, 3).hashCode(),
41        new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
42      assertNotSame(
43        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
44        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 2).hashCode());
45    }
46  
47    public void testSpecificCompare() {
48      ImmutableBytesWritable ibw1 = new ImmutableBytesWritable(new byte[]{0x0f});
49      ImmutableBytesWritable ibw2 = new ImmutableBytesWritable(new byte[]{0x00, 0x00});
50      ImmutableBytesWritable.Comparator c = new ImmutableBytesWritable.Comparator();
51      assertFalse("ibw1 < ibw2", c.compare( ibw1, ibw2 ) < 0 );
52    }
53  
54    public void testComparison() throws Exception {
55      runTests("aa", "b", -1);
56      runTests("aa", "aa", 0);
57      runTests("aa", "ab", -1);
58      runTests("aa", "aaa", -1);
59      runTests("", "", 0);
60      runTests("", "a", -1);
61    }
62  
63    private void runTests(String aStr, String bStr, int signum)
64      throws Exception {
65      ImmutableBytesWritable a = new ImmutableBytesWritable(
66        Bytes.toBytes(aStr));
67      ImmutableBytesWritable b = new ImmutableBytesWritable(
68        Bytes.toBytes(bStr));
69  
70      doComparisonsOnObjects(a, b, signum);
71      doComparisonsOnRaw(a, b, signum);
72  
73      // Tests for when the offset is non-zero
74      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr),
75                                     3, aStr.length());
76      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr),
77                                     2, bStr.length());
78      doComparisonsOnObjects(a, b, signum);
79      doComparisonsOnRaw(a, b, signum);
80  
81      // Tests for when offset is nonzero and length doesn't extend to end
82      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr + "zzz"),
83                                     3, aStr.length());
84      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr + "aaa"),
85                                     2, bStr.length());
86      doComparisonsOnObjects(a, b, signum);
87      doComparisonsOnRaw(a, b, signum);
88    }
89  
90  
91    private int signum(int i) {
92      if (i > 0) return 1;
93      if (i == 0) return 0;
94      return -1;
95    }
96  
97    private void doComparisonsOnRaw(ImmutableBytesWritable a,
98                                    ImmutableBytesWritable b,
99                                    int expectedSignum)
100     throws IOException {
101     ImmutableBytesWritable.Comparator comparator =
102       new ImmutableBytesWritable.Comparator();
103 
104     ByteArrayOutputStream baosA = new ByteArrayOutputStream();
105     ByteArrayOutputStream baosB = new ByteArrayOutputStream();
106 
107     a.write(new DataOutputStream(baosA));
108     b.write(new DataOutputStream(baosB));
109 
110     assertEquals(
111       "Comparing " + a + " and " + b + " as raw",
112       signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(),
113                                 baosB.toByteArray(), 0, baosB.size())),
114       expectedSignum);
115 
116     assertEquals(
117       "Comparing " + a + " and " + b + " as raw (inverse)",
118       -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(),
119                                  baosA.toByteArray(), 0, baosA.size())),
120       expectedSignum);
121   }
122 
123   private void doComparisonsOnObjects(ImmutableBytesWritable a,
124                                       ImmutableBytesWritable b,
125                                       int expectedSignum) {
126     ImmutableBytesWritable.Comparator comparator =
127       new ImmutableBytesWritable.Comparator();
128     assertEquals(
129       "Comparing " + a + " and " + b + " as objects",
130       signum(comparator.compare(a, b)), expectedSignum);
131     assertEquals(
132       "Comparing " + a + " and " + b + " as objects (inverse)",
133       -signum(comparator.compare(b, a)), expectedSignum);
134   }
135 
136   @org.junit.Rule
137   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
138     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
139 }
140