Skip to content

Commit

Permalink
Add Chao stream API test (gstamatelat#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Dec 26, 2017
1 parent 2838fd1 commit 0edf484
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/test/java/gr/james/sampling/ChaoSamplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class ChaoSamplingTest {
Expand Down Expand Up @@ -35,4 +38,28 @@ public void correctness() {
}
}

/**
* Same test as {@link #correctness()} but with the stream API.
*/
@Test
public void stream() {
final int[] d = new int[STREAM];
for (int reps = 0; reps < REPS; reps++) {
final Map<Integer, Double> map = new HashMap<>();
for (int i = 0; i < STREAM; i++) {
map.put(i, (double) (i + 1));
}
final Collection<Integer> sample =
map.entrySet().stream().collect(ChaoSampling.weightedCollector(SAMPLE, RANDOM));
for (int s : sample) {
d[s]++;
}
}
final double diff = 2.0 * (REPS * SAMPLE) / (STREAM * (STREAM + 1));
for (int i = 0; i < d.length - 1; i++) {
Assert.assertEquals("ChaoSamplingTest.stream",
1.0, (d[i + 1] - d[i]) / diff, 1e-2);
}
}

}

0 comments on commit 0edf484

Please sign in to comment.