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 testComparison() throws Exception {
44      runTests("aa", "b", -1);
45      runTests("aa", "aa", 0);
46      runTests("aa", "ab", -1);
47      runTests("aa", "aaa", -1);
48      runTests("", "", 0);
49      runTests("", "a", -1);
50    }
51  
52    private void runTests(String aStr, String bStr, int signum)
53      throws Exception {
54      ImmutableBytesWritable a = new ImmutableBytesWritable(
55        Bytes.toBytes(aStr));
56      ImmutableBytesWritable b = new ImmutableBytesWritable(
57        Bytes.toBytes(bStr));
58  
59      doComparisonsOnObjects(a, b, signum);
60      doComparisonsOnRaw(a, b, signum);
61  
62      // Tests for when the offset is non-zero
63      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr),
64                                     3, aStr.length());
65      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr),
66                                     2, bStr.length());
67      doComparisonsOnObjects(a, b, signum);
68      doComparisonsOnRaw(a, b, signum);
69  
70      // Tests for when offset is nonzero and length doesn't extend to end
71      a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr + "zzz"),
72                                     3, aStr.length());
73      b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr + "aaa"),
74                                     2, bStr.length());
75      doComparisonsOnObjects(a, b, signum);
76      doComparisonsOnRaw(a, b, signum);
77    }
78  
79  
80    private int signum(int i) {
81      if (i > 0) return 1;
82      if (i == 0) return 0;
83      return -1;
84    }
85  
86    private void doComparisonsOnRaw(ImmutableBytesWritable a,
87                                    ImmutableBytesWritable b,
88                                    int expectedSignum)
89      throws IOException {
90      ImmutableBytesWritable.Comparator comparator =
91        new ImmutableBytesWritable.Comparator();
92  
93      ByteArrayOutputStream baosA = new ByteArrayOutputStream();
94      ByteArrayOutputStream baosB = new ByteArrayOutputStream();
95  
96      a.write(new DataOutputStream(baosA));
97      b.write(new DataOutputStream(baosB));
98  
99      assertEquals(
100       "Comparing " + a + " and " + b + " as raw",
101       signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(),
102                                 baosB.toByteArray(), 0, baosB.size())),
103       expectedSignum);
104 
105     assertEquals(
106       "Comparing " + a + " and " + b + " as raw (inverse)",
107       -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(),
108                                  baosA.toByteArray(), 0, baosA.size())),
109       expectedSignum);
110   }
111 
112   private void doComparisonsOnObjects(ImmutableBytesWritable a,
113                                       ImmutableBytesWritable b,
114                                       int expectedSignum) {
115     ImmutableBytesWritable.Comparator comparator =
116       new ImmutableBytesWritable.Comparator();
117     assertEquals(
118       "Comparing " + a + " and " + b + " as objects",
119       signum(comparator.compare(a, b)), expectedSignum);
120     assertEquals(
121       "Comparing " + a + " and " + b + " as objects (inverse)",
122       -signum(comparator.compare(b, a)), expectedSignum);
123   }
124 }