001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.random;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020: import java.net.URL;
021:
022: import org.apache.commons.math.RetryTestCase;
023: import org.apache.commons.math.stat.descriptive.SummaryStatistics;
024:
025: /**
026: * Test cases for the ValueServer class.
027: *
028: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
029: */
030:
031: public final class ValueServerTest extends RetryTestCase {
032:
033: private ValueServer vs = new ValueServer();
034:
035: public ValueServerTest(String name) {
036: super (name);
037: }
038:
039: public void setUp() {
040: vs.setMode(ValueServer.DIGEST_MODE);
041: try {
042: URL url = getClass().getResource("testData.txt");
043: vs.setValuesFileURL(url);
044: } catch (Exception ex) {
045: fail("malformed test URL");
046: }
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(ValueServerTest.class);
051: suite.setName("ValueServer Tests");
052: return suite;
053: }
054:
055: /**
056: * Generate 1000 random values and make sure they look OK.<br>
057: * Note that there is a non-zero (but very small) probability that
058: * these tests will fail even if the code is working as designed.
059: */
060: public void testNextDigest() throws Exception {
061: double next = 0.0;
062: double tolerance = 0.1;
063: vs.computeDistribution();
064: assertTrue("empirical distribution property", vs
065: .getEmpiricalDistribution() != null);
066: SummaryStatistics stats = SummaryStatistics.newInstance();
067: for (int i = 1; i < 1000; i++) {
068: next = vs.getNext();
069: stats.addValue(next);
070: }
071: assertEquals("mean", 5.069831575018909, stats.getMean(),
072: tolerance);
073: assertEquals("std dev", 1.0173699343977738, stats
074: .getStandardDeviation(), tolerance);
075:
076: vs.computeDistribution(500);
077: stats = SummaryStatistics.newInstance();
078: for (int i = 1; i < 1000; i++) {
079: next = vs.getNext();
080: stats.addValue(next);
081: }
082: assertEquals("mean", 5.069831575018909, stats.getMean(),
083: tolerance);
084: assertEquals("std dev", 1.0173699343977738, stats
085: .getStandardDeviation(), tolerance);
086:
087: }
088:
089: /**
090: * Make sure exception thrown if digest getNext is attempted
091: * before loading empiricalDistribution.
092: */
093: public void testNextDigestFail() throws Exception {
094: try {
095: vs.getNext();
096: fail("Expecting IllegalStateException");
097: } catch (IllegalStateException ex) {
098: ;
099: }
100: }
101:
102: /**
103: * Test ValueServer REPLAY_MODE using values in testData file.<br>
104: * Check that the values 1,2,1001,1002 match data file values 1 and 2.
105: * the sample data file.
106: */
107: public void testReplay() throws Exception {
108: double firstDataValue = 4.038625496201205;
109: double secondDataValue = 3.6485326248346936;
110: double tolerance = 10E-15;
111: double compareValue = 0.0d;
112: vs.setMode(ValueServer.REPLAY_MODE);
113: vs.resetReplayFile();
114: compareValue = vs.getNext();
115: assertEquals(compareValue, firstDataValue, tolerance);
116: compareValue = vs.getNext();
117: assertEquals(compareValue, secondDataValue, tolerance);
118: for (int i = 3; i < 1001; i++) {
119: compareValue = vs.getNext();
120: }
121: compareValue = vs.getNext();
122: assertEquals(compareValue, firstDataValue, tolerance);
123: compareValue = vs.getNext();
124: assertEquals(compareValue, secondDataValue, tolerance);
125: vs.closeReplayFile();
126: // make sure no NPE
127: vs.closeReplayFile();
128: }
129:
130: /**
131: * Test other ValueServer modes
132: */
133: public void testModes() throws Exception {
134: vs.setMode(ValueServer.CONSTANT_MODE);
135: vs.setMu(0);
136: assertEquals("constant mode test", vs.getMu(), vs.getNext(),
137: Double.MIN_VALUE);
138: vs.setMode(ValueServer.UNIFORM_MODE);
139: vs.setMu(2);
140: double val = vs.getNext();
141: assertTrue(val > 0 && val < 4);
142: vs.setSigma(1);
143: vs.setMode(ValueServer.GAUSSIAN_MODE);
144: val = vs.getNext();
145: assertTrue("gaussian value close enough to mean", val < vs
146: .getMu()
147: + 100 * vs.getSigma());
148: vs.setMode(ValueServer.EXPONENTIAL_MODE);
149: val = vs.getNext();
150: assertTrue(val > 0);
151: try {
152: vs.setMode(1000);
153: vs.getNext();
154: fail("bad mode, expecting IllegalStateException");
155: } catch (IllegalStateException ex) {
156: ;
157: }
158: }
159:
160: /**
161: * Test fill
162: */
163: public void testFill() throws Exception {
164: vs.setMode(ValueServer.CONSTANT_MODE);
165: vs.setMu(2);
166: double[] val = new double[5];
167: vs.fill(val);
168: for (int i = 0; i < 5; i++) {
169: assertEquals("fill test in place", 2, val[i],
170: Double.MIN_VALUE);
171: }
172: double v2[] = vs.fill(3);
173: for (int i = 0; i < 3; i++) {
174: assertEquals("fill test in place", 2, v2[i],
175: Double.MIN_VALUE);
176: }
177: }
178:
179: /**
180: * Test getters to make Clover happy
181: */
182: public void testProperties() throws Exception {
183: vs.setMode(ValueServer.CONSTANT_MODE);
184: assertEquals("mode test", ValueServer.CONSTANT_MODE, vs
185: .getMode());
186: vs.setValuesFileURL("http://www.apache.org");
187: URL url = vs.getValuesFileURL();
188: assertEquals("valuesFileURL test", "http://www.apache.org", url
189: .toString());
190: }
191:
192: }
|