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  
18  package org.apache.commons.rng.simple.internal;
19  
20  import org.apache.commons.math3.stat.inference.ChiSquareTest;
21  import org.apache.commons.rng.UniformRandomProvider;
22  import org.apache.commons.rng.core.source64.SplitMix64;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  import java.util.Arrays;
27  
28  /**
29   * Tests for the {@link SeedUtils}.
30   */
31  public class SeedUtilsTest {
32      /**
33       * Test the int hex permutation has 8 unique hex digits per permutation.
34       * A uniformity test is performed on to check each hex digits is used evenly at each
35       * character position.
36       */
37      @Test
38      public void testCreateIntHexPermutation() {
39          final UniformRandomProvider rng = new SplitMix64(-567435247L);
40          final long[][] samples = new long[8][16];
41          for (int i = 0; i < 1000; i++) {
42              int sample = SeedUtils.createIntHexPermutation(rng);
43              int observed = 0;
44              for (int j = 0; j < 8; j++) {
45                  final int digit = sample & 0xf;
46                  Assert.assertEquals("Duplicate digit in sample", 0, observed & (1 << digit));
47                  observed |= 1 << digit;
48                  samples[j][digit]++;
49                  sample >>>= 4;
50              }
51          }
52  
53          final ChiSquareTest chiSquareTest = new ChiSquareTest();
54          final double[] expected = new double[16];
55          Arrays.fill(expected, 1.0 / 16);
56          // Pass if we cannot reject null hypothesis that distributions are the same.
57          for (int j = 0; j < 8; j++) {
58              Assert.assertFalse("Not uniform in digit " + j,
59                      chiSquareTest.chiSquareTest(expected, samples[j], 0.001));
60          }
61      }
62  
63      /**
64       * Test the long hex permutation has 8 unique hex digits per permutation in the upper and
65       * lower 32-bits.
66       * A uniformity test is performed on to check each hex digits is used evenly at each
67       * character position.
68       */
69      @Test
70      public void testCreateLongHexPermutation() {
71          final UniformRandomProvider rng = new SplitMix64(34645768L);
72          final long[][] samples = new long[16][16];
73          for (int i = 0; i < 1000; i++) {
74              long sample = SeedUtils.createLongHexPermutation(rng);
75              // Check lower 32-bits
76              long observed = 0;
77              for (int j = 0; j < 8; j++) {
78                  final int digit = (int) (sample & 0xfL);
79                  Assert.assertEquals("Duplicate digit in lower sample", 0, observed & (1 << digit));
80                  observed |= 1 << digit;
81                  samples[j][digit]++;
82                  sample >>>= 4;
83              }
84              // Check upper 32-bits
85              observed = 0;
86              for (int j = 8; j < 16; j++) {
87                  final int digit = (int) (sample & 0xfL);
88                  Assert.assertEquals("Duplicate digit in upper sample", 0, observed & (1 << digit));
89                  observed |= 1 << digit;
90                  samples[j][digit]++;
91                  sample >>>= 4;
92              }
93          }
94  
95          final ChiSquareTest chiSquareTest = new ChiSquareTest();
96          final double[] expected = new double[16];
97          Arrays.fill(expected, 1.0 / 16);
98          // Pass if we cannot reject null hypothesis that distributions are the same.
99          for (int j = 0; j < 16; j++) {
100             Assert.assertFalse("Not uniform in digit " + j,
101                     chiSquareTest.chiSquareTest(expected, samples[j], 0.001));
102         }
103     }
104 }