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