44import com .google .common .hash .HashFunction ;
55import com .google .common .hash .Hashing ;
66import net .agkn .hll .HLL ;
7+ import org .assertj .core .data .Offset ;
78import org .junit .Test ;
89
910import java .util .stream .LongStream ;
@@ -15,8 +16,8 @@ public class HLLUnitTest {
1516 @ Test
1617 public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality () {
1718 //given
18- int numberOfElements = 100_000_000 ;
19- int toleratedDifference = 1_000_000 ;
19+ long numberOfElements = 100_000_000 ;
20+ long toleratedDifference = 1_000_000 ;
2021 HashFunction hashFunction = Hashing .murmur3_128 ();
2122 HLL hll = new HLL (14 , 5 );
2223
@@ -29,14 +30,14 @@ public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardina
2930
3031 //then
3132 long cardinality = hll .cardinality ();
32- assertThat (isSimilarTo ( cardinality , numberOfElements , toleratedDifference )). isTrue ( );
33+ assertThat (cardinality ). isCloseTo ( numberOfElements , Offset . offset ( toleratedDifference ) );
3334 }
3435
3536 @ Test
3637 public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs () {
3738 //given
38- int numberOfElements = 100_000_000 ;
39- int toleratedDifference = 1_000_000 ;
39+ long numberOfElements = 100_000_000 ;
40+ long toleratedDifference = 1_000_000 ;
4041 HashFunction hashFunction = Hashing .murmur3_128 ();
4142 HLL firstHll = new HLL (15 , 5 );
4243 HLL secondHLL = new HLL (15 , 5 );
@@ -57,12 +58,6 @@ public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCar
5758 //then
5859 firstHll .union (secondHLL );
5960 long cardinality = firstHll .cardinality ();
60- assertThat (isSimilarTo (cardinality , numberOfElements * 2 ,
61- toleratedDifference * 2 )).isTrue ();
62- }
63-
64- private boolean isSimilarTo (long cardinality , int numberOfElements , int maxToleratedDifference ) {
65- System .out .println (cardinality );
66- return Math .abs (cardinality - numberOfElements ) <= maxToleratedDifference ;
61+ assertThat (cardinality ).isCloseTo (numberOfElements * 2 , Offset .offset (toleratedDifference * 2 ));
6762 }
6863}
0 commit comments