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