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