1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.random;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 import java.io.EOFException;
23 import java.net.URL;
24
25 import org.apache.commons.math.RetryTestCase;
26 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
27
28
29
30
31
32
33
34 public final class ValueServerTest extends RetryTestCase {
35
36 private ValueServer vs = new ValueServer();
37
38 public ValueServerTest(String name) {
39 super(name);
40 }
41
42 @Override
43 public void setUp() {
44 vs.setMode(ValueServer.DIGEST_MODE);
45 try {
46 URL url = getClass().getResource("testData.txt");
47 vs.setValuesFileURL(url);
48 } catch (Exception ex) {
49 fail("malformed test URL");
50 }
51 }
52
53 public static Test suite() {
54 TestSuite suite = new TestSuite(ValueServerTest.class);
55 suite.setName("ValueServer Tests");
56 return suite;
57 }
58
59
60
61
62
63
64
65 public void testNextDigest() throws Exception{
66 double next = 0.0;
67 double tolerance = 0.1;
68 vs.computeDistribution();
69 assertTrue("empirical distribution property",
70 vs.getEmpiricalDistribution() != null);
71 SummaryStatistics stats = new SummaryStatistics();
72 for (int i = 1; i < 1000; i++) {
73 next = vs.getNext();
74 stats.addValue(next);
75 }
76 assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
77 assertEquals
78 ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
79 tolerance);
80
81 vs.computeDistribution(500);
82 stats = new SummaryStatistics();
83 for (int i = 1; i < 1000; i++) {
84 next = vs.getNext();
85 stats.addValue(next);
86 }
87 assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
88 assertEquals
89 ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
90 tolerance);
91
92 }
93
94
95
96
97
98 public void testNextDigestFail() throws Exception {
99 try {
100 vs.getNext();
101 fail("Expecting IllegalStateException");
102 } catch (IllegalStateException ex) {}
103 }
104
105 public void testEmptyReplayFile() {
106 try {
107 URL url = getClass().getResource("emptyFile.txt");
108 vs.setMode(ValueServer.REPLAY_MODE);
109 vs.setValuesFileURL(url);
110 vs.getNext();
111 fail("an exception should have been thrown");
112 } catch (EOFException eof) {
113
114 } catch (Exception e) {
115 fail("wrong exception caught");
116 }
117 }
118
119 public void testEmptyDigestFile() {
120 try {
121 URL url = getClass().getResource("emptyFile.txt");
122 vs.setMode(ValueServer.DIGEST_MODE);
123 vs.setValuesFileURL(url);
124 vs.computeDistribution();
125 fail("an exception should have been thrown");
126 } catch (EOFException eof) {
127
128 } catch (Exception e) {
129 fail("wrong exception caught");
130 }
131 }
132
133
134
135
136
137
138 public void testReplay() throws Exception {
139 double firstDataValue = 4.038625496201205;
140 double secondDataValue = 3.6485326248346936;
141 double tolerance = 10E-15;
142 double compareValue = 0.0d;
143 vs.setMode(ValueServer.REPLAY_MODE);
144 vs.resetReplayFile();
145 compareValue = vs.getNext();
146 assertEquals(compareValue,firstDataValue,tolerance);
147 compareValue = vs.getNext();
148 assertEquals(compareValue,secondDataValue,tolerance);
149 for (int i = 3; i < 1001; i++) {
150 compareValue = vs.getNext();
151 }
152 compareValue = vs.getNext();
153 assertEquals(compareValue,firstDataValue,tolerance);
154 compareValue = vs.getNext();
155 assertEquals(compareValue,secondDataValue,tolerance);
156 vs.closeReplayFile();
157
158 vs.closeReplayFile();
159 }
160
161
162
163
164 public void testModes() throws Exception {
165 vs.setMode(ValueServer.CONSTANT_MODE);
166 vs.setMu(0);
167 assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE);
168 vs.setMode(ValueServer.UNIFORM_MODE);
169 vs.setMu(2);
170 double val = vs.getNext();
171 assertTrue(val > 0 && val < 4);
172 vs.setSigma(1);
173 vs.setMode(ValueServer.GAUSSIAN_MODE);
174 val = vs.getNext();
175 assertTrue("gaussian value close enough to mean",
176 val < vs.getMu() + 100*vs.getSigma());
177 vs.setMode(ValueServer.EXPONENTIAL_MODE);
178 val = vs.getNext();
179 assertTrue(val > 0);
180 try {
181 vs.setMode(1000);
182 vs.getNext();
183 fail("bad mode, expecting IllegalStateException");
184 } catch (IllegalStateException ex) {
185
186 }
187 }
188
189
190
191
192 public void testFill() throws Exception {
193 vs.setMode(ValueServer.CONSTANT_MODE);
194 vs.setMu(2);
195 double[] val = new double[5];
196 vs.fill(val);
197 for (int i = 0; i < 5; i++) {
198 assertEquals("fill test in place",2,val[i],Double.MIN_VALUE);
199 }
200 double v2[] = vs.fill(3);
201 for (int i = 0; i < 3; i++) {
202 assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE);
203 }
204 }
205
206
207
208
209 public void testProperties() throws Exception {
210 vs.setMode(ValueServer.CONSTANT_MODE);
211 assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode());
212 vs.setValuesFileURL("http://www.apache.org");
213 URL url = vs.getValuesFileURL();
214 assertEquals("valuesFileURL test","http://www.apache.org",url.toString());
215 }
216
217 }