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