ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SplittableRandomTest.java
(Generate patch)

Comparing jsr166/src/test/tck/SplittableRandomTest.java (file contents):
Revision 1.3 by jsr166, Fri Jul 12 01:14:30 2013 UTC vs.
Revision 1.9 by jsr166, Tue Sep 24 06:35:35 2013 UTC

# Line 22 | Line 22 | public class SplittableRandomTest extend
22      /*
23       * Testing coverage notes:
24       *
25 <     * 1. Many of the test methods are adapted from ThreadLocalRandomTest
25 >     * 1. Many of the test methods are adapted from ThreadLocalRandomTest.
26       *
27 <     * 2. This set of tests do not check for random number generator
28 <     * quality. But we check for minimal API compliance by requiring
29 <     * that repeated calls to nextX methods, up to NCALLS tries,
30 <     * produce at least one different result. (In some possible
31 <     * universe, a "correct" implementation might fail, but the odds
32 <     * are vastly less than that of encountering a hardware failure
33 <     * while running the test.) For bounded nextX methods, we sample
34 <     * various intervals across multiples of primes. In other tests,
35 <     * we repeat under REPS different values.
27 >     * 2. These tests do not check for random number generator quality.
28 >     * But we check for minimal API compliance by requiring that
29 >     * repeated calls to nextX methods, up to NCALLS tries, produce at
30 >     * least two distinct results. (In some possible universe, a
31 >     * "correct" implementation might fail, but the odds are vastly
32 >     * less than that of encountering a hardware failure while running
33 >     * the test.) For bounded nextX methods, we sample various
34 >     * intervals across multiples of primes. In other tests, we repeat
35 >     * under REPS different values.
36       */
37  
38      // max numbers of calls to detect getting stuck on one value
# Line 41 | Line 41 | public class SplittableRandomTest extend
41      // max sampled int bound
42      static final int MAX_INT_BOUND = (1 << 28);
43  
44 <    // Max sampled long bound
44 >    // max sampled long bound
45      static final long MAX_LONG_BOUND = (1L << 42);
46  
47      // Number of replications for other checks
48 <    static final int REPS = 20;
48 >    static final int REPS =
49 >        Integer.getInteger("SplittableRandomTest.reps", 4);
50  
51      /**
52 <     * Repeated calls to nextInt produce at least one different result
52 >     * Repeated calls to nextInt produce at least two distinct results
53       */
54      public void testNextInt() {
55          SplittableRandom sr = new SplittableRandom();
# Line 60 | Line 61 | public class SplittableRandomTest extend
61      }
62  
63      /**
64 <     * Repeated calls to nextLong produce at least one different result
64 >     * Repeated calls to nextLong produce at least two distinct results
65       */
66      public void testNextLong() {
67          SplittableRandom sr = new SplittableRandom();
# Line 72 | Line 73 | public class SplittableRandomTest extend
73      }
74  
75      /**
76 <     * Repeated calls to nextDouble produce at least one different result
76 >     * Repeated calls to nextDouble produce at least two distinct results
77       */
78      public void testNextDouble() {
79          SplittableRandom sr = new SplittableRandom();
80          double f = sr.nextDouble();
81 <        double i = 0;
81 >        int i = 0;
82          while (i < NCALLS && sr.nextDouble() == f)
83              ++i;
84          assertTrue(i < NCALLS);
# Line 127 | Line 128 | public class SplittableRandomTest extend
128      }
129  
130      /**
131 <     * nextInt(negative) throws IllegalArgumentException;
131 >     * nextInt(non-positive) throws IllegalArgumentException
132       */
133 <    public void testNextIntBoundedNeg() {
133 >    public void testNextIntNonPositive() {
134          SplittableRandom sr = new SplittableRandom();
135 <        try {
136 <            int f = sr.nextInt(-17);
137 <            shouldThrow();
138 <        } catch (IllegalArgumentException success) {}
135 >        Runnable[] throwingActions = {
136 >            () -> sr.nextInt(-17),
137 >            () -> sr.nextInt(0),
138 >            () -> sr.nextInt(Integer.MIN_VALUE),
139 >        };
140 >        assertThrows(IllegalArgumentException.class, throwingActions);
141      }
142  
143      /**
144 <     * nextInt(least >= bound) throws IllegalArgumentException;
144 >     * nextInt(least >= bound) throws IllegalArgumentException
145       */
146      public void testNextIntBadBounds() {
147          SplittableRandom sr = new SplittableRandom();
# Line 150 | Line 153 | public class SplittableRandomTest extend
153  
154      /**
155       * nextInt(bound) returns 0 <= value < bound;
156 <     * repeated calls produce at least one different result
156 >     * repeated calls produce at least two distinct results
157       */
158      public void testNextIntBounded() {
159          SplittableRandom sr = new SplittableRandom();
# Line 171 | Line 174 | public class SplittableRandomTest extend
174  
175      /**
176       * nextInt(least, bound) returns least <= value < bound;
177 <     * repeated calls produce at least one different result
177 >     * repeated calls produce at least two distinct results
178       */
179      public void testNextIntBounded2() {
180          SplittableRandom sr = new SplittableRandom();
# Line 192 | Line 195 | public class SplittableRandomTest extend
195      }
196  
197      /**
198 <     * nextLong(negative) throws IllegalArgumentException;
198 >     * nextLong(non-positive) throws IllegalArgumentException
199       */
200 <    public void testNextLongBoundedNeg() {
200 >    public void testNextLongNonPositive() {
201          SplittableRandom sr = new SplittableRandom();
202 <        try {
203 <            long f = sr.nextLong(-17);
204 <            shouldThrow();
205 <        } catch (IllegalArgumentException success) {}
202 >        Runnable[] throwingActions = {
203 >            () -> sr.nextLong(-17L),
204 >            () -> sr.nextLong(0L),
205 >            () -> sr.nextLong(Long.MIN_VALUE),
206 >        };
207 >        assertThrows(IllegalArgumentException.class, throwingActions);
208      }
209  
210      /**
211 <     * nextLong(least >= bound) throws IllegalArgumentException;
211 >     * nextLong(least >= bound) throws IllegalArgumentException
212       */
213      public void testNextLongBadBounds() {
214          SplittableRandom sr = new SplittableRandom();
# Line 215 | Line 220 | public class SplittableRandomTest extend
220  
221      /**
222       * nextLong(bound) returns 0 <= value < bound;
223 <     * repeated calls produce at least one different result
223 >     * repeated calls produce at least two distinct results
224       */
225      public void testNextLongBounded() {
226          SplittableRandom sr = new SplittableRandom();
# Line 235 | Line 240 | public class SplittableRandomTest extend
240  
241      /**
242       * nextLong(least, bound) returns least <= value < bound;
243 <     * repeated calls produce at least one different result
243 >     * repeated calls produce at least two distinct results
244       */
245      public void testNextLongBounded2() {
246          SplittableRandom sr = new SplittableRandom();
# Line 256 | Line 261 | public class SplittableRandomTest extend
261      }
262  
263      /**
264 +     * nextDouble(non-positive) throws IllegalArgumentException
265 +     */
266 +    public void testNextDoubleNonPositive() {
267 +        SplittableRandom sr = new SplittableRandom();
268 +        Runnable[] throwingActions = {
269 +            () -> sr.nextDouble(-17.0d),
270 +            () -> sr.nextDouble(0.0d),
271 +            () -> sr.nextDouble(-Double.MIN_VALUE),
272 +            () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
273 +            () -> sr.nextDouble(Double.NaN),
274 +        };
275 +        assertThrows(IllegalArgumentException.class, throwingActions);
276 +    }
277 +
278 +    /**
279       * nextDouble(least, bound) returns least <= value < bound;
280 <     * repeated calls produce at least one different result
280 >     * repeated calls produce at least two distinct results
281       */
282      public void testNextDoubleBounded2() {
283          SplittableRandom sr = new SplittableRandom();
# Line 283 | Line 303 | public class SplittableRandomTest extend
303       */
304      public void testBadStreamSize() {
305          SplittableRandom r = new SplittableRandom();
306 <        try {
307 <            java.util.stream.IntStream x = r.ints(-1L);
308 <            shouldThrow();
309 <        } catch (IllegalArgumentException ok) {
310 <        }
311 <        try {
312 <            java.util.stream.LongStream x = r.longs(-1L);
313 <            shouldThrow();
314 <        } catch (IllegalArgumentException ok) {
295 <        }
296 <        try {
297 <            java.util.stream.DoubleStream x = r.doubles(-1L);
298 <            shouldThrow();
299 <        } catch (IllegalArgumentException ok) {
300 <        }
306 >        Runnable[] throwingActions = {
307 >            () -> { java.util.stream.IntStream x = r.ints(-1L); },
308 >            () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
309 >            () -> { java.util.stream.LongStream x = r.longs(-1L); },
310 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
311 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
312 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
313 >        };
314 >        assertThrows(IllegalArgumentException.class, throwingActions);
315      }
316  
317      /**
# Line 306 | Line 320 | public class SplittableRandomTest extend
320       */
321      public void testBadStreamBounds() {
322          SplittableRandom r = new SplittableRandom();
323 <        try {
324 <            java.util.stream.IntStream x = r.ints(2, 1);
325 <            shouldThrow();
326 <        } catch (IllegalArgumentException ok) {
327 <        }
328 <        try {
329 <            java.util.stream.LongStream x = r.longs(1, -2);
330 <            shouldThrow();
331 <        } catch (IllegalArgumentException ok) {
318 <        }
319 <        try {
320 <            java.util.stream.DoubleStream x = r.doubles(0, 0);
321 <            shouldThrow();
322 <        } catch (IllegalArgumentException ok) {
323 <        }
323 >        Runnable[] throwingActions = {
324 >            () -> { java.util.stream.IntStream x = r.ints(2, 1); },
325 >            () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
326 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
327 >            () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
328 >            () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
329 >            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
330 >        };
331 >        assertThrows(IllegalArgumentException.class, throwingActions);
332      }
333  
334      /**
# Line 332 | Line 340 | public class SplittableRandomTest extend
340          long size = 0;
341          for (int reps = 0; reps < REPS; ++reps) {
342              counter.reset();
343 <            r.ints(size).parallel().forEach(x -> {counter.increment();});
344 <            assertEquals(counter.sum(), size);
343 >            r.ints(size).parallel().forEach(x -> counter.increment());
344 >            assertEquals(size, counter.sum());
345              size += 524959;
346          }
347      }
# Line 347 | Line 355 | public class SplittableRandomTest extend
355          long size = 0;
356          for (int reps = 0; reps < REPS; ++reps) {
357              counter.reset();
358 <            r.longs(size).parallel().forEach(x -> {counter.increment();});
359 <            assertEquals(counter.sum(), size);
358 >            r.longs(size).parallel().forEach(x -> counter.increment());
359 >            assertEquals(size, counter.sum());
360              size += 524959;
361          }
362      }
# Line 362 | Line 370 | public class SplittableRandomTest extend
370          long size = 0;
371          for (int reps = 0; reps < REPS; ++reps) {
372              counter.reset();
373 <            r.doubles(size).parallel().forEach(x -> {counter.increment();});
374 <            assertEquals(counter.sum(), size);
373 >            r.doubles(size).parallel().forEach(x -> counter.increment());
374 >            assertEquals(size, counter.sum());
375              size += 524959;
376          }
377      }
378  
371
379      /**
380       * Each of a parallel sized stream of bounded ints is within bounds
381       */
# Line 384 | Line 391 | public class SplittableRandomTest extend
391                                  fails.getAndIncrement(); });
392              }
393          }
394 <        assertEquals(fails.get(), 0);
394 >        assertEquals(0, fails.get());
395      }
396  
397      /**
# Line 402 | Line 409 | public class SplittableRandomTest extend
409                                  fails.getAndIncrement(); });
410              }
411          }
412 <        assertEquals(fails.get(), 0);
412 >        assertEquals(0, fails.get());
413      }
414  
415      /**
# Line 420 | Line 427 | public class SplittableRandomTest extend
427                                  fails.getAndIncrement(); });
428              }
429          }
430 <        assertEquals(fails.get(), 0);
430 >        assertEquals(0, fails.get());
431      }
432  
433      /**
# Line 430 | Line 437 | public class SplittableRandomTest extend
437          LongAdder counter = new LongAdder();
438          SplittableRandom r = new SplittableRandom();
439          long size = 100;
440 <        r.ints().limit(size).parallel().forEach(x -> {counter.increment();});
441 <        assertEquals(counter.sum(), size);
440 >        r.ints().limit(size).parallel().forEach(x -> counter.increment());
441 >        assertEquals(size, counter.sum());
442      }
443  
444      /**
# Line 441 | Line 448 | public class SplittableRandomTest extend
448          LongAdder counter = new LongAdder();
449          SplittableRandom r = new SplittableRandom();
450          long size = 100;
451 <        r.longs().limit(size).parallel().forEach(x -> {counter.increment();});
452 <        assertEquals(counter.sum(), size);
451 >        r.longs().limit(size).parallel().forEach(x -> counter.increment());
452 >        assertEquals(size, counter.sum());
453      }
454  
448
455      /**
456       * A parallel unsized stream of doubles generates at least 100 values
457       */
# Line 453 | Line 459 | public class SplittableRandomTest extend
459          LongAdder counter = new LongAdder();
460          SplittableRandom r = new SplittableRandom();
461          long size = 100;
462 <        r.doubles().limit(size).parallel().forEach(x -> {counter.increment();});
463 <        assertEquals(counter.sum(), size);
462 >        r.doubles().limit(size).parallel().forEach(x -> counter.increment());
463 >        assertEquals(size, counter.sum());
464      }
465  
466      /**
# Line 464 | Line 470 | public class SplittableRandomTest extend
470          LongAdder counter = new LongAdder();
471          SplittableRandom r = new SplittableRandom();
472          long size = 100;
473 <        r.ints().limit(size).forEach(x -> {counter.increment();});
474 <        assertEquals(counter.sum(), size);
473 >        r.ints().limit(size).forEach(x -> counter.increment());
474 >        assertEquals(size, counter.sum());
475      }
476  
477      /**
# Line 475 | Line 481 | public class SplittableRandomTest extend
481          LongAdder counter = new LongAdder();
482          SplittableRandom r = new SplittableRandom();
483          long size = 100;
484 <        r.longs().limit(size).forEach(x -> {counter.increment();});
485 <        assertEquals(counter.sum(), size);
484 >        r.longs().limit(size).forEach(x -> counter.increment());
485 >        assertEquals(size, counter.sum());
486      }
487  
482
488      /**
489       * A sequential unsized stream of doubles generates at least 100 values
490       */
# Line 487 | Line 492 | public class SplittableRandomTest extend
492          LongAdder counter = new LongAdder();
493          SplittableRandom r = new SplittableRandom();
494          long size = 100;
495 <        r.doubles().limit(size).forEach(x -> {counter.increment();});
496 <        assertEquals(counter.sum(), size);
495 >        r.doubles().limit(size).forEach(x -> counter.increment());
496 >        assertEquals(size, counter.sum());
497      }
498  
494
499   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines