KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > random > ValueServerTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.random;
17
18 import junit.framework.Test;
19 import junit.framework.TestSuite;
20 import java.net.URL JavaDoc;
21
22 import org.apache.commons.math.RetryTestCase;
23 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
24  
25 /**
26  * Test cases for the ValueServer class.
27  *
28  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
29  */

30
31 public final class ValueServerTest extends RetryTestCase {
32
33     private ValueServer vs = new ValueServer();
34     
35     public ValueServerTest(String JavaDoc name) {
36         super(name);
37     }
38
39     public void setUp() {
40         vs.setMode(ValueServer.DIGEST_MODE);
41         try {
42             URL JavaDoc url = getClass().getResource("testData.txt");
43             vs.setValuesFileURL(url);
44         } catch (Exception JavaDoc ex) {
45             fail("malformed test URL");
46         }
47     }
48
49     public static Test suite() {
50         TestSuite suite = new TestSuite(ValueServerTest.class);
51         suite.setName("ValueServer Tests");
52         return suite;
53     }
54
55    
56     /**
57       * Generate 1000 random values and make sure they look OK.<br>
58       * Note that there is a non-zero (but very small) probability that
59       * these tests will fail even if the code is working as designed.
60       */

61     public void testNextDigest() throws Exception JavaDoc{
62         double next = 0.0;
63         double tolerance = 0.1;
64         vs.computeDistribution();
65         assertTrue("empirical distribution property",
66             vs.getEmpiricalDistribution() != null);
67         SummaryStatistics stats = SummaryStatistics.newInstance();
68         for (int i = 1; i < 1000; i++) {
69             next = vs.getNext();
70             stats.addValue(next);
71         }
72         assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
73         assertEquals
74          ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
75             tolerance);
76         
77         vs.computeDistribution(500);
78         stats = SummaryStatistics.newInstance();
79         for (int i = 1; i < 1000; i++) {
80             next = vs.getNext();
81             stats.addValue(next);
82         }
83         assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
84         assertEquals
85          ("std dev", 1.0173699343977738, stats.getStandardDeviation(),
86             tolerance);
87         
88     }
89     
90     /**
91       * Make sure exception thrown if digest getNext is attempted
92       * before loading empiricalDistribution.
93       */

94     public void testNextDigestFail() throws Exception JavaDoc {
95         try {
96             vs.getNext();
97             fail("Expecting IllegalStateException");
98         } catch (IllegalStateException JavaDoc ex) {;}
99     }
100     
101     /**
102      * Test ValueServer REPLAY_MODE using values in testData file.<br>
103      * Check that the values 1,2,1001,1002 match data file values 1 and 2.
104      * the sample data file.
105      */

106     public void testReplay() throws Exception JavaDoc {
107         double firstDataValue = 4.038625496201205;
108         double secondDataValue = 3.6485326248346936;
109         double tolerance = 10E-15;
110         double compareValue = 0.0d;
111         vs.setMode(ValueServer.REPLAY_MODE);
112         vs.resetReplayFile();
113         compareValue = vs.getNext();
114         assertEquals(compareValue,firstDataValue,tolerance);
115         compareValue = vs.getNext();
116         assertEquals(compareValue,secondDataValue,tolerance);
117         for (int i = 3; i < 1001; i++) {
118            compareValue = vs.getNext();
119         }
120         compareValue = vs.getNext();
121         assertEquals(compareValue,firstDataValue,tolerance);
122         compareValue = vs.getNext();
123         assertEquals(compareValue,secondDataValue,tolerance);
124         vs.closeReplayFile();
125         // make sure no NPE
126
vs.closeReplayFile();
127     }
128     
129     /**
130      * Test other ValueServer modes
131      */

132     public void testModes() throws Exception JavaDoc {
133         vs.setMode(ValueServer.CONSTANT_MODE);
134         vs.setMu(0);
135         assertEquals("constant mode test",vs.getMu(),vs.getNext(),Double.MIN_VALUE);
136         vs.setMode(ValueServer.UNIFORM_MODE);
137         vs.setMu(2);
138         double val = vs.getNext();
139         assertTrue(val > 0 && val < 4);
140         vs.setSigma(1);
141         vs.setMode(ValueServer.GAUSSIAN_MODE);
142         val = vs.getNext();
143         assertTrue("gaussian value close enough to mean",
144             val < vs.getMu() + 100*vs.getSigma());
145         vs.setMode(ValueServer.EXPONENTIAL_MODE);
146         val = vs.getNext();
147         assertTrue(val > 0);
148         try {
149             vs.setMode(1000);
150             vs.getNext();
151             fail("bad mode, expecting IllegalStateException");
152         } catch (IllegalStateException JavaDoc ex) {
153             ;
154         }
155     }
156     
157     /**
158      * Test fill
159      */

160     public void testFill() throws Exception JavaDoc {
161         vs.setMode(ValueServer.CONSTANT_MODE);
162         vs.setMu(2);
163         double[] val = new double[5];
164         vs.fill(val);
165         for (int i = 0; i < 5; i++) {
166             assertEquals("fill test in place",2,val[i],Double.MIN_VALUE);
167         }
168         double v2[] = vs.fill(3);
169         for (int i = 0; i < 3; i++) {
170             assertEquals("fill test in place",2,v2[i],Double.MIN_VALUE);
171         }
172     }
173     
174     /**
175      * Test getters to make Clover happy
176      */

177     public void testProperties() throws Exception JavaDoc {
178         vs.setMode(ValueServer.CONSTANT_MODE);
179         assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode());
180         vs.setValuesFileURL("http://www.apache.org");
181         URL JavaDoc url = vs.getValuesFileURL();
182         assertEquals("valuesFileURL test","http://www.apache.org",url.toString());
183     }
184                           
185 }
186
Popular Tags