Skip to content

Commit

Permalink
Correctness tests with variable stream size
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 2, 2018
1 parent 3b1c17f commit 30f0b38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
43 changes: 25 additions & 18 deletions src/test/java/gr/james/sampling/RandomSamplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,43 @@ public static Collection<Supplier<RandomSampling<Integer>>> implementations() {
}

/**
* All items must be selected with equal probability. Using 20 stream elements.
* All items must be selected with equal probability.
*/
@Test
public void correctness20() {
final int STREAM = 20;
final int REPS = 1000000;
public void correctness() {
final int[] streamSizes = {1, 20, 100};
final int[] repsSizes = {1000000, 1000000, 1000000};

final int[] d = new int[STREAM];
Assert.assertEquals(streamSizes.length, repsSizes.length);

for (int reps = 0; reps < REPS; reps++) {
final RandomSampling<Integer> alg = impl.get();
for (int test = 0; test < streamSizes.length; test++) {
final int STREAM = streamSizes[test];
final int REPS = repsSizes[test];

for (int i = 0; i < STREAM; i++) {
alg.feed(i);
}
final int[] d = new int[STREAM];

for (int s : alg.sample()) {
d[s]++;
for (int reps = 0; reps < REPS; reps++) {
final RandomSampling<Integer> alg = impl.get();

for (int i = 0; i < STREAM; i++) {
alg.feed(i);
}

for (int s : alg.sample()) {
d[s]++;
}
}
}

for (int c : d) {
final double expected = (double) REPS * SAMPLE / STREAM;
final double actual = (double) c;
Assert.assertEquals("RandomSamplingTest.correctness", 1, actual / expected, 1e-2);
for (int c : d) {
final double expected = (double) REPS * Math.min(SAMPLE, STREAM) / STREAM;
final double actual = (double) c;
Assert.assertEquals("RandomSamplingTest.correctness", 1, actual / expected, 1e-2);
}
}
}

/**
* Same test as {@link #correctness20()} but with the stream API. Using 20 stream elements.
* Same test as {@link #correctness()} but with the stream API.
*/
@Test
public void stream20() {
Expand Down
39 changes: 23 additions & 16 deletions src/test/java/gr/james/sampling/WeightedRandomSamplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,41 @@ public static Collection<Supplier<WeightedRandomSampling<Integer>>> implementati
}

/**
* Increased weight means more occurrences. Using 20 stream elements.
* Increased weight means more occurrences.
*/
@Test
public void correctness20() {
final int STREAM = 20;
final int REPS = 1000000;
public void correctness() {
final int[] streamSizes = {1, 20, 100};
final int[] repsSizes = {1000000, 1000000, 1000000};

final int[] d = new int[STREAM];
Assert.assertEquals(streamSizes.length, repsSizes.length);

for (int reps = 0; reps < REPS; reps++) {
final WeightedRandomSampling<Integer> alg = impl.get();
for (int test = 0; test < streamSizes.length; test++) {
final int STREAM = streamSizes[test];
final int REPS = repsSizes[test];

for (int i = 0; i < STREAM; i++) {
alg.feed(i, i + 1);
}
final int[] d = new int[STREAM];

for (int s : alg.sample()) {
d[s]++;
for (int reps = 0; reps < REPS; reps++) {
final WeightedRandomSampling<Integer> alg = impl.get();

for (int i = 0; i < STREAM; i++) {
alg.feed(i, i + 1);
}

for (int s : alg.sample()) {
d[s]++;
}
}
}

for (int i = 0; i < d.length - 1; i++) {
Assert.assertTrue("WeightedRandomSamplingTest.correctness", d[i] < d[i + 1]);
for (int i = 0; i < d.length - 1; i++) {
Assert.assertTrue("WeightedRandomSamplingTest.correctness", d[i] < d[i + 1]);
}
}
}

/**
* Same test as {@link #correctness20()} but with the stream API. Using 20 stream elements.
* Same test as {@link #correctness()} but with the stream API.
*/
@Test
public void stream20() {
Expand Down

0 comments on commit 30f0b38

Please sign in to comment.