Path: blob/master/test/jdk/java/util/Random/RandomStreamTest.java
41149 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.security.SecureRandom;27import java.util.ArrayList;28import java.util.List;2930import java.util.Random;31import java.util.Set;32import java.util.concurrent.CompletableFuture;33import java.util.concurrent.ExecutionException;34import java.util.concurrent.ThreadLocalRandom;35import java.util.concurrent.TimeUnit;36import java.util.concurrent.TimeoutException;37import java.util.function.Supplier;38import java.util.stream.Stream;3940import static java.util.stream.Collectors.toList;41import static java.util.stream.Collectors.toSet;42import static org.testng.Assert.*;4344/**45* @test46* @run testng RandomStreamTest47* @summary test stream methods on Random48* @author Brian Goetz49* @key randomness50*/51public class RandomStreamTest {5253private static final int SIZE = 1000;5455@DataProvider(name = "suppliers")56public Object[][] randomSuppliers() {57return new Object[][] {58{new Random(), SIZE},59{new SecureRandom(), SIZE}60};61}6263@Test(dataProvider = "suppliers")64public void testRandomIntStream(final Random random, final int count) {65final List<Integer> destination = new ArrayList<>(count);66random.ints().limit(count).forEach(destination::add);67assertEquals(destination.size(), count);68}6970@Test(dataProvider = "suppliers")71public void testRandomLongStream(final Random random, final int count) {72final List<Long> destination = new ArrayList<>(count);73random.longs().limit(count).forEach(destination::add);74assertEquals(destination.size(), count);75}7677@Test(dataProvider = "suppliers")78public void testRandomDoubleStream(final Random random, final int count) {79final List<Double> destination = new ArrayList<>(count);80random.doubles().limit(count).forEach(destination::add);81random.doubles().limit(count).forEach(d -> assertTrue(d >= 0.0 && d < 1.0));82assertEquals(destination.size(), count);83}8485@Test86public void testIntStream() {87final long seed = System.currentTimeMillis();88final Random r1 = new Random(seed);89final int[] a = new int[SIZE];90for (int i=0; i < SIZE; i++) {91a[i] = r1.nextInt();92}9394final Random r2 = new Random(seed); // same seed95final int[] b = r2.ints().limit(SIZE).toArray();96assertEquals(a, b);97}9899@Test100public void testLongStream() {101final long seed = System.currentTimeMillis();102final Random r1 = new Random(seed);103final long[] a = new long[SIZE];104for (int i=0; i < SIZE; i++) {105a[i] = r1.nextLong();106}107108final Random r2 = new Random(seed); // same seed109final long[] b = r2.longs().limit(SIZE).toArray();110assertEquals(a, b);111}112113@Test114public void testDoubleStream() {115final long seed = System.currentTimeMillis();116final Random r1 = new Random(seed);117final double[] a = new double[SIZE];118for (int i=0; i < SIZE; i++) {119a[i] = r1.nextDouble();120}121122final Random r2 = new Random(seed); // same seed123final double[] b = r2.doubles().limit(SIZE).toArray();124assertEquals(a, b);125}126127@Test128public void testThreadLocalIntStream() throws InterruptedException, ExecutionException, TimeoutException {129ThreadLocalRandom tlr = ThreadLocalRandom.current();130testRandomResultSupplierConcurrently(() -> tlr.ints().limit(SIZE).boxed().collect(toList()));131}132133@Test134public void testThreadLocalLongStream() throws InterruptedException, ExecutionException, TimeoutException {135ThreadLocalRandom tlr = ThreadLocalRandom.current();136testRandomResultSupplierConcurrently(() -> tlr.longs().limit(SIZE).boxed().collect(toList()));137}138139@Test140public void testThreadLocalDoubleStream() throws InterruptedException, ExecutionException, TimeoutException {141ThreadLocalRandom tlr = ThreadLocalRandom.current();142testRandomResultSupplierConcurrently(() -> tlr.doubles().limit(SIZE).boxed().collect(toList()));143}144145<T> void testRandomResultSupplierConcurrently(Supplier<T> s) throws InterruptedException, ExecutionException, TimeoutException {146// Produce 10 completable future tasks147final int tasks = 10;148List<CompletableFuture<T>> cfs = Stream.generate(() -> CompletableFuture.supplyAsync(s)).149limit(tasks).collect(toList());150151// Wait for all tasks to complete152// Timeout is beyond reasonable doubt that completion should153// have occurred unless there is an issue154CompletableFuture<Void> all = CompletableFuture.allOf(cfs.stream().toArray(CompletableFuture[]::new));155all.get(1, TimeUnit.MINUTES);156157// Count the distinct results, which should equal the number of tasks158long rc = cfs.stream().map(CompletableFuture::join).distinct().count();159assertEquals(rc, tasks);160}161}162163164