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