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