Bael 1010 hll
* HLL article code
* moved tolerated difference to a variable
* Merge branch 'master' of into BAEL-1010_hll
# Conflicts:
#	libraries/pom.xml
* clearer code
* use isCloseTo
@@ -4,6 +4,7 @@ package com.baeldung.hll;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import net.agkn.hll.HLL;
+import org.assertj.core.data.Offset;
import org.junit.Test;
import java.util.stream.LongStream;
@@ -15,8 +16,8 @@ public class HLLUnitTest {
@Test
public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() {
//given
- int numberOfElements = 100_000_000;
- int toleratedDifference = 1_000_000;
+ long numberOfElements = 100_000_000;
+ long toleratedDifference = 1_000_000;
HashFunction hashFunction = Hashing.murmur3_128();
HLL hll = new HLL(14, 5);
@@ -29,14 +30,14 @@ public class HLLUnitTest {
//then
long cardinality = hll.cardinality();
- assertThat(isSimilarTo(cardinality, numberOfElements, toleratedDifference)).isTrue();
+ assertThat(cardinality).isCloseTo(numberOfElements, Offset.offset(toleratedDifference));
}
@Test
public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() {
//given
- int numberOfElements = 100_000_000;
- int toleratedDifference = 1_000_000;
+ long numberOfElements = 100_000_000;
+ long toleratedDifference = 1_000_000;
HashFunction hashFunction = Hashing.murmur3_128();
HLL firstHll = new HLL(15, 5);
HLL secondHLL = new HLL(15, 5);
@@ -57,12 +58,6 @@ public class HLLUnitTest {
//then
firstHll.union(secondHLL);
long cardinality = firstHll.cardinality();
- assertThat(isSimilarTo(cardinality, numberOfElements * 2,
- toleratedDifference * 2)).isTrue();
- }
-
- private boolean isSimilarTo(long cardinality, int numberOfElements, int maxToleratedDifference) {
- System.out.println(cardinality);
- return Math.abs(cardinality - numberOfElements) <= maxToleratedDifference;
+ assertThat(cardinality).isCloseTo(numberOfElements * 2, Offset.offset(toleratedDifference * 2));
}
}
