View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }