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