1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
91 }
92
93 try {
94 new HaltonSequenceGenerator(41);
95 Assert.fail("an exception should have been thrown");
96 } catch (MathIllegalArgumentException e) {
97
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
108 }
109
110 try {
111 new HaltonSequenceGenerator(2, null, null);
112 Assert.fail("an exception should have been thrown");
113 } catch (NullArgumentException e) {
114
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
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 }