Bael 1010 hll (#2274)

* BAEL-1010 HLL article code

* BAEL-1010 moved tolerated difference to a variable

* Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1010_hll

# Conflicts:
#	libraries/pom.xml

* BAEL-1010 clearer code

* use isCloseTo
This commit is contained in:
Tomasz Lelek 2017-07-17 06:10:55 +02:00 committed by Grzegorz Piwowarek
parent b4d3e23c3e
commit fb7d3fe8f4
1 changed files with 7 additions and 12 deletions

View File

@ -4,6 +4,7 @@ package com.baeldung.hll;
import com.google.common.hash.HashFunction; import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import net.agkn.hll.HLL; import net.agkn.hll.HLL;
import org.assertj.core.data.Offset;
import org.junit.Test; import org.junit.Test;
import java.util.stream.LongStream; import java.util.stream.LongStream;
@ -15,8 +16,8 @@ public class HLLUnitTest {
@Test @Test
public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() { public void givenHLL_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinality() {
//given //given
int numberOfElements = 100_000_000; long numberOfElements = 100_000_000;
int toleratedDifference = 1_000_000; long toleratedDifference = 1_000_000;
HashFunction hashFunction = Hashing.murmur3_128(); HashFunction hashFunction = Hashing.murmur3_128();
HLL hll = new HLL(14, 5); HLL hll = new HLL(14, 5);
@ -29,14 +30,14 @@ public class HLLUnitTest {
//then //then
long cardinality = hll.cardinality(); long cardinality = hll.cardinality();
assertThat(isSimilarTo(cardinality, numberOfElements, toleratedDifference)).isTrue(); assertThat(cardinality).isCloseTo(numberOfElements, Offset.offset(toleratedDifference));
} }
@Test @Test
public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() { public void givenTwoHLLs_whenAddHugeAmountOfNumbers_thenShouldReturnEstimatedCardinalityForUnionOfHLLs() {
//given //given
int numberOfElements = 100_000_000; long numberOfElements = 100_000_000;
int toleratedDifference = 1_000_000; long toleratedDifference = 1_000_000;
HashFunction hashFunction = Hashing.murmur3_128(); HashFunction hashFunction = Hashing.murmur3_128();
HLL firstHll = new HLL(15, 5); HLL firstHll = new HLL(15, 5);
HLL secondHLL = new HLL(15, 5); HLL secondHLL = new HLL(15, 5);
@ -57,12 +58,6 @@ public class HLLUnitTest {
//then //then
firstHll.union(secondHLL); firstHll.union(secondHLL);
long cardinality = firstHll.cardinality(); long cardinality = firstHll.cardinality();
assertThat(isSimilarTo(cardinality, numberOfElements * 2, assertThat(cardinality).isCloseTo(numberOfElements * 2, Offset.offset(toleratedDifference * 2));
toleratedDifference * 2)).isTrue();
}
private boolean isSimilarTo(long cardinality, int numberOfElements, int maxToleratedDifference) {
System.out.println(cardinality);
return Math.abs(cardinality - numberOfElements) <= maxToleratedDifference;
} }
} }