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 java.util.ArrayList;
25  import java.util.List;
26  import java.util.concurrent.Callable;
27  import java.util.concurrent.ExecutionException;
28  import java.util.concurrent.ExecutorService;
29  import java.util.concurrent.Executors;
30  import java.util.concurrent.Future;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  public class SynchronizedRandomGeneratorTest {
36  
37      @Test
38      public void testAdapter() {
39          final int seed = 12345;
40          final RandomGenerator orig = new MersenneTwister(seed);
41          final RandomGenerator wrap
42              = new SynchronizedRandomGenerator(new MersenneTwister(seed));
43  
44          final int bSize = 67;
45          final byte[] bOrig = new byte[bSize];
46          final byte[] bWrap = new byte[bSize];
47  
48          for (int i = 0; i < 100; i++) {
49              orig.nextBytes(bOrig);
50              wrap.nextBytes(bWrap);
51              for (int k = 0; k < bSize; k++) {
52                  Assert.assertEquals(bOrig[k], bWrap[k]);
53              }
54  
55              Assert.assertEquals(orig.nextInt(), wrap.nextInt());
56  
57              final int range = (i + 1) * 89;
58              Assert.assertEquals(orig.nextInt(range), wrap.nextInt(range));
59  
60              Assert.assertEquals(orig.nextLong(), wrap.nextLong());
61              Assert.assertEquals(orig.nextBoolean(), wrap.nextBoolean());
62              Assert.assertEquals(orig.nextFloat(), wrap.nextFloat(), 0);
63              Assert.assertEquals(orig.nextDouble(), wrap.nextDouble(), 0);
64              Assert.assertEquals(orig.nextGaussian(), wrap.nextGaussian(), 0);
65  
66          }
67      }
68  
69      @Test
70      public void testMath899Sync() throws Throwable {
71          try {
72              final int numberOfThreads = 5;
73              final int numberOfGenerators = 5;
74              final int numberOfSamples = 100000;
75              // Running the test several times in order to decrease the
76              // probability that a non-thread-safe code did not trigger
77              // a concurrency problem.
78              for (int i = 0; i < 10; i++) {
79                  doTestMath899(true, numberOfThreads, numberOfGenerators,
80                                numberOfSamples);
81              }
82          } catch (InterruptedException e) {
83              Assert.fail(e.getMessage());
84          } catch (ExecutionException e) {
85              throw e.getCause();
86          }
87      }
88  
89      /**
90       * @param sync Whether to use a synchronizing wrapper.
91       */
92      private double[] doTestMath899(final boolean sync,
93                                     final int numThreads,
94                                     final int numGenerators,
95                                     final int numSamples)
96          throws InterruptedException,
97                 ExecutionException {
98          final RandomGenerator rng = new MersenneTwister();
99          final RandomGenerator wrapper = sync ? new SynchronizedRandomGenerator(rng) : rng;
100 
101         final List<Callable<Double>> tasks = new ArrayList<Callable<Double>>();
102         for (int i = 0; i < numGenerators; i++) {
103             tasks.add(new Callable<Double>() {
104                     @Override
105                     public Double call() {
106                         Double lastValue = 0d;
107                         for (int j = 0; j < numSamples; j++) {
108                             lastValue = wrapper.nextGaussian();
109                         }
110                         return lastValue;
111                     }
112                 });
113         }
114 
115         final ExecutorService exec = Executors.newFixedThreadPool(numThreads);
116         final List<Future<Double>> results = exec.invokeAll(tasks);
117 
118         final double[] values = new double[numGenerators];
119         for (int i = 0; i < numGenerators; i++) {
120             values[i] = results.get(i).get();
121         }
122         return values;
123     }
124 }