View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import org.apache.hadoop.hbase.testclassification.MiscTests;
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  
27  import org.junit.ClassRule;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category({MiscTests.class, SmallTests.class})
32  public class TestLossyCounting {
33  
34    @Test
35    public void testBucketSize() {
36      LossyCounting lossyCounting = new LossyCounting(0.01);
37      assertEquals(100L, lossyCounting.getBuketSize());
38      LossyCounting lossyCounting2 = new LossyCounting();
39      assertEquals(50L, lossyCounting2.getBuketSize());
40    }
41  
42    @Test
43    public void testAddByOne() {
44      LossyCounting lossyCounting = new LossyCounting(0.01);
45      for(int i = 0; i < 100; i++){
46        String key = "" + i;
47        lossyCounting.addByOne(key);
48      }
49      assertEquals(100L, lossyCounting.getDataSize());
50      for(int i = 0; i < 100; i++){
51        String key = "" + i;
52        assertEquals(true, lossyCounting.contains(key));
53      }
54    }
55  
56    @Test
57    public void testSweep1() {
58      LossyCounting lossyCounting = new LossyCounting(0.01);
59      for(int i = 0; i < 400; i++){
60        String key = "" + i;
61        lossyCounting.addByOne(key);
62      }
63      assertEquals(4L, lossyCounting.getCurrentTerm());
64      assertEquals(0L, lossyCounting.getDataSize());
65    }
66  
67    @Test
68    public void testSweep2() {
69      LossyCounting lossyCounting = new LossyCounting(0.1);
70      for(int i = 0; i < 10; i++){
71        String key = "" + i;
72        lossyCounting.addByOne(key);
73      }
74      assertEquals(10L, lossyCounting.getDataSize());
75      for(int i = 0; i < 10; i++){
76        String key = "1";
77        lossyCounting.addByOne(key);
78      }
79      assertEquals(1L, lossyCounting.getDataSize());
80    }
81  
82  
83  }