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.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  
24  import java.util.ArrayList;
25  
26  
27  class TwoCmresTest {
28      @Test
29      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[0] < n; i[0]++) {
40              Assertions.assertNotEquals(rng1.nextLong(), rng2.nextLong(), () -> "i=" + i[0]);
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      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      void testSubcycleGeneratorsMustBeDifferent() {
69          final int max = TwoCmres.numberOfSubcycleGenerators();
70          for (int i = 0; i < max; i++) {
71              final int subCycle = i;
72              Assertions.assertThrows(IllegalArgumentException.class,
73                  () -> new TwoCmres(-97845, subCycle, subCycle));
74          }
75      }
76  
77      @Test
78      void testSubcycleGeneratorsIndex() {
79          final int seed = 246810;
80  
81          // Valid indices are between 0 (included) and max (excluded).
82          final int max = TwoCmres.numberOfSubcycleGenerators();
83  
84          for (int i = 0; i < max; i++) {
85              for (int j = 0; j < max; j++) {
86                  if (i != j) { // Subcycle generators must be different.
87                      // Can be instantiated.
88                      new TwoCmres(seed, i, j);
89                  }
90              }
91          }
92  
93          for (int wrongIndex : new int[] {-1, max}) {
94              Assertions.assertThrows(IndexOutOfBoundsException.class,
95                  () -> new TwoCmres(seed, wrongIndex, 1),
96                  () -> "Exception expected for index i = " + wrongIndex);
97              Assertions.assertThrows(IndexOutOfBoundsException.class,
98                  () -> new TwoCmres(seed, 1, wrongIndex),
99                  () -> "Exception expected for index j = " + wrongIndex);
100         }
101     }
102 
103     @Test
104     void testCmresFactoryThrowsWithDuplicateMultiplier() {
105         final ArrayList<Cmres> list = new ArrayList<>();
106         final long multiply = 0;
107         final int rotate = 3;
108         final int start = 5;
109 
110         list.add(new Cmres(multiply, rotate, start));
111 
112         long nextMultiply = multiply + 1;
113         Assertions.assertDoesNotThrow(
114             () -> Cmres.Factory.checkUnique(list, nextMultiply),
115             () -> "The next multiply should be unique: " + nextMultiply);
116 
117         list.add(new Cmres(nextMultiply, rotate, start));
118         // This should throw as the list now contains the multiply value
119         Assertions.assertThrows(IllegalStateException.class, () -> Cmres.Factory.checkUnique(list, nextMultiply));
120     }
121 }