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.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  }