1 #include "TestUniformDistribution.h"
2 #include "Distribution.h"
3 #include "arrcmp.h"
4 #include <cstdlib>
5 #include <cstdio>
6 #include <cmath>
7 #include <iostream>
8 #include <fstream>
9 #include <string.h>
10
11 using namespace std;
12
TestUniformDistribution()13 TestUniformDistribution::TestUniformDistribution()
14 {
15 // set the seed
16 seed = 779;
17 DistributionBase::setSeed( seed );
18
19 // set lower and upper bounds
20 lb = 5;
21 ub = 10;
22
23 /* get some deviates */
24 UniformDistribution distnb;
25 DistributionBase::setSeed( seed );
26 this->deviates[0] = distnb.getDeviate();
27 DistributionBase::setSeed( seed );
28
29 UniformDistribution distwb( lb, ub );
30 DistributionBase::setSeed( seed );
31 this->deviates[1] = distwb.getDeviate();
32 DistributionBase::setSeed( seed );
33
34
35
36 }
37
~TestUniformDistribution()38 TestUniformDistribution::~TestUniformDistribution()
39 {
40 }
41
run()42 void TestUniformDistribution::run()
43 {
44 testUniformDistributionNoBounds();
45 testUniformDistributionWithBounds();
46 testClone();
47 testGetDeviateNoProb();
48 testGetDeviateWithProb();
49 testGetCDF();
50 testLowerBound();
51 testUpperBound();
52 testMean();
53 testStdDev();
54 testPrint();
55 testPrintAttributes();
56 testTypeName();
57 }
testUniformDistributionNoBounds()58 void TestUniformDistribution::testUniformDistributionNoBounds()
59 {
60 // use constructor UniformDistribution();
61 UniformDistribution distnb;
62
63 _test( distnb.lowerBound() == 0 );
64 _test( distnb.upperBound() == 1 );
65 }
66
testUniformDistributionWithBounds()67 void TestUniformDistribution::testUniformDistributionWithBounds()
68 {
69 // use constructor UniformDistribution(double lowerBound, double upperBound);
70 UniformDistribution distwb( lb, ub );
71
72 _test( distwb.lowerBound() == lb );
73 _test( distwb.upperBound() == ub );
74 }
75
testClone()76 void TestUniformDistribution::testClone()
77 {
78 UniformDistribution distnb;
79 UniformDistribution distwb( lb, ub );
80
81 // call clone and test results
82 UniformDistribution* rtn = (UniformDistribution*)distnb.clone();
83
84 _test( rtn->lowerBound() == distnb.lowerBound() );
85 _test( rtn->upperBound() == distnb.upperBound() );
86 _test( rtn->mean() == distnb.mean() );
87 _test( rtn->stdDev() == distnb.stdDev() );
88 _test( rtn->typeName() == distnb.typeName() );
89
90 // clean up memory
91 delete rtn;
92
93 // call clone and test results
94 rtn = (UniformDistribution*)distwb.clone();
95
96 _test( rtn->lowerBound() == distwb.lowerBound() );
97 _test( rtn->upperBound() == distwb.upperBound() );
98 _test( rtn->mean() == distwb.mean() );
99 _test( rtn->stdDev() == distwb.stdDev() );
100 _test( rtn->typeName() == distwb.typeName() );
101
102 // clean up memory
103 delete rtn;
104 }
105
testGetDeviateNoProb()106 void TestUniformDistribution::testGetDeviateNoProb()
107 {
108 DistributionBase::setSeed( seed );
109 UniformDistribution distnb;
110 DistributionBase::setSeed( seed );
111 double deviate = distnb.getDeviate();
112 _test( fabs(deviate - this->deviates[0]) < 0.001 );
113 DistributionBase::setSeed( seed );
114
115
116 UniformDistribution distwb( lb, ub );
117 DistributionBase::setSeed( seed );
118 deviate = distwb.getDeviate();
119 _test( fabs(deviate - this->deviates[1]) < 0.001 );
120 DistributionBase::setSeed( seed );
121 }
122
testGetDeviateWithProb()123 void TestUniformDistribution::testGetDeviateWithProb()
124 {
125 UniformDistribution distnb;
126 UniformDistribution distwb( lb, ub );
127
128 // test the returned deviate value
129 _test( distnb.getDeviate( 0.5 ) == 0.5 );
130 _test( distwb.getDeviate( 0.5 ) == (lb + (ub-lb)*0.5) );
131 }
132
testGetCDF()133 void TestUniformDistribution::testGetCDF()
134 {
135 UniformDistribution distnb;
136 UniformDistribution distwb( lb, ub );
137
138 // test the returned CDF value
139 _test( distnb.getCDF( 0.5 ) == 0.5 );
140
141 double a = distwb.getCDF( 0.5 );
142 double b = ((0.5-lb)/(ub-lb));
143 _test( a == b );
144 // _test( distwb.getCDF( 0.5 ) == ((0.5-lb)/(ub-lb)) );
145 }
146
testLowerBound()147 void TestUniformDistribution::testLowerBound()
148 {
149 UniformDistribution distnb;
150 UniformDistribution distwb( lb, ub );
151
152 _test( distnb.lowerBound() == 0 );
153
154 _test( distwb.lowerBound() == lb );
155 }
156
testUpperBound()157 void TestUniformDistribution::testUpperBound()
158 {
159 UniformDistribution distnb;
160 UniformDistribution distwb( lb, ub );
161
162 _test( distnb.upperBound() == 1 );
163
164 _test( distwb.upperBound() == ub );
165 }
166
testMean()167 void TestUniformDistribution::testMean()
168 {
169 UniformDistribution distnb;
170 UniformDistribution distwb( lb, ub );
171
172 _test( distnb.mean() == 0.5 );
173
174 _test( distwb.mean() == lb+(ub-lb)/2 );
175 }
176
testStdDev()177 void TestUniformDistribution::testStdDev()
178 {
179 UniformDistribution distnb;
180 UniformDistribution distwb( lb, ub );
181
182 _test( fabs(distnb.stdDev() - sqrt(1/12.0)) < 1.0e-10);
183
184 _test( fabs(distwb.stdDev() - sqrt(pow(ub-lb,2)/12.0)) < 1.0e-10 );
185 }
186
testPrint()187 void TestUniformDistribution::testPrint()
188 {
189 char buf[128];
190
191 UniformDistribution distnb;
192 UniformDistribution distwb( lb, ub );
193
194 ofstream fout;
195 ifstream fin;
196
197 string data;
198 string test_str;
199
200 #ifdef HAVE_SNPRINTF
201 snprintf( buf, 128, "UNIFORM 0 1" );
202 #else
203 int numberOfChars = sprintf( buf, "UNIFORM 0 1" );
204 assert(numberOfChars < 128);
205 #endif // HAVE_SNPRINTF
206
207 test_str = buf;
208
209 // write the data to a file, then reopen the
210 // file and verify the data was written correctly
211 fout.open( "TestUniformDistribution_Log" );
212 if( fout )
213 {
214 distnb.print( fout );
215 fout << endl;
216 fout.close();
217 }
218 else
219 {
220 _test( false );
221 }
222
223 fin.open( "TestUniformDistribution_Log" );
224 if( fin )
225 {
226 fin.getline( buf, 128 );
227 data = buf;
228
229 _test( data == test_str );
230 fin.close();
231 }
232 else
233 {
234 _test( false );
235 }
236
237 // write the data to a file, then reopen the
238 // file and verify the data was written correctly
239 memset( buf, 0, 128 );
240 #ifdef HAVE_SNPRINTF
241 snprintf( buf, 128, "UNIFORM %d %d", (int)lb, (int)ub );
242 #else
243 numberOfChars = sprintf( buf, "UNIFORM %d %d", (int)lb, (int)ub );
244 assert(numberOfChars < 128);
245 #endif // HAVE_SNPRINTF
246 test_str = buf;
247
248 fout.open( "TestUniformDistribution_Log" );
249 if( fout )
250 {
251 distwb.print( fout );
252 fout << endl;
253 fout.close();
254 }
255 else
256 {
257 _test( false );
258 }
259
260 fin.open( "TestUniformDistribution_Log" );
261 if( fin )
262 {
263 fin.getline( buf, 128 );
264 data = buf;
265
266 _test( data == test_str );
267 fin.close();
268 }
269 else
270 {
271 _test( false );
272 }
273 }
274
275
testPrintAttributes()276 void TestUniformDistribution::testPrintAttributes()
277 {
278 char buf[128];
279
280 UniformDistribution distnb;
281 UniformDistribution distwb( lb, ub );
282
283 ofstream fout;
284 ifstream fin;
285
286 string data;
287 string test_str;
288
289 #ifdef HAVE_SNPRINTF
290 snprintf( buf, 128, "distribution=\"uniform\" lower=\"0\" upper=\"1\"");
291 #else
292 int n = sprintf( buf, "distribution=\"uniform\" lower=\"0\" upper=\"1\"");
293 assert(n < 128);
294 #endif // HAVE_SNPRINTF
295 test_str = buf;
296
297 // write the data to a file, then reopen the
298 // file and verify the data was written correctly
299 fout.open( "TestUniformDistribution_Log" );
300 if( fout )
301 {
302 distnb.printAttributes( fout );
303 fout << endl;
304 fout.close();
305 }
306 else
307 {
308 _test( false );
309 }
310
311 fin.open( "TestUniformDistribution_Log" );
312 if( fin )
313 {
314 fin.getline( buf, 128 );
315 data = buf;
316
317 _test( data == test_str );
318 fin.close();
319 }
320 else
321 {
322 _test( false );
323 }
324
325 // write the data to a file, then reopen the
326 // file and verify the data was written correctly
327 memset( buf, 0, 128 );
328 #ifdef HAVE_SNPRINTF
329 snprintf( buf, 128, "distribution=\"uniform\" lower=\"%d\" upper=\"%d\"", (int)lb, (int)ub );
330 #else
331 n = sprintf( buf, "distribution=\"uniform\" lower=\"%d\" upper=\"%d\"", (int)lb, (int)ub );
332 assert(n < 128);
333 #endif // HAVE_SNPRINTF
334 test_str = buf;
335
336 fout.open( "TestUniformDistribution_Log" );
337 if( fout )
338 {
339 distwb.printAttributes( fout );
340 fout << endl;
341 fout.close();
342 }
343 else
344 {
345 _test( false );
346 }
347
348 fin.open( "TestUniformDistribution_Log" );
349 if( fin )
350 {
351 fin.getline( buf, 128 );
352 data = buf;
353
354 _test( data == test_str );
355 fin.close();
356 }
357 else
358 {
359 _test( false );
360 }
361 }
362
testTypeName()363 void TestUniformDistribution::testTypeName()
364 {
365 UniformDistribution distnb;
366 UniformDistribution distwb( lb, ub );
367
368 _test( distnb.typeName() == std::string( "UniformDistribution" ) );
369 _test( distwb.typeName() == std::string( "UniformDistribution" ) );
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407