1 #include "TestDDaceSampler.h"
2 //#include "DDaceArraySampler.h"
3 #include "UniformDistribution.h"
4 #include "DDaceFactorialSampler.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <string>
8 
9 using namespace std;
10 
Arrcmp_sp(const std::vector<DDaceSamplePoint> & a,const std::vector<DDaceSamplePoint> & b)11 int Arrcmp_sp( const std::vector<DDaceSamplePoint> & a,
12 		 const std::vector<DDaceSamplePoint> & b )
13 {
14   int i,j;
15   int len_a = a.size();
16   int len_b = b.size();
17 
18   if( len_a != len_b ) {
19     return 1;    // failed! the arrays don't match
20   }
21 
22   for( i = 0; i < len_a; i++ ) {
23     if( a[i].length() != b[i].length() ) {
24       return 1;  // failed! the arrays don't match
25     }
26     for( j = 0; j < a[i].length(); j++ ) {
27       if( a[i][j] != b[i][j] ) {
28         return 1;  // failed! the arrays don't match
29       }
30     }
31   }
32 
33   return 0;     // success! the arrays match
34 }
35 
Arrcmp_d(const std::vector<double> & a,const std::vector<double> & b)36 int Arrcmp_d( const std::vector<double> & a, const std::vector<double> & b )
37 {
38   int i;
39   int len_a = a.size();
40   int len_b = b.size();
41 
42   if( len_a != len_b ) {
43     return 1;   // failed! the arrays don't match
44   }
45 
46   for( i = 0; i < len_a; i++ ) {
47      if( a[i] != b[i] ) {
48        return 1;   // failed! the arrays don't match
49      }
50   }
51 
52   return 0;     // success! the arrays match
53 }
54 
55 
TestDDaceSampler()56 TestDDaceSampler::TestDDaceSampler() :
57     data_d(0), data_sp(0), lb(0), ub(0)
58 {
59    // int i,j;
60 
61    std::vector<double>     tmp( 0 );
62 /**
63    double num[4][4] = { { 6, 6, 7, 6 },
64                         { 2, 0, 2, 5 },
65                         { 2, 4, 0, 5 },
66                         { 4, 4, 5, 7 } };
67    double lowb[4] =     { 2, 0, 0, 5 };
68    double upb[4]  =     { 6, 6, 7, 7 };
69 **/
70    // put numbers from above into the bounds arrays
71    // and load numbers into the data arrays
72 //   for( i = 0; i < 4; i++ )
73 //   {
74 //      data_d.append( tmp );
75 //      for( j = 0; j < 4; j++ )
76 //      {
77 //         data_d[i].append( num[i][j] );
78 //      }
79 //
80 //      data_sp.append( DDaceSamplePoint( 4, data_d[i] ) );
81 //
82 //      lb.append( lowb[i] );
83 //      ub.append( upb[i] );
84 //   }
85   double   pts[16][2] = { {12.5,12.5},  // correct data for nSamples=16, nSymbols=4, nInputs=2
86                           {37.5,12.5},
87                           {62.5,12.5},
88                           {87.5,12.5},
89                           {12.5,37.5},
90                           {37.5,37.5},
91                           {62.5,37.5},
92                           {87.5,37.5},
93                           {12.5,62.5},
94                           {37.5,62.5},
95                           {62.5,62.5},
96                           {87.5,62.5},
97                           {12.5,87.5},
98                           {37.5,87.5},
99                           {62.5,87.5},
100                           {87.5,87.5} };
101 	std::vector<double> temp(2);
102   for (int i=0; i<16; i++) {
103 	for(int j=0; j < 2; j++) {
104 		temp[j] = pts[i][j];
105 		}
106       	data_sp.push_back(DDaceSamplePoint(i, temp));
107 	}
108   lb.push_back(0);
109   lb.push_back(0);
110   ub.push_back(100);
111   ub.push_back(100);
112 
113 
114 
115 }
116 
~TestDDaceSampler()117 TestDDaceSampler::~TestDDaceSampler()
118 {
119 }
120 
run()121 void TestDDaceSampler::run()
122 {
123   testDDaceSampler();
124   testDDaceSamplerBase();
125   testGetSamples();
126   testPrint();
127   testTypeName();
128   testNSamples();
129   testNInputs();
130   testGetParameter();
131   testDist();
132   testLowerBounds();
133   testUpperBounds();
134   testNoise();
135 }
136 
testDDaceSampler()137 void TestDDaceSampler::testDDaceSampler()
138 {
139   // create a new DDaceSampler using the default constructor.
140   DDaceSampler default_data;
141 }
142 
testDDaceSamplerBase()143 void TestDDaceSampler::testDDaceSamplerBase()
144 {
145   //DDaceArraySampler ddaceArraySampler( data_d );
146   DistributionBase::setSeed(0);
147   std::vector<Distribution>       dists;
148   dists.resize( 0 );
149   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
150   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
151   DistributionBase::setSeed(0);
152   DDaceFactorialSampler sampler( 16, 4, true, dists );
153 
154 
155   // create a new DDaceSampler using the base constructor.
156   DDaceSampler base_data( sampler );
157 }
158 
testGetSamples()159 void TestDDaceSampler::testGetSamples()
160 {
161   // create a new DDaceSampler using the default constructor.
162   DDaceSampler default_data;
163 
164   //DDaceArraySampler ddaceArraySampler( data_d );
165   DistributionBase::setSeed(0);
166   std::vector<Distribution>       dists;
167   dists.resize( 0 );
168   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
169   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
170   DistributionBase::setSeed(0);
171   DDaceFactorialSampler sampler( 16, 4, false, dists );
172 
173   // create a new DDaceSampler using the base constructor.
174   DDaceSampler base_data( sampler );
175 
176   std::vector<DDaceSamplePoint> sp_pass( 0 );
177   std::vector<DDaceSamplePoint> sp_ret( 0 );
178 
179 
180   // call getSamples, should throw an exception
181   try {
182     //sp_ret = default_data.getSamples( sp_pass );
183     default_data.getSamples( sp_pass );
184     sp_ret = sp_pass;
185 
186     // if here then no exception was thrown, record
187     // a failure
188     _test( false );
189   } catch( std::exception& e ) {
190     // we're in the catch block so an exception was thrown
191     // record a success
192     _test( true );
193   }
194 
195   // call getSamples and compare the returned values
196   //sp_ret = base_data.getSamples( sp_pass );
197   base_data.getSamples( sp_pass );
198   sp_ret = sp_pass;
199 
200   _test( Arrcmp_sp( sp_ret, data_sp ) == 0 );
201   _test( Arrcmp_sp( sp_pass, data_sp ) == 0 );
202 }
203 
testPrint()204 void TestDDaceSampler::testPrint()
205 {
206   // create a new DDaceSampler using the default constructor.
207   DDaceSampler default_data;
208 
209   //DDaceArraySampler ddaceArraySampler( data_d );
210   DistributionBase::setSeed(0);
211   std::vector<Distribution>       dists;
212   dists.resize( 0 );
213   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
214   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
215   DistributionBase::setSeed(0);
216   DDaceFactorialSampler sampler( 16, 4, false, dists );
217 
218   // create a new DDaceSampler using the base constructor.
219   DDaceSampler base_data( sampler );
220 
221   char buf[1024];
222   string data_str;
223   string test_str( "<Factorial samples=\"16\" symbols=\"4\" perturb=\"false\" seed=\"0\"/>" );
224 
225   ifstream fin;
226   ofstream fout;
227   fout.open( "TestDDaceSampler_Log" );
228   if( !fout )
229   {
230      cerr << "Couldn't open TestDDaceSampler_Log" << endl;
231      return;
232   }
233 
234   try {
235     fout << default_data << endl;
236 
237      // if here then no exception was thrown so record a failure
238      _test( false );
239   } catch( std::exception& e ) {
240     // if here then exceptionw as thrown so record a success
241     _test( true );
242   }
243 
244   // write the data out then close the file
245   fout << base_data << endl;
246   fout.close();
247 
248   // re-open the file
249   fin.open( "TestDDaceSampler_Log" );
250   if( !fin )
251   {
252      cerr << "Couldn't open TestDDaceSampler_Log" << endl;
253      return;
254   }
255 
256   // read in the data
257   fin.getline( buf,1023 );
258   fin.close();
259   data_str = buf;
260 
261   // check if the data read in is what it is suppose to be
262   _test( data_str == test_str );
263 }
264 
testTypeName()265 void TestDDaceSampler::testTypeName()
266 {
267   // create a new DDaceSampler using the default constructor.
268   DDaceSampler default_data;
269 
270   //DDaceArraySampler ddaceArraySampler( data_d );
271   DistributionBase::setSeed(0);
272   std::vector<Distribution>       dists;
273   dists.resize( 0 );
274   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
275   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
276   DistributionBase::setSeed(0);
277   DDaceFactorialSampler sampler( 16, 4, false, dists );
278 
279   // create a new DDaceSampler using the base constructor.
280   DDaceSampler base_data( sampler );
281 
282   // call typeName and test the result
283 
284   try {  // for this case it should throw an exception
285     default_data.typeName();
286 
287     // if here then no exception was thrown, record a failure
288     _test( false );
289   } catch( std::exception& e ) {
290     // if here then exception was thrown, record a success
291     _test( true );
292   }
293 
294   _test( base_data.typeName() == "DDaceFactorialSampler" );
295 }
296 
testNSamples()297 void TestDDaceSampler::testNSamples()
298 {
299   // create a new DDaceSampler using the default constructor.
300   DDaceSampler default_data;
301 
302   //DDaceArraySampler ddaceArraySampler( data_d );
303   DistributionBase::setSeed(0);
304   std::vector<Distribution>       dists;
305   dists.resize( 0 );
306   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
307   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
308   DistributionBase::setSeed(0);
309   DDaceFactorialSampler sampler( 16, 4, false, dists );
310 
311   // create a new DDaceSampler using the base constructor.
312   DDaceSampler base_data( sampler );
313 
314   // call nSamples and test the result
315 
316   try {  // for this case it should throw an exception
317     default_data.nSamples();
318 
319     // if here then no exception was thrown, record a failure
320     _test( false );
321   } catch( std::exception& e ) {
322     // if here then exception was thrown, record a success
323     _test( true );
324   }
325 
326   _test( base_data.nSamples() == 16 );
327 }
328 
testNInputs()329 void TestDDaceSampler::testNInputs()
330 {
331   // create a new DDaceSampler using the default constructor.
332   DDaceSampler default_data;
333 
334   //DDaceArraySampler ddaceArraySampler( data_d );
335   DistributionBase::setSeed(0);
336   std::vector<Distribution>       dists;
337   dists.resize( 0 );
338   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
339   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
340   DistributionBase::setSeed(0);
341   DDaceFactorialSampler sampler( 16, 4, false, dists );
342 
343 
344   // create a new DDaceSampler using the base constructor.
345   DDaceSampler base_data( sampler );
346 
347   // call nInputs and test the result
348 
349   try {  // for this case it should throw an exception
350     default_data.nInputs();
351 
352     // if here then no exception was thrown, record a failure
353     _test( false );
354   } catch( std::exception& e ) {
355     // if here then exception was thrown, record a success
356     _test( true );
357   }
358 
359   _test( base_data.nInputs() == 2 );
360 }
361 
testGetParameter()362 void TestDDaceSampler::testGetParameter()
363 {
364   std::string s;
365 
366   // create a new DDaceSampler using the default constructor.
367   DDaceSampler default_data;
368 
369   //DDaceArraySampler ddaceArraySampler( data_d );
370   DistributionBase::setSeed(0);
371   std::vector<Distribution>       dists;
372   dists.resize( 0 );
373   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
374   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
375   DistributionBase::setSeed(0);
376   DDaceFactorialSampler sampler( 16, 4, false, dists );
377 
378   // create a new DDaceSampler using the base constructor.
379   DDaceSampler base_data( sampler );
380 
381   // call getParameter, it should throw an exception.
382   try {
383     default_data.getParameter( s );
384 
385     // if we're here then no exception was thrown so record a failure
386     _test( false );
387   } catch( std::exception& e ) {
388 
389     // since we're in the catch block an exception was thrown so
390     // record a success
391     _test( true );
392   }
393 
394   try {
395     base_data.getParameter( s );
396 
397     // if we're here then no exception was thrown so record a failure
398     _test( false );
399   } catch( std::exception& e ) {
400 
401     // since we're in the catch block an exception was thrown so
402     // record a success
403     _test( true );
404   }
405 }
406 
testDist()407 void TestDDaceSampler::testDist()
408 {
409   // create a new DDaceSampler using the default constructor.
410   DDaceSampler default_data;
411 
412   //DDaceArraySampler ddaceArraySampler( data_d );
413   DistributionBase::setSeed(0);
414   std::vector<Distribution>       dists;
415   dists.resize( 0 );
416   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
417   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
418   DistributionBase::setSeed(0);
419   DDaceFactorialSampler sampler( 16, 4, false, dists );
420 
421   // create a new DDaceSampler using the base constructor.
422   DDaceSampler base_data( sampler );
423 
424   // call dist, it should throw an exception.
425   try {
426     default_data.dist();
427 
428     // if we're here then no exception was thrown so record a failure
429     _test( false );
430   } catch( std::exception& e ) {
431 
432     // since we're in the catch block an exception was thrown so
433     // record a success
434     _test( true );
435   }
436 
437 //  try {
438 //    base_data.dist();
439 //
440 //    // if we're here then no exception was thrown so record a failure
441 //    _test( false );
442 //  } catch( ExceptionBase e ) {
443 //
444 //    // since we're in the catch block an exception was thrown so
445 //    // record a success
446 //    _test( true );
447 //  }
448 }
449 
testLowerBounds()450 void TestDDaceSampler::testLowerBounds()
451 {
452   // create a new DDaceSampler using the default constructor.
453   DDaceSampler default_data;
454 
455   //DDaceArraySampler ddaceArraySampler( data_d );
456   DistributionBase::setSeed(0);
457   std::vector<Distribution>       dists;
458   dists.resize( 0 );
459   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
460   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
461   DistributionBase::setSeed(0);
462   DDaceFactorialSampler sampler( 16, 4, false, dists );
463 
464   // create a new DDaceSampler using the base constructor.
465   DDaceSampler base_data( sampler );
466 
467    // call lowerBounds and compare the result
468 
469   try {  // for this case it should throw an exception
470     default_data.lowerBounds();
471 
472     // if here then no exception was thrown, record a failure
473     _test( false );
474   } catch( std::exception& e ) {
475     // if here then exception was thrown, record a success
476     _test( true );
477   }
478 
479 
480    _test( Arrcmp_d( base_data.lowerBounds(), lb ) == 0 );
481 }
482 
testUpperBounds()483 void TestDDaceSampler::testUpperBounds()
484 {
485   // create a new DDaceSampler using the default constructor.
486   DDaceSampler default_data;
487 
488   //DDaceArraySampler ddaceArraySampler( data_d );
489   DistributionBase::setSeed(0);
490   std::vector<Distribution>       dists;
491   dists.resize( 0 );
492   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
493   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
494   DistributionBase::setSeed(0);
495   DDaceFactorialSampler sampler( 16, 4, false, dists );
496 
497   // create a new DDaceSampler using the base constructor.
498   DDaceSampler base_data( sampler );
499 
500    // call lowerBounds and compare the result
501 
502   try {  // for this case it should throw an exception
503     default_data.upperBounds();
504 
505     // if here then no exception was thrown, record a failure
506     _test( false );
507   } catch( std::exception& e ) {
508     // if here then exception was thrown, record a success
509     _test( true );
510   }
511 
512    _test( Arrcmp_d( base_data.upperBounds(), ub ) == 0 );
513 }
514 
testNoise()515 void TestDDaceSampler::testNoise()
516 {
517   // create a new DDaceSampler using the default constructor.
518   DDaceSampler default_data;
519 
520   //DDaceArraySampler ddaceArraySampler( data_d );
521   DistributionBase::setSeed(0);
522   std::vector<Distribution>       dists;
523   dists.resize( 0 );
524   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
525   dists.push_back( Distribution( UniformDistribution( 0, 100 ) ) );
526   DistributionBase::setSeed(0);
527   DDaceFactorialSampler sampler( 16, 4, false, dists );
528 
529   // create a new DDaceSampler using the base constructor.
530   DDaceSampler base_data( sampler );
531 
532   // call noise, it should return false
533 
534   try {  // for this case it should throw an exception
535     default_data.noise();
536 
537     // if here then no exception was thrown, record a failure
538     _test( false );
539   } catch( std::exception& e ) {
540     // if here then exception was thrown, record a success
541     _test( true );
542   }
543 
544   _test( base_data.noise() == false );
545 }
546 
547