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.hipparchus.exception.NullArgumentException;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.fail;
32  
33  class HaltonSequenceGeneratorTest {
34  
35      private double[][] referenceValues = {
36              { 0.0,    0.0,    0.0  },
37              { 0.5,    0.6667, 0.6  },
38              { 0.25,   0.3333, 0.2  },
39              { 0.75,   0.2223, 0.8  },
40              { 0.125,  0.8888, 0.4  },
41              { 0.625,  0.5555, 0.12 },
42              { 0.375,  0.1111, 0.72 },
43              { 0.875,  0.7777, 0.32 },
44              { 0.0625, 0.4444, 0.92 },
45              { 0.5625, 0.0740, 0.52 }
46      };
47  
48      private double[][] referenceValuesUnscrambled = {
49              { 0.0,    0.0    },
50              { 0.5,    0.3333 },
51              { 0.25,   0.6666 },
52              { 0.75,   0.1111 },
53              { 0.125,  0.4444 },
54              { 0.625,  0.7777 },
55              { 0.375,  0.2222 },
56              { 0.875,  0.5555 },
57              { 0.0625, 0.8888 },
58              { 0.5625, 0.0370 }
59      };
60  
61      private HaltonSequenceGenerator generator;
62  
63      @BeforeEach
64      void setUp() {
65          generator = new HaltonSequenceGenerator(3);
66      }
67  
68      @Test
69      void test3DReference() {
70          for (int i = 0; i < referenceValues.length; i++) {
71              double[] result = generator.nextVector();
72              assertArrayEquals(referenceValues[i], result, 1e-3);
73              assertEquals(i + 1, generator.getNextIndex());
74          }
75      }
76  
77      @Test
78      void test2DUnscrambledReference() {
79          generator = new HaltonSequenceGenerator(2, new int[] {2, 3}, null);
80          for (int i = 0; i < referenceValuesUnscrambled.length; i++) {
81              double[] result = generator.nextVector();
82              assertArrayEquals(referenceValuesUnscrambled[i], result, 1e-3);
83              assertEquals(i + 1, generator.getNextIndex());
84          }
85      }
86  
87      @Test
88      void testConstructor() {
89          try {
90              new HaltonSequenceGenerator(0);
91              fail("an exception should have been thrown");
92          } catch (MathIllegalArgumentException e) {
93              // expected
94          }
95  
96          try {
97              new HaltonSequenceGenerator(41);
98              fail("an exception should have been thrown");
99          } catch (MathIllegalArgumentException e) {
100             // expected
101         }
102     }
103 
104     @Test
105     void testConstructor2() throws Exception{
106         try {
107             new HaltonSequenceGenerator(2, new int[] { 1 }, null);
108             fail("an exception should have been thrown");
109         } catch (MathIllegalArgumentException e) {
110             // expected
111         }
112 
113         try {
114             new HaltonSequenceGenerator(2, null, null);
115             fail("an exception should have been thrown");
116         } catch (NullArgumentException e) {
117             // expected
118         }
119 
120         try {
121             new HaltonSequenceGenerator(2, new int[] { 1, 1 }, new int[] { 1 });
122             fail("an exception should have been thrown");
123         } catch (MathIllegalArgumentException e) {
124             // expected
125         }
126     }
127 
128     @Test
129     void testSkip() {
130         double[] result = generator.skipTo(5);
131         assertArrayEquals(referenceValues[5], result, 1e-3);
132         assertEquals(6, generator.getNextIndex());
133 
134         for (int i = 6; i < referenceValues.length; i++) {
135             result = generator.nextVector();
136             assertArrayEquals(referenceValues[i], result, 1e-3);
137             assertEquals(i + 1, generator.getNextIndex());
138         }
139     }
140 
141 }