001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.random; 018 019 import junit.framework.Test; 020 import junit.framework.TestSuite; 021 022 import java.io.EOFException; 023 import java.net.URL; 024 025 import org.apache.commons.math.RetryTestCase; 026 import org.apache.commons.math.stat.descriptive.SummaryStatistics; 027 028 /** 029 * Test cases for the ValueServer class. 030 * 031 * @version $Revision: 764749 $ $Date: 2009-04-14 07:51:40 -0400 (Tue, 14 Apr 2009) $ 032 */ 033 034 public final class ValueServerTest extends RetryTestCase { 035 036 private ValueServer vs = new ValueServer(); 037 038 public ValueServerTest(String name) { 039 super(name); 040 } 041 042 @Override 043 public void setUp() { 044 vs.setMode(ValueServer.DIGEST_MODE); 045 try { 046 URL url = getClass().getResource("testData.txt"); 047 vs.setValuesFileURL(url); 048 } catch (Exception ex) { 049 fail("malformed test URL"); 050 } 051 } 052 053 public static Test suite() { 054 TestSuite suite = new TestSuite(ValueServerTest.class); 055 suite.setName("ValueServer Tests"); 056 return suite; 057 } 058 059 060 /** 061 * Generate 1000 random values and make sure they look OK.<br> 062 * Note that there is a non-zero (but very small) probability that 063 * these tests will fail even if the code is working as designed. 064 */ 065 public void testNextDigest() throws Exception{ 066 double next = 0.0; 067 double tolerance = 0.1; 068 vs.computeDistribution(); 069 assertTrue("empirical distribution property", 070 vs.getEmpiricalDistribution() != null); 071 SummaryStatistics stats = new SummaryStatistics(); 072 for (int i = 1; i < 1000; i++) { 073 next = vs.getNext(); 074 stats.addValue(next); 075 } 076 assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); 077 assertEquals 078 ("std dev", 1.0173699343977738, stats.getStandardDeviation(), 079 tolerance); 080 081 vs.computeDistribution(500); 082 stats = new SummaryStatistics(); 083 for (int i = 1; i < 1000; i++) { 084 next = vs.getNext(); 085 stats.addValue(next); 086 } 087 assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); 088 assertEquals 089 ("std dev", 1.0173699343977738, stats.getStandardDeviation(), 090 tolerance); 091 092 } 093 094 /** 095 * Make sure exception thrown if digest getNext is attempted 096 * before loading empiricalDistribution. 097 */ 098 public void testNextDigestFail() throws Exception { 099 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 }