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.apache.commons.rng.core.RandomAssert;
20  import org.apache.commons.rng.core.source64.TwoCmres.Cmres;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  import java.util.ArrayList;
25  
26  
27  public class TwoCmresTest {
28      @Test
29      public void testAsymmetric() {
30          final int index1 = 2;
31          final int index2 = 5;
32          final int seed = -123456789;
33  
34          final TwoCmres rng1 = new TwoCmres(seed, index1, index2);
35          final TwoCmres rng2 = new TwoCmres(seed, index2, index1);
36  
37          // Try a few values.
38          final int n = 1000;
39          for (int i = 0; i < n; i++) {
40              Assert.assertNotEquals("i=" + i, rng1.nextLong(), rng2.nextLong());
41          }
42      }
43  
44      /**
45       * This test targets the seeding procedure to verify any bit of the input seed contributes
46       * to the output. Note: The seeding routine creates 2 16-bit integers from the 32-bit seed,
47       * thus a change of any single bit should make a different output.
48       */
49      @Test
50      public void testSeedingWithASingleBitProducesDifferentOutputFromZeroSeed() {
51          final int n = 100;
52  
53          // Output with a zero seed
54          final long[] values = new long[n];
55          final TwoCmres rng = new TwoCmres(0);
56          for (int i = 0; i < n; i++) {
57              values[i] = rng.nextLong();
58          }
59  
60          // Seed with a single bit
61          for (int bit = 0; bit < 32; bit++) {
62              final int seed = 1 << bit;
63              RandomAssert.assertNotEquals(values, new TwoCmres(seed));
64          }
65      }
66  
67      @Test
68      public void testSubcycleGeneratorsMustBeDifferent() {
69          final int max = TwoCmres.numberOfSubcycleGenerators();
70          for (int i = 0; i < max; i++) {
71              try {
72                  new TwoCmres(-97845, i, i);
73                  Assert.fail("Exception expected");
74              } catch (IllegalArgumentException e) {
75                  // Expected.
76              }
77          }
78      }
79  
80      @Test
81      public void testSubcycleGeneratorsIndex() {
82          final int seed = 246810;
83  
84          // Valid indices are between 0 (included) and max (excluded).
85          final int max = TwoCmres.numberOfSubcycleGenerators();
86  
87          for (int i = 0; i < max; i++) {
88              for (int j = 0; j < max; j++) {
89                  if (i != j) { // Subcycle generators must be different.
90                      // Can be instantiated.
91                      new TwoCmres(seed, i, j);
92                  }
93              }
94          }
95  
96          for (int wrongIndex : new int[] {-1, max}) {
97              try {
98                  new TwoCmres(seed, wrongIndex, 1);
99                  Assert.fail("Exception expected for index=" + wrongIndex);
100             } catch (IndexOutOfBoundsException e) {
101                 // Expected.
102             }
103 
104             try {
105                 new TwoCmres(seed, 1, wrongIndex);
106                 Assert.fail("Exception expected for index=" + wrongIndex);
107             } catch (IndexOutOfBoundsException e) {
108                 // Expected.
109             }
110         }
111     }
112 
113     @Test(expected = IllegalStateException.class)
114     public void testCmresFactoryThrowsWithDuplicateMultiplier() {
115         ArrayList<Cmres> list = new ArrayList<Cmres>();
116         final long multiply = 0;
117         final int rotate = 3;
118         final int start = 5;
119 
120         list.add(new Cmres(multiply, rotate, start));
121 
122         long nextMultiply = multiply + 1;
123         try {
124             Cmres.Factory.checkUnique(list, nextMultiply);
125         } catch (IllegalStateException ex) {
126             Assert.fail("The next multiply should be unique: " + nextMultiply);
127         }
128 
129         list.add(new Cmres(nextMultiply, rotate, start));
130         // This should throw as the list now contains the multiply value
131         Cmres.Factory.checkUnique(list, nextMultiply);
132     }
133 }