1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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