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.util.Bytes;
26  import java.io.ByteArrayOutputStream;
27  import java.io.DataOutputStream;
28  import java.io.IOException;
29  
30  public class TestImmutableBytesWritable extends TestCase {
31    public void testHash() throws Exception {
32      assertEquals(
33        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
34        new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
35      assertEquals(
36        new ImmutableBytesWritable(Bytes.toBytes("xxabcd"), 2, 3).hashCode(),
37        new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
38      assertNotSame(
39        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
40        new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 2).hashCode());
41    }
42  
43    public void testSpecificCompare() {
44      ImmutableBytesWritable ibw1 = new ImmutableBytesWritable(new byte[]{0x0f});
45      ImmutableBytesWritable ibw2 = new ImmutableBytesWritable(new byte[]{0x00, 0x00});
46      ImmutableBytesWritable.Comparator c = new ImmutableBytesWritable.Comparator();
47      assertFalse("ibw1 < ibw2", c.compare( ibw1, ibw2 ) < 0 );
48    }
49  
50    public void testComparison() throws Exception {
51      runTests("aa", "b", -1);
52      runTests("aa", "aa", 0);
53      runTests("aa", "ab", -1);
54      runTests("aa", "aaa", -1);
55      runTests("", "", 0);
56      runTests("", "a", -1);
57    }
58  
59    private void runTests(String aStr, String bStr, int signum)
60      throws Exception {
61      ImmutableBytesWritable a = new ImmutableBytesWritable(
62        Bytes.toBytes(aStr));
63      ImmutableBytesWritable b = new ImmutableBytesWritable(
64        Bytes.toBytes(bStr));
65  
66      doComparisonsOnObjects(a, b, signum);
67      doComparisonsOnRaw(a, b, signum);
68  
69      // Tests for when the offset is non-zero
70      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr),
71                                     3, aStr.length());
72      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr),
73                                     2, bStr.length());
74      doComparisonsOnObjects(a, b, signum);
75      doComparisonsOnRaw(a, b, signum);
76  
77      // Tests for when offset is nonzero and length doesn't extend to end
78      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr + "zzz"),
79                                     3, aStr.length());
80      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr + "aaa"),
81                                     2, bStr.length());
82      doComparisonsOnObjects(a, b, signum);
83      doComparisonsOnRaw(a, b, signum);
84    }
85  
86  
87    private int signum(int i) {
88      if (i > 0) return 1;
89      if (i == 0) return 0;
90      return -1;
91    }
92  
93    private void doComparisonsOnRaw(ImmutableBytesWritable a,
94                                    ImmutableBytesWritable b,
95                                    int expectedSignum)
96      throws IOException {
97      ImmutableBytesWritable.Comparator comparator =
98        new ImmutableBytesWritable.Comparator();
99  
100     ByteArrayOutputStream baosA = new ByteArrayOutputStream();
101     ByteArrayOutputStream baosB = new ByteArrayOutputStream();
102 
103     a.write(new DataOutputStream(baosA));
104     b.write(new DataOutputStream(baosB));
105 
106     assertEquals(
107       "Comparing " + a + " and " + b + " as raw",
108       signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(),
109                                 baosB.toByteArray(), 0, baosB.size())),
110       expectedSignum);
111 
112     assertEquals(
113       "Comparing " + a + " and " + b + " as raw (inverse)",
114       -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(),
115                                  baosA.toByteArray(), 0, baosA.size())),
116       expectedSignum);
117   }
118 
119   private void doComparisonsOnObjects(ImmutableBytesWritable a,
120                                       ImmutableBytesWritable b,
121                                       int expectedSignum) {
122     ImmutableBytesWritable.Comparator comparator =
123       new ImmutableBytesWritable.Comparator();
124     assertEquals(
125       "Comparing " + a + " and " + b + " as objects",
126       signum(comparator.compare(a, b)), expectedSignum);
127     assertEquals(
128       "Comparing " + a + " and " + b + " as objects (inverse)",
129       -signum(comparator.compare(b, a)), expectedSignum);
130   }
131 }