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.7 by jsr166, Mon Sep 23 17:23:50 2013 UTC vs.
Revision 1.16 by jsr166, Wed Dec 31 19:05:43 2014 UTC

# Line 3 | Line 3
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6 < import junit.framework.*;
7 < import java.util.*;
6 >
7   import java.util.SplittableRandom;
8   import java.util.concurrent.atomic.AtomicInteger;
10 import java.util.concurrent.atomic.AtomicLong;
9   import java.util.concurrent.atomic.LongAdder;
10  
11 + import junit.framework.Test;
12 + import junit.framework.TestSuite;
13 +
14   public class SplittableRandomTest extends JSR166TestCase {
15  
16      public static void main(String[] args) {
# Line 39 | Line 40 | public class SplittableRandomTest extend
40      static final int NCALLS = 10000;
41  
42      // max sampled int bound
43 <    static final int MAX_INT_BOUND = (1 << 28);
43 >    static final int MAX_INT_BOUND = (1 << 26);
44  
45      // max sampled long bound
46 <    static final long MAX_LONG_BOUND = (1L << 42);
46 >    static final long MAX_LONG_BOUND = (1L << 40);
47  
48      // Number of replications for other checks
49      static final int REPS =
# Line 89 | Line 90 | public class SplittableRandomTest extend
90       * same values for nextLong.
91       */
92      public void testSeedConstructor() {
93 <        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863)  {
93 >        for (long seed = 2; seed < MAX_LONG_BOUND; seed += 15485863) {
94              SplittableRandom sr1 = new SplittableRandom(seed);
95              SplittableRandom sr2 = new SplittableRandom(seed);
96              for (int i = 0; i < REPS; ++i)
# Line 128 | Line 129 | public class SplittableRandomTest extend
129      }
130  
131      /**
132 <     * nextInt(negative) throws IllegalArgumentException
132 >     * nextInt(non-positive) throws IllegalArgumentException
133       */
134 <    public void testNextIntBoundedNeg() {
134 >    public void testNextIntBoundNonPositive() {
135          SplittableRandom sr = new SplittableRandom();
136 <        try {
137 <            int f = sr.nextInt(-17);
138 <            shouldThrow();
139 <        } catch (IllegalArgumentException success) {}
136 >        Runnable[] throwingActions = {
137 >            () -> sr.nextInt(-17),
138 >            () -> sr.nextInt(0),
139 >            () -> sr.nextInt(Integer.MIN_VALUE),
140 >        };
141 >        assertThrows(IllegalArgumentException.class, throwingActions);
142      }
143  
144      /**
# Line 143 | Line 146 | public class SplittableRandomTest extend
146       */
147      public void testNextIntBadBounds() {
148          SplittableRandom sr = new SplittableRandom();
149 <        try {
150 <            int f = sr.nextInt(17, 2);
151 <            shouldThrow();
152 <        } catch (IllegalArgumentException success) {}
149 >        Runnable[] throwingActions = {
150 >            () -> sr.nextInt(17, 2),
151 >            () -> sr.nextInt(-42, -42),
152 >            () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE),
153 >        };
154 >        assertThrows(IllegalArgumentException.class, throwingActions);
155      }
156  
157      /**
# Line 193 | Line 198 | public class SplittableRandomTest extend
198      }
199  
200      /**
201 <     * nextLong(negative) throws IllegalArgumentException
201 >     * nextLong(non-positive) throws IllegalArgumentException
202       */
203 <    public void testNextLongBoundedNeg() {
203 >    public void testNextLongBoundNonPositive() {
204          SplittableRandom sr = new SplittableRandom();
205 <        try {
206 <            long f = sr.nextLong(-17);
207 <            shouldThrow();
208 <        } catch (IllegalArgumentException success) {}
205 >        Runnable[] throwingActions = {
206 >            () -> sr.nextLong(-17L),
207 >            () -> sr.nextLong(0L),
208 >            () -> sr.nextLong(Long.MIN_VALUE),
209 >        };
210 >        assertThrows(IllegalArgumentException.class, throwingActions);
211      }
212  
213      /**
# Line 208 | Line 215 | public class SplittableRandomTest extend
215       */
216      public void testNextLongBadBounds() {
217          SplittableRandom sr = new SplittableRandom();
218 <        try {
219 <            long f = sr.nextLong(17, 2);
220 <            shouldThrow();
221 <        } catch (IllegalArgumentException success) {}
218 >        Runnable[] throwingActions = {
219 >            () -> sr.nextLong(17L, 2L),
220 >            () -> sr.nextLong(-42L, -42L),
221 >            () -> sr.nextLong(Long.MAX_VALUE, Long.MIN_VALUE),
222 >        };
223 >        assertThrows(IllegalArgumentException.class, throwingActions);
224      }
225  
226      /**
# Line 257 | Line 266 | public class SplittableRandomTest extend
266      }
267  
268      /**
269 +     * nextDouble(non-positive) throws IllegalArgumentException
270 +     */
271 +    public void testNextDoubleBoundNonPositive() {
272 +        SplittableRandom sr = new SplittableRandom();
273 +        Runnable[] throwingActions = {
274 +            () -> sr.nextDouble(-17.0d),
275 +            () -> sr.nextDouble(0.0d),
276 +            () -> sr.nextDouble(-Double.MIN_VALUE),
277 +            () -> sr.nextDouble(Double.NEGATIVE_INFINITY),
278 +            () -> sr.nextDouble(Double.NaN),
279 +        };
280 +        assertThrows(IllegalArgumentException.class, throwingActions);
281 +    }
282 +
283 +    /**
284 +     * nextDouble(! (least < bound)) throws IllegalArgumentException
285 +     */
286 +    public void testNextDoubleBadBounds() {
287 +        SplittableRandom sr = new SplittableRandom();
288 +        Runnable[] throwingActions = {
289 +            () -> sr.nextDouble(17.0d, 2.0d),
290 +            () -> sr.nextDouble(-42.0d, -42.0d),
291 +            () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE),
292 +            () -> sr.nextDouble(Double.NaN, 0.0d),
293 +            () -> sr.nextDouble(0.0d, Double.NaN),
294 +        };
295 +        assertThrows(IllegalArgumentException.class, throwingActions);
296 +    }
297 +
298 +    // TODO: Test infinite bounds!
299 +    //() -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d),
300 +    //() -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY),
301 +
302 +    /**
303       * nextDouble(least, bound) returns least <= value < bound;
304       * repeated calls produce at least two distinct results
305       */
# Line 284 | Line 327 | public class SplittableRandomTest extend
327       */
328      public void testBadStreamSize() {
329          SplittableRandom r = new SplittableRandom();
330 <        try {
331 <            java.util.stream.IntStream x = r.ints(-1L);
332 <            shouldThrow();
333 <        } catch (IllegalArgumentException success) {}
334 <        try {
335 <            java.util.stream.IntStream x = r.ints(-1L, 2, 3);
336 <            shouldThrow();
337 <        } catch (IllegalArgumentException success) {}
338 <        try {
296 <            java.util.stream.LongStream x = r.longs(-1L);
297 <            shouldThrow();
298 <        } catch (IllegalArgumentException success) {}
299 <        try {
300 <            java.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
301 <            shouldThrow();
302 <        } catch (IllegalArgumentException success) {}
303 <        try {
304 <            java.util.stream.DoubleStream x = r.doubles(-1L);
305 <            shouldThrow();
306 <        } catch (IllegalArgumentException success) {}
307 <        try {
308 <            java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
309 <            shouldThrow();
310 <        } catch (IllegalArgumentException success) {}
330 >        Runnable[] throwingActions = {
331 >            () -> { java.util.stream.IntStream x = r.ints(-1L); },
332 >            () -> { java.util.stream.IntStream x = r.ints(-1L, 2, 3); },
333 >            () -> { java.util.stream.LongStream x = r.longs(-1L); },
334 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L, 1L); },
335 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L); },
336 >            () -> { java.util.stream.DoubleStream x = r.doubles(-1L, .5, .6); },
337 >        };
338 >        assertThrows(IllegalArgumentException.class, throwingActions);
339      }
340  
341      /**
# Line 316 | Line 344 | public class SplittableRandomTest extend
344       */
345      public void testBadStreamBounds() {
346          SplittableRandom r = new SplittableRandom();
347 <        try {
348 <            java.util.stream.IntStream x = r.ints(2, 1);
349 <            shouldThrow();
350 <        } catch (IllegalArgumentException success) {}
351 <        try {
352 <            java.util.stream.IntStream x = r.ints(10, 42, 42);
353 <            shouldThrow();
354 <        } catch (IllegalArgumentException success) {}
355 <        try {
328 <            java.util.stream.LongStream x = r.longs(-1L, -1L);
329 <            shouldThrow();
330 <        } catch (IllegalArgumentException success) {}
331 <        try {
332 <            java.util.stream.LongStream x = r.longs(10, 1L, -2L);
333 <            shouldThrow();
334 <        } catch (IllegalArgumentException success) {}
335 <        try {
336 <            java.util.stream.DoubleStream x = r.doubles(0.0, 0.0);
337 <            shouldThrow();
338 <        } catch (IllegalArgumentException success) {}
339 <        try {
340 <            java.util.stream.DoubleStream x = r.doubles(10, .5, .4);
341 <            shouldThrow();
342 <        } catch (IllegalArgumentException success) {}
347 >        Runnable[] throwingActions = {
348 >            () -> { java.util.stream.IntStream x = r.ints(2, 1); },
349 >            () -> { java.util.stream.IntStream x = r.ints(10, 42, 42); },
350 >            () -> { java.util.stream.LongStream x = r.longs(-1L, -1L); },
351 >            () -> { java.util.stream.LongStream x = r.longs(10, 1L, -2L); },
352 >            () -> { java.util.stream.DoubleStream x = r.doubles(0.0, 0.0); },
353 >            () -> { java.util.stream.DoubleStream x = r.doubles(10, .5, .4); },
354 >        };
355 >        assertThrows(IllegalArgumentException.class, throwingActions);
356      }
357  
358      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines