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.testclassification.SmallTests;
25 import org.apache.hadoop.hbase.util.SimpleByteRange;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import com.google.common.collect.Lists;
32
33 @Category(SmallTests.class)
34 public class TestTreeDepth {
35
36 @Test
37 public void testSingleNode() {
38 List<String> inputs = Lists.newArrayList("a");
39 testInternal(inputs, 1);
40 }
41
42 @Test
43 public void testSimpleBranch() {
44 List<String> inputs = Lists.newArrayList("a", "aa", "ab");
45 testInternal(inputs, 2);
46 }
47
48 @Test
49 public void testEmptyRoot() {
50 List<String> inputs = Lists.newArrayList("a", "b");
51 testInternal(inputs, 2);
52 }
53
54 @Test
55 public void testRootAsNub() {
56 List<String> inputs = Lists.newArrayList("a", "aa");
57 testInternal(inputs, 2);
58 }
59
60 @Test
61 public void testRootAsNubPlusNub() {
62 List<String> inputs = Lists.newArrayList("a", "aa", "aaa");
63 testInternal(inputs, 3);
64 }
65
66 @Test
67 public void testEmptyRootPlusNub() {
68 List<String> inputs = Lists.newArrayList("a", "aa", "b");
69 testInternal(inputs, 3);
70 }
71
72 @Test
73 public void testSplitDistantAncestor() {
74 List<String> inputs = Lists.newArrayList("a", "ac", "acd", "b");
75 testInternal(inputs, 4);
76 }
77
78 protected void testInternal(List<String> inputs, int expectedTreeDepth) {
79 Tokenizer builder = new Tokenizer();
80 for (String s : inputs) {
81 SimpleByteRange b = new SimpleByteRange(Bytes.toBytes(s));
82 builder.addSorted(b);
83 }
84 Assert.assertEquals(1, builder.getRoot().getNodeDepth());
85 Assert.assertEquals(expectedTreeDepth, builder.getTreeDepth());
86 }
87
88 }