View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.random;
23  
24  import org.junit.jupiter.api.Test;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.concurrent.Callable;
29  import java.util.concurrent.ExecutionException;
30  import java.util.concurrent.ExecutorService;
31  import java.util.concurrent.Executors;
32  import java.util.concurrent.Future;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  class SynchronizedRandomGeneratorTest {
38  
39      @Test
40      void testAdapter() {
41          final int seed = 12345;
42          final RandomGenerator orig = new MersenneTwister(seed);
43          final RandomGenerator wrap
44              = new SynchronizedRandomGenerator(new MersenneTwister(seed));
45  
46          final int bSize = 67;
47          final byte[] bOrig = new byte[bSize];
48          final byte[] bWrap = new byte[bSize];
49  
50          for (int i = 0; i < 100; i++) {
51              orig.nextBytes(bOrig);
52              wrap.nextBytes(bWrap);
53              for (int k = 0; k < bSize; k++) {
54                  assertEquals(bOrig[k], bWrap[k]);
55              }
56  
57              assertEquals(orig.nextInt(), wrap.nextInt());
58  
59              final int range = (i + 1) * 89;
60              assertEquals(orig.nextInt(range), wrap.nextInt(range));
61  
62              assertEquals(orig.nextLong(), wrap.nextLong());
63              assertEquals(orig.nextBoolean(), wrap.nextBoolean());
64              assertEquals(orig.nextFloat(), wrap.nextFloat(), 0);
65              assertEquals(orig.nextDouble(), wrap.nextDouble(), 0);
66              assertEquals(orig.nextGaussian(), wrap.nextGaussian(), 0);
67  
68          }
69      }
70  
71      @Test
72      void testMath899Sync() throws Throwable {
73          try {
74              final int numberOfThreads = 5;
75              final int numberOfGenerators = 5;
76              final int numberOfSamples = 100000;
77              // Running the test several times in order to decrease the
78              // probability that a non-thread-safe code did not trigger
79              // a concurrency problem.
80              for (int i = 0; i < 10; i++) {
81                  doTestMath899(true, numberOfThreads, numberOfGenerators,
82                                numberOfSamples);
83              }
84          } catch (InterruptedException e) {
85              fail(e.getMessage());
86          } catch (ExecutionException e) {
87              throw e.getCause();
88          }
89      }
90  
91      /**
92       * @param sync Whether to use a synchronizing wrapper.
93       */
94      private double[] doTestMath899(final boolean sync,
95                                     final int numThreads,
96                                     final int numGenerators,
97                                     final int numSamples)
98          throws InterruptedException,
99                 ExecutionException {
100         final RandomGenerator rng = new MersenneTwister();
101         final RandomGenerator wrapper = sync ? new SynchronizedRandomGenerator(rng) : rng;
102 
103         final List<Callable<Double>> tasks = new ArrayList<Callable<Double>>();
104         for (int i = 0; i < numGenerators; i++) {
105             tasks.add(new Callable<Double>() {
106                     @Override
107                     public Double call() {
108                         Double lastValue = 0d;
109                         for (int j = 0; j < numSamples; j++) {
110                             lastValue = wrapper.nextGaussian();
111                         }
112                         return lastValue;
113                     }
114                 });
115         }
116 
117         final ExecutorService exec = Executors.newFixedThreadPool(numThreads);
118         final List<Future<Double>> results = exec.invokeAll(tasks);
119 
120         final double[] values = new double[numGenerators];
121         for (int i = 0; i < numGenerators; i++) {
122             values[i] = results.get(i).get();
123         }
124         return values;
125     }
126 }