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