@Test public void testSample() { double p = 0.1; int n = 1000; double sd = Math.sqrt(n * p * (1.0 - p)); for (int i = 0; i < 1000; i++) { SamplingLongPrimitiveIterator t = new SamplingLongPrimitiveIterator(countingIterator(n), p); int k = 0; while (t.hasNext()) { long v = t.nextLong(); k++; assertTrue(v >= 0L); assertTrue(v < 1000L); } // Should be +/- 5 standard deviations except in about 1 out of 1.7M cases assertTrue(k >= 100 - 5 * sd); assertTrue(k <= 100 + 5 * sd); } }
@Test public void testSmallInput() { SamplingLongPrimitiveIterator t = new SamplingLongPrimitiveIterator( countingIterator(1), 0.9999); assertTrue(t.hasNext()); assertEquals(0L, t.nextLong()); assertFalse(t.hasNext()); }