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