Skip to content

Commit

Permalink
Add test for weighted stream API (gstamatelat#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Dec 28, 2017
1 parent 4c18f8e commit c3d0642
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/test/java/gr/james/sampling/WeightedRandomSamplingTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gr.james.sampling;

import gr.james.sampling.collect.WeightedRandomSamplingCollector;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -9,6 +10,8 @@
import java.util.Collection;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Tests for weighted algorithms
Expand Down Expand Up @@ -56,4 +59,34 @@ public void correctness() {
}
}

@Test
public void stream() {
final int[] d = new int[STREAM];

for (int reps = 0; reps < REPS; reps++) {
final WeightedRandomSamplingCollector<Integer> collector;

final WeightedRandomSampling<Integer> alg = impl.get();
if (alg instanceof EfraimidisSampling) {
collector = EfraimidisSampling.weightedCollector(SAMPLE, RANDOM);
} else if (alg instanceof ChaoSampling) {
collector = ChaoSampling.weightedCollector(SAMPLE, RANDOM);
} else {
throw new AssertionError("WeightedRandomSamplingTest.stream");
}

final Collection<Integer> sample = IntStream.range(0, STREAM).boxed()
.collect(Collectors.toMap(o -> o, o -> (double) (o + 1)))
.entrySet().stream().collect(collector);

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

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

}

0 comments on commit c3d0642

Please sign in to comment.