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