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