1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.io;
21
22 import junit.framework.TestCase;
23
24 import org.apache.hadoop.hbase.testclassification.SmallTests;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.junit.experimental.categories.Category;
27
28 import java.io.ByteArrayOutputStream;
29 import java.io.DataOutputStream;
30 import java.io.IOException;
31
32 @Category(SmallTests.class)
33 public class TestImmutableBytesWritable extends TestCase {
34 public void testHash() throws Exception {
35 assertEquals(
36 new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
37 new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
38 assertEquals(
39 new ImmutableBytesWritable(Bytes.toBytes("xxabcd"), 2, 3).hashCode(),
40 new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
41 assertNotSame(
42 new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
43 new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 2).hashCode());
44 }
45
46 public void testSpecificCompare() {
47 ImmutableBytesWritable ibw1 = new ImmutableBytesWritable(new byte[]{0x0f});
48 ImmutableBytesWritable ibw2 = new ImmutableBytesWritable(new byte[]{0x00, 0x00});
49 ImmutableBytesWritable.Comparator c = new ImmutableBytesWritable.Comparator();
50 assertFalse("ibw1 < ibw2", c.compare( ibw1, ibw2 ) < 0 );
51 }
52
53 public void testComparison() throws Exception {
54 runTests("aa", "b", -1);
55 runTests("aa", "aa", 0);
56 runTests("aa", "ab", -1);
57 runTests("aa", "aaa", -1);
58 runTests("", "", 0);
59 runTests("", "a", -1);
60 }
61
62 private void runTests(String aStr, String bStr, int signum)
63 throws Exception {
64 ImmutableBytesWritable a = new ImmutableBytesWritable(
65 Bytes.toBytes(aStr));
66 ImmutableBytesWritable b = new ImmutableBytesWritable(
67 Bytes.toBytes(bStr));
68
69 doComparisonsOnObjects(a, b, signum);
70 doComparisonsOnRaw(a, b, signum);
71
72
73 a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr),
74 3, aStr.length());
75 b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr),
76 2, bStr.length());
77 doComparisonsOnObjects(a, b, signum);
78 doComparisonsOnRaw(a, b, signum);
79
80
81 a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr + "zzz"),
82 3, aStr.length());
83 b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr + "aaa"),
84 2, bStr.length());
85 doComparisonsOnObjects(a, b, signum);
86 doComparisonsOnRaw(a, b, signum);
87 }
88
89
90 private int signum(int i) {
91 if (i > 0) return 1;
92 if (i == 0) return 0;
93 return -1;
94 }
95
96 private void doComparisonsOnRaw(ImmutableBytesWritable a,
97 ImmutableBytesWritable b,
98 int expectedSignum)
99 throws IOException {
100 ImmutableBytesWritable.Comparator comparator =
101 new ImmutableBytesWritable.Comparator();
102
103 ByteArrayOutputStream baosA = new ByteArrayOutputStream();
104 ByteArrayOutputStream baosB = new ByteArrayOutputStream();
105
106 a.write(new DataOutputStream(baosA));
107 b.write(new DataOutputStream(baosB));
108
109 assertEquals(
110 "Comparing " + a + " and " + b + " as raw",
111 signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(),
112 baosB.toByteArray(), 0, baosB.size())),
113 expectedSignum);
114
115 assertEquals(
116 "Comparing " + a + " and " + b + " as raw (inverse)",
117 -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(),
118 baosA.toByteArray(), 0, baosA.size())),
119 expectedSignum);
120 }
121
122 private void doComparisonsOnObjects(ImmutableBytesWritable a,
123 ImmutableBytesWritable b,
124 int expectedSignum) {
125 ImmutableBytesWritable.Comparator comparator =
126 new ImmutableBytesWritable.Comparator();
127 assertEquals(
128 "Comparing " + a + " and " + b + " as objects",
129 signum(comparator.compare(a, b)), expectedSignum);
130 assertEquals(
131 "Comparing " + a + " and " + b + " as objects (inverse)",
132 -signum(comparator.compare(b, a)), expectedSignum);
133 }
134
135 }
136