1 16 package org.apache.commons.math.random; 17 18 import junit.framework.Test; 19 import junit.framework.TestSuite; 20 import java.net.URL ; 21 22 import org.apache.commons.math.RetryTestCase; 23 import org.apache.commons.math.stat.descriptive.SummaryStatistics; 24 25 30 31 public final class ValueServerTest extends RetryTestCase { 32 33 private ValueServer vs = new ValueServer(); 34 35 public ValueServerTest(String name) { 36 super(name); 37 } 38 39 public void setUp() { 40 vs.setMode(ValueServer.DIGEST_MODE); 41 try { 42 URL url = getClass().getResource("testData.txt"); 43 vs.setValuesFileURL(url); 44 } catch (Exception 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 61 public void testNextDigest() throws Exception { 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 94 public void testNextDigestFail() throws Exception { 95 try { 96 vs.getNext(); 97 fail("Expecting IllegalStateException"); 98 } catch (IllegalStateException ex) {;} 99 } 100 101 106 public void testReplay() throws Exception { 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 vs.closeReplayFile(); 127 } 128 129 132 public void testModes() throws Exception { 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 ex) { 153 ; 154 } 155 } 156 157 160 public void testFill() throws Exception { 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 177 public void testProperties() throws Exception { 178 vs.setMode(ValueServer.CONSTANT_MODE); 179 assertEquals("mode test",ValueServer.CONSTANT_MODE,vs.getMode()); 180 vs.setValuesFileURL("http://www.apache.org"); 181 URL url = vs.getValuesFileURL(); 182 assertEquals("valuesFileURL test","http://www.apache.org",url.toString()); 183 } 184 185 } 186 | Popular Tags |