File tree Expand file tree Collapse file tree
guava/src/test/java/org/baeldung/guava Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .guava ;
2+
3+
4+ import com .google .common .hash .BloomFilter ;
5+ import com .google .common .hash .Funnels ;
6+ import org .junit .Test ;
7+
8+ import java .util .stream .IntStream ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+
12+ public class BloomFilterTest {
13+
14+ @ Test
15+ public void givenBloomFilter_whenAddNStringsToIt_thenShouldNotReturnAnyFalsePositive () {
16+ //when
17+ BloomFilter <Integer > filter = BloomFilter .create (
18+ Funnels .integerFunnel (),
19+ 500 ,
20+ 0.01 );
21+
22+ //when
23+ filter .put (1 );
24+ filter .put (2 );
25+ filter .put (3 );
26+
27+ //then
28+ // the probability that it returns true, but is actually false is 1%
29+ assertThat (filter .mightContain (1 )).isTrue ();
30+ assertThat (filter .mightContain (2 )).isTrue ();
31+ assertThat (filter .mightContain (3 )).isTrue ();
32+
33+ assertThat (filter .mightContain (100 )).isFalse ();
34+ }
35+
36+ @ Test
37+ public void givenBloomFilter_whenAddNStringsToItMoreThanDefinedExpectedInsertions_thenItWillReturnTrueForAlmostAllElements () {
38+ //when
39+ BloomFilter <Integer > filter = BloomFilter .create (
40+ Funnels .integerFunnel (),
41+ 5 ,
42+ 0.01 );
43+
44+ //when
45+ IntStream .range (0 , 100_000 ).forEach (filter ::put );
46+
47+
48+ //then
49+ assertThat (filter .mightContain (1 )).isTrue ();
50+ assertThat (filter .mightContain (2 )).isTrue ();
51+ assertThat (filter .mightContain (3 )).isTrue ();
52+ assertThat (filter .mightContain (1_000_000 )).isTrue ();
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments