Skip to content

Commit fb7d3fe

Browse files
tomekl007pivovarit
authored andcommitted
Bael 1010 hll (eugenp#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
1 parent b4d3e23 commit fb7d3fe

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

‎libraries/src/test/java/com/baeldung/hll/HLLUnitTest.java‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.common.hash.HashFunction;
55
import com.google.common.hash.Hashing;
66
import net.agkn.hll.HLL;
7+
import org.assertj.core.data.Offset;
78
import org.junit.Test;
89

910
import 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

Comments
 (0)