001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.util; 021 022import static org.junit.Assert.assertEquals; 023 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.testclassification.MiscTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027 028import org.junit.ClassRule; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032@Category({MiscTests.class, SmallTests.class}) 033public class TestLossyCounting { 034 035 @ClassRule 036 public static final HBaseClassTestRule CLASS_RULE = 037 HBaseClassTestRule.forClass(TestLossyCounting.class); 038 039 @Test 040 public void testBucketSize() { 041 LossyCounting lossyCounting = new LossyCounting(0.01); 042 assertEquals(100L, lossyCounting.getBuketSize()); 043 LossyCounting lossyCounting2 = new LossyCounting(); 044 assertEquals(50L, lossyCounting2.getBuketSize()); 045 } 046 047 @Test 048 public void testAddByOne() { 049 LossyCounting lossyCounting = new LossyCounting(0.01); 050 for(int i = 0; i < 100; i++){ 051 String key = "" + i; 052 lossyCounting.addByOne(key); 053 } 054 assertEquals(100L, lossyCounting.getDataSize()); 055 for(int i = 0; i < 100; i++){ 056 String key = "" + i; 057 assertEquals(true, lossyCounting.contains(key)); 058 } 059 } 060 061 @Test 062 public void testSweep1() { 063 LossyCounting lossyCounting = new LossyCounting(0.01); 064 for(int i = 0; i < 400; i++){ 065 String key = "" + i; 066 lossyCounting.addByOne(key); 067 } 068 assertEquals(4L, lossyCounting.getCurrentTerm()); 069 assertEquals(0L, lossyCounting.getDataSize()); 070 } 071 072 @Test 073 public void testSweep2() { 074 LossyCounting lossyCounting = new LossyCounting(0.1); 075 for(int i = 0; i < 10; i++){ 076 String key = "" + i; 077 lossyCounting.addByOne(key); 078 } 079 assertEquals(10L, lossyCounting.getDataSize()); 080 for(int i = 0; i < 10; i++){ 081 String key = "1"; 082 lossyCounting.addByOne(key); 083 } 084 assertEquals(1L, lossyCounting.getDataSize()); 085 } 086 087 088}