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  package org.apache.commons.rng.core.source64;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  
22  
23  public class TwoCmresTest {
24      @Test
25      public void testAsymmetric() {
26          final int index1 = 2;
27          final int index2 = 5;
28          final int seed = -123456789;
29  
30          final TwoCmres rng1 = new TwoCmres(seed, index1, index2);
31          final TwoCmres rng2 = new TwoCmres(seed, index2, index1);
32  
33          // Try a few values.
34          final int n = 1000;
35          for (int i = 0; i < n; i++) {
36              Assert.assertNotEquals("i=" + i, rng1.nextLong(), rng2.nextLong());
37          }
38      }
39  
40      @Test
41      public void testSubcycleGeneratorsMustBeDifferent() {
42          final int max = TwoCmres.numberOfSubcycleGenerators();
43          for (int i = 0; i < max; i++) {
44              try {
45                  new TwoCmres(-97845, i, i);
46                  Assert.fail("Exception expected");
47              } catch (IllegalArgumentException e) {
48                  // Expected.
49              }
50          }
51      }
52  
53      @Test
54      public void testSubcycleGeneratorsIndex() {
55          final int seed = 246810;
56  
57          // Valid indices are between 0 (included) and max (excluded).
58          final int max = TwoCmres.numberOfSubcycleGenerators();
59  
60          for (int i = 0; i < max; i++) {
61              for (int j = 0; j < max; j++) {
62                  if (i != j) { // Subcycle generators must be different.
63                      // Can be instantiated.
64                      new TwoCmres(seed, i, j);
65                  }
66              }
67          }
68  
69          for (int wrongIndex : new int[] { -1, max }) {
70              try {
71                  new TwoCmres(seed, wrongIndex, 1);
72                  Assert.fail("Exception expected for index=" + wrongIndex);
73              } catch (IndexOutOfBoundsException e) {
74                  // Expected.
75              }
76  
77              try {
78                  new TwoCmres(seed, 1, wrongIndex);
79                  Assert.fail("Exception expected for index=" + wrongIndex);
80              } catch (IndexOutOfBoundsException e) {
81                  // Expected.
82              }
83          }
84      }
85  }