1 #include "TestDDaceRandomSampler.h"
2 //#include "ArgumentMisMatchException.h"
3 #include "Distribution.h"
4 #include "arrcmp.h"
5 #include <stdlib.h>
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9
10 using namespace std;
11
TestDDaceRandomSampler()12 TestDDaceRandomSampler::TestDDaceRandomSampler() : seed( 779 )
13 {
14
15 /* For unit tests, we do NOT want a random number generator. */
16 /* We want a number generator that pushes out a known sequence of numbers.*/
17 DistributionBase::usePseudoRandom(true);
18
19
20 // set the seed to ensure the data matches
21 DistributionBase::setSeed( seed );
22
23 // create distributions need by sampler
24 dists.resize( 0 );
25 dists.push_back( Distribution( UniformDistribution( 0, 10 ) ) );
26 dists.push_back( Distribution( UniformDistribution( 0, 10 ) ) );
27 dists.push_back( Distribution( UniformDistribution( 0, 10 ) ) );
28
29 // set up the getSamples test data
30 int i,j;
31 std::vector<double> tmp( 0 );
32
33
34 double pts[3][3] = { {7.79, 7.8, 7.81},
35 {7.82, 7.83, 7.84},
36 {7.85, 7.86, 7.87} };
37
38 test_data.resize( 0 );
39
40 for( i = 0; i < 3; i++ )
41 {
42 test_data.push_back( tmp );
43 for( j = 0; j < 3; j++ )
44 {
45 test_data[i].push_back( pts[i][j] );
46 }
47 }
48 }
49
~TestDDaceRandomSampler()50 TestDDaceRandomSampler::~TestDDaceRandomSampler()
51 {
52 /* Restore the Random Number Generator */
53 DistributionBase::usePseudoRandom(false);
54
55 // reseed the Random Number Generator
56 DistributionBase::setSeed( seed );
57 }
58
run()59 void TestDDaceRandomSampler::run()
60 {
61 testDDaceRandomSamplerWithDist();
62 testDDaceRandomSamplerWithoutDist();
63 testGetSamples();
64 testClone();
65 testPrint();
66 testTypeName();
67 testGetParameter();
68 }
69
testDDaceRandomSamplerWithDist()70 void TestDDaceRandomSampler::testDDaceRandomSamplerWithDist()
71 {
72 // call constructor DDaceRandomSampler(int nSamples, const Array<Distribution>& dist);
73 DDaceRandomSampler samplerwd( 3, dists );
74
75 _test( samplerwd.nSamples() == 3 );
76 _test( samplerwd.nInputs() == 3 );
77 _test( samplerwd.dist().size() == 3 );
78
79 for( int i = 0; i < (int) samplerwd.dist().size(); i++ )
80 {
81 _test( dists[i].lowerBound() == samplerwd.dist()[i].lowerBound() );
82 _test( dists[i].upperBound() == samplerwd.dist()[i].upperBound() );
83 }
84 }
85
testDDaceRandomSamplerWithoutDist()86 void TestDDaceRandomSampler::testDDaceRandomSamplerWithoutDist()
87 {
88 // call constructor DDaceRandomSampler(int nSamples, int nInputs);
89 DDaceRandomSampler samplernd( 3, 3 );
90
91 _test( samplernd.nSamples() == 3 );
92 _test( samplernd.nInputs() == 3 );
93 _test( samplernd.dist().size() == 0 );
94
95 for( int i = 0; i < (int) samplernd.dist().size(); i++ )
96 {
97 _test( dists[i].lowerBound() == samplernd.dist()[i].lowerBound() );
98 _test( dists[i].upperBound() == samplernd.dist()[i].upperBound() );
99 }
100 }
101
testGetSamples()102 void TestDDaceRandomSampler::testGetSamples()
103 {
104 int i,j;
105
106 /* reset the seed */
107 DistributionBase::setSeed( seed );
108
109 /**
110 * dists[] is an array that contains 3 different uniform distributions.
111 * From each distribution, use a random # generator to generate one value.
112 */
113 DDaceRandomSampler samplerwd( 3, dists );
114
115
116 DDaceRandomSampler samplernd( 3, 3 );
117
118 std::vector<DDaceSamplePoint> pass( 0 );
119 std::vector<DDaceSamplePoint> ret( 0 );
120
121 // call getSamples and test the results
122 // the array of sample data points is passed into "pass"
123 //ret = samplerwd.getSamples( pass );
124 samplerwd.getSamples( pass );
125 ret = pass;
126
127
128 if( _test( Arrcmp_ad_est( pass, test_data, 0.001) == 0 ) == false )
129 {
130 for( i = 0; i < 3; i++ )
131 {
132 cout << "[";
133 for( j = 0; j < 3; j++ )
134 {
135 cout << "(" << pass[i][j] << "|" << test_data[i][j] << "), ";
136 }
137 cout << "]" << endl;
138 }
139 }
140 _test( Arrcmp_ad_est( ret, test_data, 0.001 ) == 0 );
141
142 //try {
143 //ret = samplernd.getSamples( pass );
144 //samplernd.getSamples( pass );
145 //ret = pass;
146
147 // if here then exception was thrown, record failure
148 //_test( false );
149 //} catch( ArgumentMisMatchException e ) {
150 //} catch (...) {
151
152 // if here exception was thrown, record success
153 //_test( true );
154 //}
155 }
156
testClone()157 void TestDDaceRandomSampler::testClone()
158 {
159 DDaceRandomSampler samplerwd( 3, dists );
160 DDaceRandomSampler samplernd( 3, 3 );
161 std::vector<double> a;
162 std::vector<double> b;
163
164 // call clone and test the results
165 DDaceRandomSampler* rtn = (DDaceRandomSampler*)samplerwd.clone();
166
167 // check internal values to see if clone() worked
168 _test( rtn->typeName() == samplerwd.typeName() );
169 _test( rtn->nSamples() == samplerwd.nSamples() );
170 _test( rtn->nInputs() == samplerwd.nInputs() );
171
172 if( _test( (rtn->dist()).size() == samplerwd.dist().size() ) )
173 {
174 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
175 {
176 _test( (rtn->dist())[i].lowerBound() == samplerwd.dist()[i].lowerBound() );
177 _test( (rtn->dist())[i].upperBound() == samplerwd.dist()[i].upperBound() );
178 }
179 }
180
181
182 a = rtn->lowerBounds();
183 b = samplerwd.lowerBounds();
184 _test( Arrcmp_d( a, b ) == 0 );
185 a.resize( 0 );
186 b.resize( 0 );
187
188 a = rtn->upperBounds();
189 b = samplerwd.upperBounds();
190 _test( Arrcmp_d( a, b ) == 0 );
191
192 // clean up the dynamic memory
193 delete rtn;
194
195 // call clone and test the results
196 rtn = (DDaceRandomSampler*)samplernd.clone();
197
198 // check internal values to see if clone() worked
199 _test( rtn->typeName() == samplernd.typeName() );
200 _test( rtn->nSamples() == samplernd.nSamples() );
201 _test( rtn->nInputs() == samplernd.nInputs() );
202
203 if( _test( (rtn->dist()).size() == samplernd.dist().size() ) )
204 {
205 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
206 {
207 _test( (rtn->dist())[i].lowerBound() == samplernd.dist()[i].lowerBound() );
208 _test( (rtn->dist())[i].upperBound() == samplernd.dist()[i].upperBound() );
209 }
210 }
211
212
213 a = rtn->lowerBounds();
214 b = samplernd.lowerBounds();
215 _test( Arrcmp_d( a, b ) == 0 );
216 a.resize( 0 );
217 b.resize( 0 );
218
219 a = rtn->upperBounds();
220 b = samplernd.upperBounds();
221 _test( Arrcmp_d( a, b ) == 0 );
222
223 // clean up the dynamic memory
224 delete rtn;
225 }
226
testPrint()227 void TestDDaceRandomSampler::testPrint()
228 {
229 DDaceRandomSampler samplerwd( 3, dists );
230 DDaceRandomSampler samplernd( 3, 3 );
231
232 char buf[256];
233 char num[16];
234 string data;
235 string test_str;
236
237 // convert seed from 'long' to 'char*'
238 itoa( DistributionBase::seed(), num, 16 );
239
240 test_str = "<Random samples=\"3\" seed=\"";
241 test_str += string( num );
242 test_str += "\"/>";
243
244 ifstream fin;
245 ofstream fout;
246
247 // write data to file then read the data
248 // back from the file to verify it out
249 // what it was suppose to
250
251 fout.open( "TestDDaceRandomSamplerWD_Log" );
252 if( fout )
253 {
254 samplerwd.print( fout );
255 fout << endl;
256 fout.close();
257 }
258 else
259 {
260 _test( false );
261 }
262
263 fin.open( "TestDDaceRandomSamplerWD_Log" );
264 if( fin )
265 {
266 fin.getline( buf, 255 );
267 data = buf;
268
269 _test( data == test_str );
270
271 fin.close();
272 }
273 else
274 {
275 _test( false );
276 }
277
278 // write data to file then read the data
279 // back from the file to verify it out
280 // what it was suppose to
281
282 fout.open( "TestDDaceRandomSamplerND_Log" );
283 if( fout )
284 {
285 samplernd.print( fout );
286 fout << endl;
287 fout.close();
288 }
289 else
290 {
291 _test( false );
292 }
293
294 fin.open( "TestDDaceRandomSamplerND_Log" );
295 if( fin )
296 {
297 fin.getline( buf, 255 );
298 data = buf;
299
300 _test( data == test_str );
301
302 fin.close();
303 }
304 else
305 {
306 _test( false );
307 }
308 }
309
testTypeName()310 void TestDDaceRandomSampler::testTypeName()
311 {
312 DDaceRandomSampler samplerwd( 3, dists );
313 DDaceRandomSampler samplernd( 3, 3 );
314
315 // call typeName and test the results
316 _test( samplerwd.typeName() == std::string( "DDaceRandomSampler" ) );
317 _test( samplernd.typeName() == std::string( "DDaceRandomSampler" ) );
318 }
319
testGetParameter()320 void TestDDaceRandomSampler::testGetParameter()
321 {
322 DDaceRandomSampler samplerwd( 3, dists );
323 DDaceRandomSampler samplernd( 3, 3 );
324
325 // call getParamter, should throw an exception
326 try {
327 samplerwd.getParameter( std::string( "INPUT" ) );
328
329 // if here then exception was not thrown record failure
330 _test(false);
331 } catch( std::exception& e ) {
332 // if here then exception was thrown record success
333 _test( true );
334 }
335
336 try {
337 samplernd.getParameter( std::string( "INPUT" ) );
338
339 // if here then exception was not thrown, record failure
340 _test( false );
341 } catch( std::exception& e ) {
342 // if here then exception was thrown record success
343 _test( true );
344 }
345 }
346
347