1 #include "TestDDaceFactorialSampler.h"
2 //#include "ArgumentMisMatchException.h"
3 #include "Distribution.h"
4 #include "arrcmp.h"
5 #include <cstdlib>
6 #include <fstream>
7 #include <iostream>
8 #include <string>
9
10 using namespace std;
11
TestDDaceFactorialSampler()12 TestDDaceFactorialSampler::TestDDaceFactorialSampler() : 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 // set random number generator seed
20 DistributionBase::setSeed( seed );
21
22 // set up the getSamples test data
23 int i,j;
24 std::vector<double> tmp( 0 );
25 double pts[16][2] = { {12.5,12.5}, // correct data for nSamples=16, nSymbols=4, nInputs=2
26 {37.5,12.5},
27 {62.5,12.5},
28 {87.5,12.5},
29 {12.5,37.5},
30 {37.5,37.5},
31 {62.5,37.5},
32 {87.5,37.5},
33 {12.5,62.5},
34 {37.5,62.5},
35 {62.5,62.5},
36 {87.5,62.5},
37 {12.5,87.5},
38 {37.5,87.5},
39 {62.5,87.5},
40 {87.5,87.5} };
41
42 double pts_wn[16][2] = {{19.475, 19.5},
43 {44.525, 19.55},
44 {69.575, 19.6},
45 {94.625,19.65},
46 {19.675,44.7},
47 {44.725,44.75},
48 {69.775,44.8},
49 {94.825,44.85},
50 {19.875,69.9},
51 {44.925,69.95},
52 {69.975,70},
53 {95.025,70.05},
54 {20.075,95.1},
55 {45.125,95.15},
56 {70.175,95.2},
57 {95.225,95.25}};
58
59 DistributionBase::setSeed( seed );
60
61 // create distributions need by sampler
62 dists.resize( 0 );
63 dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
64 dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
65 std::vector<DDaceSamplePoint> data( 0 );
66 DistributionBase::setSeed( seed );
67
68
69
70 test_data.resize( 0 );
71 test_data_wn.resize( 0 );
72
73 for( i = 0; i < 16; i++ )
74 {
75 test_data.push_back( tmp );
76 test_data_wn.push_back( tmp );
77 for( j = 0; j < 2; j++ )
78 {
79 test_data[i].push_back( pts[i][j] );
80 test_data_wn[i].push_back( pts_wn[i][j] );
81 }
82 }
83 }
84
~TestDDaceFactorialSampler()85 TestDDaceFactorialSampler::~TestDDaceFactorialSampler()
86 {
87 /* Restore the Random Number Generator */
88 DistributionBase::usePseudoRandom(false);
89
90 // reseed the Random Number Generator
91 DistributionBase::setSeed( seed );
92 }
93
run()94 void TestDDaceFactorialSampler::run()
95 {
96 testDDaceFactorialSampler2();
97 testDDaceFactorialSampler4();
98 testGetSamplesWithoutNoise();
99 testGetSamplesWithNoise();
100 testClone();
101 testPrint();
102 testTypeName();
103 testGetParameter();
104 }
105
testDDaceFactorialSampler4()106 void TestDDaceFactorialSampler::testDDaceFactorialSampler4()
107 {
108 // create a factorial sampler using the constructor
109 // DDaceFactorialSampler(int nSamples, int nSymbols, bool noise,
110 // const Array<Distribution>& dist);
111 try {
112 DDaceFactorialSampler sampler4( 16, 4, false, dists );
113
114 // if here then no exception was thrown, record success
115 _test( true );
116 _test( sampler4.nSamples() == 16 );
117 _test( sampler4.nInputs() == 2 );
118 //_test( sampler4.nSymbols() == 4 );
119 _test( sampler4.noise() == false );
120 _test( sampler4.dist().size() == 2 );
121
122 for( int i = 0; i < (int) sampler4.dist().size(); i++ )
123 {
124 _test( dists[i].lowerBound() == sampler4.dist()[i].lowerBound() );
125 _test( dists[i].upperBound() == sampler4.dist()[i].upperBound() );
126 }
127 } catch( std::exception& e ) {
128
129 // if here then exception was thrown, record failure
130 _test( false );
131 }
132 }
133
testDDaceFactorialSampler2()134 void TestDDaceFactorialSampler::testDDaceFactorialSampler2()
135 {
136 // create a factorial sampler using the constructor
137 // DDaceFactorialSampler(int nSamples, int nSymbols);
138 try {
139 DDaceFactorialSampler sampler2( 16, 4 );
140
141 // if here then no exception was thrown, record success
142 _test( true );
143 _test( sampler2.nSamples() == 16 );
144 _test( sampler2.nInputs() == 2 );
145 //_test( sampler2.nSymbols() == 4 );
146 _test( sampler2.noise() == false );
147 _test( sampler2.dist().size() == 0 );
148
149 for( int i = 0; i < (int) sampler2.dist().size(); i++ )
150 {
151 _test( dists[i].lowerBound() == sampler2.dist()[i].lowerBound() );
152 _test( dists[i].upperBound() == sampler2.dist()[i].upperBound() );
153 }
154 } catch( std::exception& e ) {
155
156 // if here then exception was thrown, record failure
157 _test( false );
158 }
159 }
160
testGetSamplesWithoutNoise()161 void TestDDaceFactorialSampler::testGetSamplesWithoutNoise()
162 {
163 DistributionBase::setSeed( seed );
164 DDaceFactorialSampler sampler4( 16, 4, false, dists );
165 DDaceFactorialSampler sampler2( 16, 4 );
166
167 std::vector<DDaceSamplePoint> pass( 0 );
168 std::vector<DDaceSamplePoint> ret( 0 );
169
170 // call getSamples and test the results
171 //ret = sampler4.getSamples( pass );
172 DistributionBase::setSeed( seed );
173 sampler4.getSamples( pass );
174 ret = pass;
175
176 _test( Arrcmp_ad( pass, test_data ) == 0 );
177 _test( Arrcmp_ad( ret, test_data ) == 0 );
178
179 try {
180 //ret = sampler2.getSamples( pass );
181 sampler2.getSamples( pass );
182 ret = pass;
183
184 // if here then exception was thrown, record failure
185 _test( false );
186 } catch( std::exception& e ) {
187
188 // if here exception was thrown, record success
189 _test( true );
190
191 } catch(...) {
192 // if here, an exception was throw that was not a std::exception
193 // hence record failure.
194 _test( false );
195 }
196 }
197
198
199
200
201
202
testGetSamplesWithNoise()203 void TestDDaceFactorialSampler::testGetSamplesWithNoise()
204 {
205 int i,j;
206
207 DistributionBase::setSeed( seed );
208 DDaceFactorialSampler sampler4( 16, 4, true, dists );
209
210 std::vector<DDaceSamplePoint> pass( 0 );
211 std::vector<DDaceSamplePoint> ret( 0 );
212
213 // call getSamples and test the results
214 //ret = sampler4.getSamples( pass );
215 DistributionBase::setSeed( seed );
216 sampler4.getSamples( pass );
217 ret = pass;
218
219 if( _test( Arrcmp_ad_est( pass, test_data_wn, 0.001 ) == 0 ) == false )
220 {
221 for( i = 0; i < 16; i++ )
222 {
223 cout << "[";
224 for( j = 0; j < 2; j++ )
225 {
226 cout << "(" << pass[i][j] << "|" << test_data_wn[i][j] << "), ";
227 }
228 cout << "]" << endl;
229 }
230 }
231 _test( Arrcmp_ad_est( ret, test_data_wn, 0.001 ) == 0 );
232 }
233
testClone()234 void TestDDaceFactorialSampler::testClone()
235 {
236 DDaceFactorialSampler sampler4( 16, 4, false, dists );
237 DDaceFactorialSampler sampler2( 16, 4 );
238 std::vector<double> a;
239 std::vector<double> b;
240
241 // call clone and test the results
242 DDaceFactorialSampler* rtn = (DDaceFactorialSampler*)sampler4.clone();
243
244 // check internal values to see if clone() worked
245 _test( rtn->typeName() == sampler4.typeName() );
246 _test( rtn->nSamples() == sampler4.nSamples() );
247 //_test( rtn->nSymbols() == sampler4.nSymbols() );
248 _test( rtn->nInputs() == sampler4.nInputs() );
249 _test( rtn->noise() == sampler4.noise() );
250
251 if( _test( (rtn->dist()).size() == sampler4.dist().size() ) )
252 {
253 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
254 {
255 _test( (rtn->dist())[i].lowerBound() == sampler4.dist()[i].lowerBound() );
256 _test( (rtn->dist())[i].upperBound() == sampler4.dist()[i].upperBound() );
257 }
258 }
259
260
261 a = rtn->lowerBounds();
262 b = sampler4.lowerBounds();
263 _test( Arrcmp_d( a, b ) == 0 );
264 a.resize( 0 );
265 b.resize( 0 );
266
267 a = rtn->upperBounds();
268 b = sampler4.upperBounds();
269 _test( Arrcmp_d( a, b ) == 0 );
270
271 // clean up the dynamic memory
272 delete rtn;
273
274 // call clone and test the results
275 rtn = (DDaceFactorialSampler*)sampler2.clone();
276
277 // check internal values to see if clone() worked
278 _test( rtn->typeName() == sampler2.typeName() );
279 _test( rtn->nSamples() == sampler2.nSamples() );
280 //_test( rtn->nSymbols() == sampler2.nSymbols() );
281 _test( rtn->nInputs() == sampler2.nInputs() );
282 _test( rtn->noise() == sampler2.noise() );
283
284 if( _test( (rtn->dist()).size() == sampler2.dist().size() ) )
285 {
286 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
287 {
288 _test( (rtn->dist())[i].lowerBound() == sampler2.dist()[i].lowerBound() );
289 _test( (rtn->dist())[i].upperBound() == sampler2.dist()[i].upperBound() );
290 }
291 }
292
293
294 a = rtn->lowerBounds();
295 b = sampler2.lowerBounds();
296 _test( Arrcmp_d( a, b ) == 0 );
297 a.resize( 0 );
298 b.resize( 0 );
299
300 a = rtn->upperBounds();
301 b = sampler2.upperBounds();
302 _test( Arrcmp_d( a, b ) == 0 );
303
304 // clean up the dynamic memory
305 delete rtn;
306 }
307
testPrint()308 void TestDDaceFactorialSampler::testPrint()
309 {
310 DDaceFactorialSampler sampler4( 16, 4, false, dists );
311 DDaceFactorialSampler sampler2( 16, 4 );
312 char buf[256];
313 char num[16];
314 string data;
315 string test_str;
316
317 // convert seed from 'long' to 'char*'
318 itoa( DistributionBase::seed(), num, 16 );
319
320 test_str = "<Factorial samples=\"16\" symbols=\"4\" perturb=\"false\" seed=\"";
321 test_str += string( num );
322 test_str += "\"/>";
323
324 ifstream fin;
325 ofstream fout;
326
327 // write data to file then read the data
328 // back from the file to verify it out
329 // what it was suppose to
330
331 fout.open( "TestDDaceFactorialSampler4_Log" );
332 if( fout )
333 {
334 sampler4.print( fout );
335 fout << endl;
336 fout.close();
337 }
338 else
339 {
340 _test( false );
341 }
342
343 fin.open( "TestDDaceFactorialSampler4_Log" );
344 if( fin )
345 {
346 fin.getline( buf, 255 );
347 data = buf;
348
349 _test( data == test_str );
350
351 fin.close();
352 }
353 else
354 {
355 _test( false );
356 }
357
358 // write data to file then read the data
359 // back from the file to verify it out
360 // what it was suppose to
361
362 fout.open( "TestDDaceFactorialSampler2_Log" );
363 if( fout )
364 {
365 sampler2.print( fout );
366 fout << endl;
367 fout.close();
368 }
369 else
370 {
371 _test( false );
372 }
373
374 fin.open( "TestDDaceFactorialSampler2_Log" );
375 if( fin )
376 {
377 fin.getline( buf, 255 );
378 data = buf;
379
380 _test( data == test_str );
381
382 fin.close();
383 }
384 else
385 {
386 _test( false );
387 }
388 }
389
testTypeName()390 void TestDDaceFactorialSampler::testTypeName()
391 {
392 DDaceFactorialSampler sampler4( 16, 4, false, dists );
393 DDaceFactorialSampler sampler2( 16, 4 );
394
395 // call type name and check the results
396 _test( sampler4.typeName() == "DDaceFactorialSampler" );
397 _test( sampler2.typeName() == "DDaceFactorialSampler" );
398 }
399
testGetParameter()400 void TestDDaceFactorialSampler::testGetParameter()
401 {
402 DDaceFactorialSampler sampler4( 16, 4, false, dists );
403 DDaceFactorialSampler sampler2( 16, 4 );
404
405 // call getParameters and test the output
406 try {
407 _test( sampler4.getParameter( std::string( "SYMBOLS" ) ) == 4 );
408 } catch( std::exception& e ) {
409 // if here then exception was thrown record failure
410 _test( false );
411 }
412
413 try {
414 _test( sampler2.getParameter( std::string( "SYMBOLS" ) ) == 4 );
415 } catch( std::exception& e ) {
416 // if here then exception was thrown record failure
417 _test( false );
418 }
419
420 try {
421 sampler4.getParameter( std::string( "WRONG_INPUT" ) );
422
423 // if here then exception was not thrown record failure
424 } catch( std::exception& e ) {
425 // if here then exception was thrown record success
426 _test( true );
427 }
428
429 try {
430 sampler2.getParameter( std::string( "WRONG_INPUT" ) );
431
432 // if here then exception was not thrown, record failure
433 _test( false );
434 } catch( std::exception& e ) {
435 // if here then exception was thrown record success
436 _test( true );
437 }
438 }
439
440