1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.codec.prefixtree.builder;
20
21 import java.util.List;
22
23 import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.Tokenizer;
24 import org.apache.hadoop.hbase.util.SimpleByteRange;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29 import com.google.common.collect.Lists;
30
31 public class TestTreeDepth {
32
33 @Test
34 public void testSingleNode() {
35 List<String> inputs = Lists.newArrayList("a");
36 testInternal(inputs, 1);
37 }
38
39 @Test
40 public void testSimpleBranch() {
41 List<String> inputs = Lists.newArrayList("a", "aa", "ab");
42 testInternal(inputs, 2);
43 }
44
45 @Test
46 public void testEmptyRoot() {
47 List<String> inputs = Lists.newArrayList("a", "b");
48 testInternal(inputs, 2);
49 }
50
51 @Test
52 public void testRootAsNub() {
53 List<String> inputs = Lists.newArrayList("a", "aa");
54 testInternal(inputs, 2);
55 }
56
57 @Test
58 public void testRootAsNubPlusNub() {
59 List<String> inputs = Lists.newArrayList("a", "aa", "aaa");
60 testInternal(inputs, 3);
61 }
62
63 @Test
64 public void testEmptyRootPlusNub() {
65 List<String> inputs = Lists.newArrayList("a", "aa", "b");
66 testInternal(inputs, 3);
67 }
68
69 @Test
70 public void testSplitDistantAncestor() {
71 List<String> inputs = Lists.newArrayList("a", "ac", "acd", "b");
72 testInternal(inputs, 4);
73 }
74
75 protected void testInternal(List<String> inputs, int expectedTreeDepth) {
76 Tokenizer builder = new Tokenizer();
77 for (String s : inputs) {
78 SimpleByteRange b = new SimpleByteRange(Bytes.toBytes(s));
79 builder.addSorted(b);
80 }
81 Assert.assertEquals(1, builder.getRoot().getNodeDepth());
82 Assert.assertEquals(expectedTreeDepth, builder.getTreeDepth());
83 }
84
85 }