Skip to content

Commit

Permalink
Setup proper testing for feed(Iterable)
Browse files Browse the repository at this point in the history
The current test only used a stream equal to sample size.
  • Loading branch information
gstamatelat committed Nov 20, 2022
1 parent 3d418f6 commit e3ad270
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions src/test/java/gr/james/sampling/RandomSamplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -107,42 +108,54 @@ public void stream20() {
}

/**
* All implementations should handle 2^28 stream size without problems.
* Same test as {@link #correctness()} but for the feed(Iterator) and feed(Iterable) API.
*/
@Test
public void largeStream() {
final RandomSampling<Integer> rs = impl.get();
for (long i = 0; i < 0x10000000L; i++) {
rs.feed(0);
public void feedAlternative() {
final int STREAM = 20;
final int REPS = 1000000;

final Map<String, int[]> d = new HashMap<>();
d.put("iterator", new int[STREAM]);
d.put("set", new int[STREAM]);
d.put("list", new int[STREAM]);

for (int reps = 0; reps < REPS; reps++) {
final Map<String, RandomSampling<Integer>> alg = new HashMap<>();
alg.put("iterator", impl.get());
alg.put("set", impl.get());
alg.put("list", impl.get());

alg.get("iterator").feed(IntStream.range(0, STREAM).iterator());
alg.get("set").feed(IntStream.range(0, STREAM).boxed().collect(Collectors.toSet()));
alg.get("list").feed(IntStream.range(0, STREAM).boxed().collect(Collectors.toList()));

for (String method : d.keySet()) {
final Collection<Integer> sample = alg.get(method).sample();
for (int s : sample) {
d.get(method)[s]++;
}
}
}

for (String method : d.keySet()) {
for (int c : d.get(method)) {
final double expected = (double) REPS * SAMPLE / STREAM;
final double actual = (double) c;
assertEquals(1, actual / expected, 1e-2);
}
}
}

/**
* Equivalence between {@link RandomSampling#feed(Object)}, {@link RandomSampling#feed(Iterator)} and
* {@link RandomSampling#feed(Iterable)}.
* All implementations should handle 2^28 stream size without problems.
*/
@Test
public void feedAlternative() {
final RandomSampling<Integer> rs1 = impl.get(); // Iterator
final RandomSampling<Integer> rs2 = impl.get(); // Iterable
final RandomSampling<Integer> rs3 = impl.get(); // Set
final RandomSampling<Integer> rs4 = impl.get(); // List
final Set<Integer> set = new HashSet<>();
for (int i = 0; i < SAMPLE; i++) {
set.add(i);
rs1.feed(i);
public void largeStream() {
final RandomSampling<Integer> rs = impl.get();
for (long i = 0; i < 0x10000000L; i++) {
rs.feed(0);
}
rs2.feed(set.iterator());
rs3.feed(set);
rs4.feed(new ArrayList<>(set));
Assert.assertTrue(RandomSamplingUtils.samplesEquals(rs1.sample(), rs2.sample()));
Assert.assertTrue(RandomSamplingUtils.samplesEquals(rs2.sample(), rs3.sample()));
assertEquals(rs1.streamSize(), rs2.streamSize());
assertEquals(rs2.streamSize(), rs3.streamSize());
assertEquals(rs3.streamSize(), rs4.streamSize());
assertEquals(rs1.sample().size(), rs2.sample().size());
assertEquals(rs2.sample().size(), rs3.sample().size());
assertEquals(rs3.sample().size(), rs4.sample().size());
}

/**
Expand Down

0 comments on commit e3ad270

Please sign in to comment.