BAEL-1005 bloom filter code (#2171)
This commit is contained in:
parent
d191622109
commit
57279a1b5c
|
@ -0,0 +1,54 @@
|
|||
package org.baeldung.guava;
|
||||
|
||||
|
||||
import com.google.common.hash.BloomFilter;
|
||||
import com.google.common.hash.Funnels;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BloomFilterTest {
|
||||
|
||||
@Test
|
||||
public void givenBloomFilter_whenAddNStringsToIt_thenShouldNotReturnAnyFalsePositive() {
|
||||
//when
|
||||
BloomFilter<Integer> filter = BloomFilter.create(
|
||||
Funnels.integerFunnel(),
|
||||
500,
|
||||
0.01);
|
||||
|
||||
//when
|
||||
filter.put(1);
|
||||
filter.put(2);
|
||||
filter.put(3);
|
||||
|
||||
//then
|
||||
// the probability that it returns true, but is actually false is 1%
|
||||
assertThat(filter.mightContain(1)).isTrue();
|
||||
assertThat(filter.mightContain(2)).isTrue();
|
||||
assertThat(filter.mightContain(3)).isTrue();
|
||||
|
||||
assertThat(filter.mightContain(100)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBloomFilter_whenAddNStringsToItMoreThanDefinedExpectedInsertions_thenItWillReturnTrueForAlmostAllElements() {
|
||||
//when
|
||||
BloomFilter<Integer> filter = BloomFilter.create(
|
||||
Funnels.integerFunnel(),
|
||||
5,
|
||||
0.01);
|
||||
|
||||
//when
|
||||
IntStream.range(0, 100_000).forEach(filter::put);
|
||||
|
||||
|
||||
//then
|
||||
assertThat(filter.mightContain(1)).isTrue();
|
||||
assertThat(filter.mightContain(2)).isTrue();
|
||||
assertThat(filter.mightContain(3)).isTrue();
|
||||
assertThat(filter.mightContain(1_000_000)).isTrue();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue