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: 617850 $ $Date: 2008-02-02 11:01:29 -0700 (Sat, 02 Feb 2008) $
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        * 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.
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        * Make sure exception thrown if digest getNext is attempted
95        * before loading empiricalDistribution.
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             // expected behavior
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             // expected behavior
127         } catch (Exception e) {
128             fail("wrong exception caught");
129         }
130     }
131 
132     /**
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.
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         // make sure no NPE
157         vs.closeReplayFile();
158     }
159     
160     /** 
161      * Test other ValueServer modes
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      * Test fill
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      * Test getters to make Clover happy
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 }