jdk/test/java/util/Random/RandomStreamTest.java
author psandoz
Wed, 28 Aug 2013 22:11:14 +0200
changeset 19609 108f52a7438f
parent 19073 a4d1398d9897
child 30046 cf2c86e1819e
permissions -rw-r--r--
8023155: Ensure functional consistency across Random, ThreadLocalRandom, SplittableRandom Reviewed-by: mduigou Contributed-by: Doug Lea <dl@cs.oswego.edu>, Paul Sandoz <paul.sandoz@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     1
/*
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     4
 *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     8
 *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    13
 * accompanied this code).
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    14
 *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    18
 *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    21
 * questions.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    22
 */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    23
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    24
import org.testng.annotations.DataProvider;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    25
import org.testng.annotations.Test;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    26
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    27
import java.security.SecureRandom;
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    28
import java.util.ArrayList;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    29
import java.util.List;
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    30
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    31
import java.util.Random;
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    32
import java.util.Set;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    33
import java.util.concurrent.CompletableFuture;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    34
import java.util.concurrent.ExecutionException;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    35
import java.util.concurrent.ThreadLocalRandom;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    36
import java.util.concurrent.TimeUnit;
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    37
import java.util.concurrent.TimeoutException;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    38
import java.util.function.Supplier;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    39
import java.util.stream.Stream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    40
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    41
import static java.util.stream.Collectors.toList;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    42
import static java.util.stream.Collectors.toSet;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
    43
import static org.testng.Assert.*;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    44
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    45
/**
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    46
 * @test
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    47
 * @run testng RandomStreamTest
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    48
 * @summary test stream methods on Random
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    49
 * @author Brian Goetz
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    50
 */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    51
public class RandomStreamTest {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    52
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    53
    private static final int SIZE = 1000;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    54
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    55
    @DataProvider(name = "suppliers")
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    56
    public Object[][] randomSuppliers() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    57
        return new Object[][] {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    58
            {new Random(), SIZE},
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    59
            {new SecureRandom(), SIZE}
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    60
        };
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    61
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    62
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    63
    @Test(dataProvider = "suppliers")
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    64
    public void testRandomIntStream(final Random random, final int count) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    65
        final List<Integer> destination = new ArrayList<>(count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    66
        random.ints().limit(count).forEach(destination::add);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    67
        assertEquals(destination.size(), count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    68
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    69
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    70
    @Test(dataProvider = "suppliers")
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    71
    public void testRandomLongStream(final Random random, final int count) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    72
        final List<Long> destination = new ArrayList<>(count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    73
        random.longs().limit(count).forEach(destination::add);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    74
        assertEquals(destination.size(), count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    75
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    76
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    77
    @Test(dataProvider = "suppliers")
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    78
    public void testRandomDoubleStream(final Random random, final int count) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    79
        final List<Double> destination = new ArrayList<>(count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    80
        random.doubles().limit(count).forEach(destination::add);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    81
        random.doubles().limit(count).forEach(d -> assertTrue(d >= 0.0 && d < 1.0));
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    82
        assertEquals(destination.size(), count);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    83
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    84
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    85
    @Test
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    86
    public void testIntStream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    87
        final long seed = System.currentTimeMillis();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    88
        final Random r1 = new Random(seed);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    89
        final int[] a = new int[SIZE];
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    90
        for (int i=0; i < SIZE; i++) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    91
            a[i] = r1.nextInt();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    92
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    93
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    94
        final Random r2 = new Random(seed); // same seed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    95
        final int[] b = r2.ints().limit(SIZE).toArray();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    96
        assertEquals(a, b);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    97
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    98
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
    99
    @Test
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   100
    public void testLongStream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   101
        final long seed = System.currentTimeMillis();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   102
        final Random r1 = new Random(seed);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   103
        final long[] a = new long[SIZE];
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   104
        for (int i=0; i < SIZE; i++) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   105
            a[i] = r1.nextLong();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   106
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   107
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   108
        final Random r2 = new Random(seed); // same seed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   109
        final long[] b = r2.longs().limit(SIZE).toArray();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   110
        assertEquals(a, b);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   111
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   112
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   113
    @Test
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   114
    public void testDoubleStream() {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   115
        final long seed = System.currentTimeMillis();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   116
        final Random r1 = new Random(seed);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   117
        final double[] a = new double[SIZE];
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   118
        for (int i=0; i < SIZE; i++) {
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   119
            a[i] = r1.nextDouble();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   120
        }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   121
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   122
        final Random r2 = new Random(seed); // same seed
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   123
        final double[] b = r2.doubles().limit(SIZE).toArray();
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   124
        assertEquals(a, b);
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   125
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   126
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   127
    @Test
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   128
    public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   129
        ThreadLocalRandom tlr = ThreadLocalRandom.current();
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   130
        testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   131
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   132
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   133
    @Test
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   134
    public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   135
        ThreadLocalRandom tlr = ThreadLocalRandom.current();
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   136
        testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   137
    }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   138
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   139
    @Test
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   140
    public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   141
        ThreadLocalRandom tlr = ThreadLocalRandom.current();
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   142
        testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   143
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   144
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   145
    <T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException {
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   146
        // Produce 10 completable future tasks
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   147
        final int tasks = 10;
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   148
        List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)).
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   149
                limit(tasks).collect(toList());
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   150
19073
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   151
        // Wait for all tasks to complete
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   152
        // Timeout is beyond reasonable doubt that completion should
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   153
        // have occurred unless there is an issue
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   154
        CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new));
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   155
        all.get(1, TimeUnit.MINUTES);
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   156
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   157
        // Count the distinct results, which should equal the number of tasks
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   158
        long rc = cfs.stream().map(CompletableFuture::join).distinct().count();
a4d1398d9897 8021883: j.u.Random/RandomStream.java test needs more robust timeout duration
psandoz
parents: 17421
diff changeset
   159
        assertEquals(rc, tasks);
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   160
    }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents:
diff changeset
   161
}