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