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.examples.stress;
18  
19  import org.apache.commons.rng.UniformRandomProvider;
20  import org.apache.commons.rng.core.source64.SplitMix64;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link RNGUtils}.
26   */
27  class RNGUtilsTest {
28      @Test
29      void testCreateIntProviderLongThrows() {
30          final SplitMix64 rng = new SplitMix64(42);
31          Assertions.assertThrows(IllegalArgumentException.class,
32              () -> RNGUtils.createIntProvider(rng, Source64Mode.LONG));
33      }
34  
35      @Test
36      void testCreateIntProviderInt() {
37          final long seed = 236784264237894L;
38          final UniformRandomProvider rng1 = new SplitMix64(seed);
39          final UniformRandomProvider rng2 =
40              RNGUtils.createIntProvider(new SplitMix64(seed), Source64Mode.INT);
41          for (int i = 0; i < 50; i++) {
42              Assertions.assertEquals(rng1.nextInt(), rng2.nextInt());
43          }
44      }
45  
46      @Test
47      void testCreateIntProviderLoHi() {
48          final long seed = 236784264237894L;
49          final UniformRandomProvider rng1 = new SplitMix64(seed);
50          final UniformRandomProvider rng2 =
51              RNGUtils.createIntProvider(new SplitMix64(seed), Source64Mode.LO_HI);
52          for (int i = 0; i < 50; i++) {
53              final long l = rng1.nextLong();
54              final int hi = (int) (l >>> 32);
55              final int lo = (int) l;
56              Assertions.assertEquals(lo, rng2.nextInt());
57              Assertions.assertEquals(hi, rng2.nextInt());
58          }
59      }
60  
61      @Test
62      void testCreateIntProviderHiLo() {
63          final long seed = 2367234237894L;
64          final UniformRandomProvider rng1 = new SplitMix64(seed);
65          final UniformRandomProvider rng2 =
66              RNGUtils.createIntProvider(new SplitMix64(seed), Source64Mode.HI_LO);
67          for (int i = 0; i < 50; i++) {
68              final long l = rng1.nextLong();
69              final int hi = (int) (l >>> 32);
70              final int lo = (int) l;
71              Assertions.assertEquals(hi, rng2.nextInt());
72              Assertions.assertEquals(lo, rng2.nextInt());
73          }
74      }
75  
76      @Test
77      void testCreateIntProviderHi() {
78          final long seed = 2367234237894L;
79          final UniformRandomProvider rng1 = new SplitMix64(seed);
80          final UniformRandomProvider rng2 =
81              RNGUtils.createIntProvider(new SplitMix64(seed), Source64Mode.HI);
82          for (int i = 0; i < 50; i++) {
83              final int hi = (int) (rng1.nextLong() >>> 32);
84              Assertions.assertEquals(hi, rng2.nextInt());
85          }
86      }
87  
88      @Test
89      void testCreateIntProviderLo() {
90          final long seed = 2367234237894L;
91          final UniformRandomProvider rng1 = new SplitMix64(seed);
92          final UniformRandomProvider rng2 =
93              RNGUtils.createIntProvider(new SplitMix64(seed), Source64Mode.LO);
94          for (int i = 0; i < 50; i++) {
95              final int lo = (int) rng1.nextLong();
96              Assertions.assertEquals(lo, rng2.nextInt());
97          }
98      }
99  
100     /**
101      * Test that the default source64 mode matches the nextInt implementation for a LongProvider.
102      * If this changes then the default mode should be updated. The value is used as
103      * the default for the stress test application.
104      */
105     @Test
106     void testCreateIntProviderDefault() {
107         final long seed = 236784264237894L;
108         final UniformRandomProvider rng1 = new SplitMix64(seed);
109         final UniformRandomProvider rng2 =
110             RNGUtils.createIntProvider(new SplitMix64(seed), RNGUtils.getSource64Default());
111         for (int i = 0; i < 50; i++) {
112             final int a = rng1.nextInt();
113             final int b = rng1.nextInt();
114             Assertions.assertEquals(a, rng2.nextInt());
115             Assertions.assertEquals(b, rng2.nextInt());
116         }
117     }
118 }