2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.random;
19
import junit.framework.Test;
20
import junit.framework.TestSuite;
22
import java.io.EOFException;
25
import org.apache.commons.math.RetryTestCase;
26
import org.apache.commons.math.stat.descriptive.SummaryStatistics;
29
* Test cases for the ValueServer class.
31
* @version $Revision: 617850 $ $Date: 2008-02-02 11:01:29 -0700 (Sat, 02 Feb 2008) $
34
public final class ValueServerTest extends RetryTestCase {
36
private ValueServer vs = new ValueServer();
38
public ValueServerTest(String name) {
43
vs.setMode(ValueServer.DIGEST_MODE);
45
URL url = getClass().getResource("testData.txt");
46
vs.setValuesFileURL(url);
47
} catch (Exception ex) {
48
fail("malformed test URL");
52
public static Test suite() {
53
TestSuite suite = new TestSuite(ValueServerTest.class);
54
suite.setName("ValueServer Tests");
60
* Generate 1000 random values and make sure they look OK.<br>
61
* Note that there is a non-zero (but very small) probability that
62
* these tests will fail even if the code is working as designed.
64
public void testNextDigest() throws Exception{
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++) {
75
assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
77
("std dev", 1.0173699343977738, stats.getStandardDeviation(),
80
vs.computeDistribution(500);
81
stats = new SummaryStatistics();
82
for (int i = 1; i < 1000; i++) {
86
assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
88
("std dev", 1.0173699343977738, stats.getStandardDeviation(),
94
* Make sure exception thrown if digest getNext is attempted
95
* before loading empiricalDistribution.
97
public void testNextDigestFail() throws Exception {
100
fail("Expecting IllegalStateException");
101
} catch (IllegalStateException ex) {;}
104
public void testEmptyReplayFile() {
106
URL url = getClass().getResource("emptyFile.txt");
107
vs.setMode(ValueServer.REPLAY_MODE);
108
vs.setValuesFileURL(url);
110
fail("an exception should have been thrown");
111
} catch (EOFException eof) {
113
} catch (Exception e) {
114
fail("wrong exception caught");
118
public void testEmptyDigestFile() {
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) {
127
} catch (Exception e) {
128
fail("wrong exception caught");
133
* Test ValueServer REPLAY_MODE using values in testData file.<br>
134
* Check that the values 1,2,1001,1002 match data file values 1 and 2.
135
* the sample data file.
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();
151
compareValue = vs.getNext();
152
assertEquals(compareValue,firstDataValue,tolerance);
153
compareValue = vs.getNext();
154
assertEquals(compareValue,secondDataValue,tolerance);
155
vs.closeReplayFile();
157
vs.closeReplayFile();
161
* Test other ValueServer modes
163
public void testModes() throws Exception {
164
vs.setMode(ValueServer.CONSTANT_MODE);
166
assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE);
167
vs.setMode(ValueServer.UNIFORM_MODE);
169
double val = vs.getNext();
170
assertTrue(val > 0 && val < 4);
172
vs.setMode(ValueServer.GAUSSIAN_MODE);
174
assertTrue("gaussian value close enough to mean",
175
val < vs.getMu() + 100*vs.getSigma());
176
vs.setMode(ValueServer.EXPONENTIAL_MODE);
182
fail("bad mode, expecting IllegalStateException");
183
} catch (IllegalStateException ex) {
191
public void testFill() throws Exception {
192
vs.setMode(ValueServer.CONSTANT_MODE);
194
double[] val = new double[5];
196
for (int i = 0; i < 5; i++) {
197
assertEquals("fill test in place",2,val[i],Double.MIN_VALUE);
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);
206
* Test getters to make Clover happy
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());