1   /*
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
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
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.
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   * Test cases for the ValueServer class.
30   *
31   * @version $Revision: 764749 $ $Date: 2009-04-14 07:51:40 -0400 (Tue, 14 Apr 2009) $
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        * Generate 1000 random values and make sure they look OK.<br>
62        * Note that there is a non-zero (but very small) probability that
63        * these tests will fail even if the code is working as designed.
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        * Make sure exception thrown if digest getNext is attempted
96        * before loading empiricalDistribution.
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             // expected behavior
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             // expected behavior
128         } catch (Exception e) {
129             fail("wrong exception caught");
130         }
131     }
132 
133     /**
134      * Test ValueServer REPLAY_MODE using values in testData file.<br> 
135      * Check that the values 1,2,1001,1002 match data file values 1 and 2.
136      * the sample data file.
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         // make sure no NPE
158         vs.closeReplayFile();
159     }
160     
161     /** 
162      * Test other ValueServer modes
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             // ignored
186         }
187     }
188     
189     /**
190      * Test fill
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      * Test getters to make Clover happy
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 }