001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.stream.IntStream;
025import org.apache.hadoop.hbase.testclassification.MiscTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.junit.jupiter.api.Tag;
028import org.junit.jupiter.api.Test;
029
030@Tag(MiscTests.TAG)
031@Tag(SmallTests.TAG)
032public class TestReservoirSample {
033
034  @Test
035  public void test() {
036    int round = 100000;
037    int containsOne = 0;
038    for (int i = 0; i < round; i++) {
039      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
040      for (int j = 0; j < 100; j++) {
041        rs.add(j);
042        if (j < 10) {
043          assertEquals(j + 1, rs.getSamplingResult().size());
044        } else {
045          assertEquals(10, rs.getSamplingResult().size());
046        }
047      }
048      if (rs.getSamplingResult().contains(1)) {
049        containsOne++;
050      }
051    }
052    // we assume a 5% error rate
053    assertTrue(containsOne > round / 10 * 0.95);
054    assertTrue(containsOne < round / 10 * 1.05);
055  }
056
057  @Test
058  public void testIterator() {
059    int round = 100000;
060    int containsOne = 0;
061    for (int i = 0; i < round; i++) {
062      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
063      rs.add(IntStream.range(0, 100).mapToObj(Integer::valueOf).iterator());
064      if (rs.getSamplingResult().contains(1)) {
065        containsOne++;
066      }
067    }
068    // we assume a 5% error rate
069    assertTrue(containsOne > round / 10 * 0.95);
070    assertTrue(containsOne < round / 10 * 1.05);
071  }
072
073  @Test
074  public void testStream() {
075    int round = 100000;
076    int containsOne = 0;
077    for (int i = 0; i < round; i++) {
078      ReservoirSample<Integer> rs = new ReservoirSample<>(10);
079      rs.add(IntStream.range(0, 100).mapToObj(Integer::valueOf));
080      if (rs.getSamplingResult().contains(1)) {
081        containsOne++;
082      }
083    }
084    // we assume a 5% error rate
085    assertTrue(containsOne > round / 10 * 0.95);
086    assertTrue(containsOne < round / 10 * 1.05);
087  }
088
089  @Test
090  public void testNegativeSamplingNumber() {
091    IllegalArgumentException e =
092      assertThrows(IllegalArgumentException.class, () -> new ReservoirSample<Integer>(-1));
093    assertEquals("negative sampling number(-1) is not allowed", e.getMessage());
094  }
095}