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 XoRoShiRo64StarTest {
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/xoroshiro64star.c
31           */
32          final int[] seed = {
33              0x012de1ba, 0xa5a818b8,
34          };
35  
36          final int[] expectedSequence = {
37              0xd72accde, 0x29cbd26c, 0xa00fd44a, 0xa4d612c8,
38              0xf9c7572b, 0xce94c084, 0x47a3d7ee, 0xb64aa982,
39              0x67a9b2a4, 0x0c3d61a8, 0x8f70f7fa, 0xd1edbd63,
40              0xac954b3a, 0xd7fe941e, 0xaa38e658, 0x019ecf61,
41              0xcded7d7c, 0xd6588891, 0x4414454a, 0xb3c3a124,
42              0x4a16fcfe, 0x3fb393c2, 0x4d8d14d6, 0x3a02c906,
43              0x0c82f080, 0x174186c4, 0x1199966b, 0x12b83d6a,
44              0xe697999e, 0x9df4d2f4, 0x5a5a0879, 0xc44ad6b4,
45              0x96a9adc3, 0x4603c20f, 0x3171ca57, 0x66e349c9,
46              0xa77dba19, 0xbe4f279d, 0xf5cd3402, 0x1962933d,
47          };
48  
49          RandomAssert.assertEquals(expectedSequence, new XoRoShiRo64Star(seed));
50      }
51  
52      @Test
53      void testConstructorWithZeroSeedIsNonFunctional() {
54          RandomAssert.assertNextIntZeroOutput(new XoRoShiRo64Star(new int[SEED_SIZE]), 2 * SEED_SIZE);
55      }
56  
57      @Test
58      void testConstructorWithSingleBitSeedIsFunctional() {
59          RandomAssert.assertIntArrayConstructorWithSingleBitSeedIsFunctional(XoRoShiRo64Star.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 XoRoShiRo64Star(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 XoRoShiRo64Star rng1 = new XoRoShiRo64Star(seed);
75          final XoRoShiRo64Star rng2 = new XoRoShiRo64Star(seed[0], seed[1]);
76          RandomAssert.assertNextIntEquals(seed.length * 2, rng1, rng2);
77      }
78  }