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    *      http://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  package org.apache.commons.rng.simple;
19  
20  import java.util.Arrays;
21  import org.junit.Assert;
22  
23  import org.apache.commons.rng.UniformRandomProvider;
24  
25  public final class RandomAssert {
26      /**
27       * Class contains only static methods.
28       */
29      private RandomAssert() {}
30  
31      /**
32       * Exercise all methods from the UniformRandomProvider interface, and
33       * ensure that the two generators produce the same sequence.
34       *
35       * @param rng1 RNG.
36       * @param rng2 RNG.
37       */
38      public static void assertProduceSameSequence(UniformRandomProvider rng1,
39                                                   UniformRandomProvider rng2) {
40          for (int i = 0; i < 54; i++) {
41              Assert.assertTrue(rng1.nextBoolean() == rng2.nextBoolean());
42          }
43          for (int i = 0; i < 23; i++) {
44              Assert.assertEquals(rng1.nextInt(), rng2.nextInt());
45          }
46          for (int i = 0; i < 4; i++) {
47              for (int j = 0; j < 5; j++) {
48                  final int max = 107 * i + 374 * j + 11;
49                  Assert.assertEquals(rng1.nextInt(max), rng2.nextInt(max));
50              }
51          }
52          for (int i = 0; i < 23; i++) {
53              Assert.assertEquals(rng1.nextLong(), rng2.nextLong());
54          }
55          for (int i = 0; i < 4; i++) {
56              for (int j = 0; j < 5; j++) {
57                  final long max = (Long.MAX_VALUE << 2) + 107 * i + 374 * j + 11;
58                  Assert.assertEquals(rng1.nextLong(max), rng2.nextLong(max));
59              }
60          }
61          for (int i = 0; i < 103; i++) {
62              Assert.assertEquals(rng1.nextFloat(), rng2.nextFloat(), 0);
63          }
64          for (int i = 0; i < 79; i++) {
65              Assert.assertEquals(rng1.nextDouble(), rng2.nextDouble(), 0);
66          }
67  
68          final int size = 345;
69          final byte[] a1 = new byte[size];
70          final byte[] a2 = new byte[size];
71  
72          for (int i = 0; i < 3; i++) {
73              rng1.nextBytes(a1);
74              rng2.nextBytes(a2);
75              Assert.assertTrue(Arrays.equals(a1, a2));
76          }
77  
78          for (int i = 0; i < 5; i++) {
79              final int offset = 200 + i;
80              final int n = 23 + i;
81              rng1.nextBytes(a1, offset, n);
82              rng2.nextBytes(a2, offset, n);
83              Assert.assertTrue(Arrays.equals(a1, a2));
84          }
85      }
86  }