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.math4.legacy.random;
18  
19  import org.junit.Assert;
20  
21  import java.io.InputStream;
22  
23  import org.apache.commons.math4.legacy.exception.OutOfRangeException;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  public class SobolSequenceGeneratorTest {
28      private static final String RESOURCE_NAME = "/assets/org/apache/commons/math4/legacy/random/new-joe-kuo-6.21201";
29  
30      private double[][] referenceValues = {
31              { 0.0, 0.0, 0.0 },
32              { 0.5, 0.5, 0.5 },
33              { 0.75, 0.25, 0.25 },
34              { 0.25, 0.75, 0.75 },
35              { 0.375, 0.375, 0.625 },
36              { 0.875, 0.875, 0.125 },
37              { 0.625, 0.125, 0.875 },
38              { 0.125, 0.625, 0.375 },
39              { 0.1875, 0.3125, 0.9375 },
40              { 0.6875, 0.8125, 0.4375 }
41      };
42  
43      private SobolSequenceGenerator generator;
44  
45      @Before
46      public void setUp() {
47          generator = new SobolSequenceGenerator(3);
48      }
49  
50      @Test
51      public void test3DReference() {
52          for (int i = 0; i < referenceValues.length; i++) {
53              double[] result = generator.get();
54              Assert.assertArrayEquals(referenceValues[i], result, 1e-6);
55              Assert.assertEquals(i + 1, generator.getNextIndex());
56          }
57      }
58  
59      @Test
60      public void testConstructor() {
61          try {
62              new SobolSequenceGenerator(0);
63              Assert.fail("an exception should have been thrown");
64          } catch (OutOfRangeException e) {
65              // expected
66          }
67  
68          try {
69              new SobolSequenceGenerator(21202);
70              Assert.fail("an exception should have been thrown");
71          } catch (OutOfRangeException e) {
72              // expected
73          }
74      }
75  
76      @Test
77      public void testConstructor2() throws Exception{
78          try {
79              final InputStream is = getClass().getResourceAsStream(RESOURCE_NAME);
80              new SobolSequenceGenerator(21202, is);
81              Assert.fail("an exception should have been thrown");
82          } catch (OutOfRangeException e) {
83              // expected
84          }
85  
86          try {
87              new SobolSequenceGenerator(21202);
88              Assert.fail("an exception should have been thrown");
89          } catch (OutOfRangeException e) {
90              // expected
91          }
92      }
93  
94      @Test
95      public void testSkip() {
96          double[] result = generator.skipTo(5);
97          Assert.assertArrayEquals(referenceValues[5], result, 1e-6);
98          Assert.assertEquals(6, generator.getNextIndex());
99  
100         for (int i = 6; i < referenceValues.length; i++) {
101             result = generator.get();
102             Assert.assertArrayEquals(referenceValues[i], result, 1e-6);
103             Assert.assertEquals(i + 1, generator.getNextIndex());
104         }
105     }
106 }