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.source32;
18  
19  import org.apache.commons.rng.core.RandomAssert;
20  import org.junit.jupiter.api.Test;
21  
22  class XoRoShiRo64StarStarTest {
23      /** The size of the array seed. */
24      private static final int SEED_SIZE = 2;
25  
26      @Test
27      void testReferenceCode() {
28          /*
29           * Data from running the executable compiled from the author's C code:
30           *   http://xoshiro.di.unimi.it/xoroshiro64starstar.c
31           */
32          final int[] seed = {
33              0x012de1ba, 0xa5a818b8,
34          };
35  
36          final int[] expectedSequence = {
37              0x7ac00b42, 0x1f638399, 0x09e4aea4, 0x05cbbd64,
38              0x1c967b7b, 0x1cf852fd, 0xc666f4e8, 0xeea9f1ae,
39              0xca0fa6bc, 0xa65d0905, 0xa69afc95, 0x34965e62,
40              0xdd4f04a9, 0xff1c9342, 0x638ff769, 0x03419ca0,
41              0xb46e6dfd, 0xf7555b22, 0x8cab4e68, 0x5a44b6ee,
42              0x4e5e1eed, 0xd03c5963, 0x782d05ed, 0x41bda3e3,
43              0xd1d65005, 0x88f43a8a, 0xfffe02ea, 0xb326624a,
44              0x1ec0034c, 0xb903d8df, 0x78454bd7, 0xaec630f8,
45              0x2a0c9a3a, 0xc2594988, 0xe71e767e, 0x4e0e1ddc,
46              0xae945004, 0xf178c293, 0xa04081d6, 0xdd9c062f,
47          };
48  
49          RandomAssert.assertEquals(expectedSequence, new XoRoShiRo64StarStar(seed));
50      }
51  
52      @Test
53      void testConstructorWithZeroSeedIsNonFunctional() {
54          RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64StarStar(new int[SEED_SIZE]), 2 * SEED_SIZE);
55      }
56  
57      @Test
58      void testConstructorWithSingleBitSeedIsFunctional() {
59          RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64StarStar.class, SEED_SIZE);
60      }
61  
62      @Test
63      void testConstructorWithoutFullLengthSeed() {
64          // Hit the case when the input seed is self-seeded when not full length
65          RandomAssert.assertNextLongNonZeroOutput(new XoRoShiRo64StarStar(new int[] {0x012de1ba}),
66                  SEED_SIZE, SEED_SIZE);
67      }
68  
69      @Test
70      void testElementConstructor() {
71          final int[] seed = {
72              0x012de1ba, 0xa5a818b8,
73          };
74          final XoRoShiRo64StarStar rng1 = new XoRoShiRo64StarStar(seed);
75          final XoRoShiRo64StarStar rng2 = new XoRoShiRo64StarStar(seed[0], seed[1]);
76          RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2);
77      }
78  }