1 #include "TestDDaceOALHSampler.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 ///////////////////////////
11 // Written By: J Cramp
12 // Date: July 2004
13 ///////////////////////////
14
15 using namespace std;
16
TestDDaceOALHSampler()17 TestDDaceOALHSampler::TestDDaceOALHSampler() : lb( 1 ), ub( 4 ), seed( 779 )
18 {
19
20 /* For unit tests, we do NOT want a random number generator. */
21 /* We want a number generator that pushes out a known sequence of numbers.*/
22 DistributionBase::usePseudoRandom(true);
23
24 // set the seed to ensure the data matches
25 DistributionBase::setSeed( seed );
26
27 // set up the getSamples test data
28 int i,j;
29 std::vector<double> dtmp( 0 );
30 std::vector<int> itmp( 0 );
31
32
33
34 double pts[4][2] = { {2.335, 2.335},
35 {1.58425,3.8365},
36 {3.08575,1.58425},
37 {3.8365,3.08575} };
38
39
40
41
42
43
44 int ipts[4][2] = { {2,2},
45 {1,4},
46 {3,1},
47 {4,3} };
48
49
50
51
52
53 int oapts[4][2] = { {0,0},
54 {0,1},
55 {1,0},
56 {1,1} };
57
58 test_data.resize( 0 );
59 test_design.resize( 0 );
60 test_oa.resize( 0 );
61
62 for( i = 0; i < 4; i++ )
63 {
64 test_data.push_back( dtmp );
65 test_design.push_back( itmp );
66 test_oa.push_back( itmp );
67 for( j = 0; j < 2; j++ )
68 {
69 test_data[i].push_back( pts[i][j] );
70 test_design[i].push_back( ipts[i][j] );
71 test_oa[i].push_back( oapts[i][j] );
72 }
73 }
74 }
75
~TestDDaceOALHSampler()76 TestDDaceOALHSampler::~TestDDaceOALHSampler()
77 {
78 /* Restore the Random Number Generator */
79 DistributionBase::usePseudoRandom(false);
80
81 // reseed the Random Number Generator
82 DistributionBase::setSeed( seed );
83 }
84
run()85 void TestDDaceOALHSampler::run()
86 {
87 testDDaceOALHSampler();
88 testGetSamples();
89 testClone();
90 testPrint();
91 testTypeName();
92 testGetParameter();
93 testGetDesign();
94 testGetOA();
95 }
96
testDDaceOALHSampler()97 void TestDDaceOALHSampler::testDDaceOALHSampler()
98 {
99 // use constructor DDaceOALHSampler( int nSamples, int nInputs,
100 // int Strength, int lower, int upper );
101 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
102
103 // check the lower bounds
104 _test( sampler.lowerBound() == lb );
105 _test( sampler.upperBound() == ub );
106 }
107
testGetSamples()108 void TestDDaceOALHSampler::testGetSamples()
109 {
110 int i,j;
111
112 std::vector<DDaceSamplePoint> pass;
113 std::vector<DDaceSamplePoint> ret;
114
115 DistributionBase::setSeed( seed );
116 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
117 DistributionBase::setSeed( seed );
118
119 // call getSamples() and test results
120 //ret = sampler.getSamples( pass );
121 sampler.getSamples( pass );
122 DistributionBase::setSeed( seed );
123 ret = pass;
124
125 if( _test( Arrcmp_ad_est( pass, test_data, 0.001 ) == 0 ) == false )
126 {
127 cout << "pass: " << endl;
128 for( i = 0; i < (int) pass.size(); i++ )
129 {
130 cout << "[";
131 for( j = 0; j < pass[i].length(); j++ )
132 {
133 cout << "(" << pass[i][j] << "|" << test_data[i][j] << ")";
134 if( j != pass[i].length()-1 ) cout << ", ";
135 }
136 cout << "]" << endl;
137 }
138 }
139
140 _test( Arrcmp_ad_est( ret, test_data, 0.001 ) == 0 );
141 }
142
testClone()143 void TestDDaceOALHSampler::testClone()
144 {
145 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
146
147 // call clone and test the results
148 DDaceOALHSampler* rtn = (DDaceOALHSampler*)sampler.clone();
149
150 // check internal values to see if clone() worked
151 _test( rtn->typeName() == sampler.typeName() );
152 _test( rtn->nSamples() == sampler.nSamples() );
153 _test( rtn->nSymbols() == sampler.nSymbols() );
154 _test( rtn->nInputs() == sampler.nInputs() );
155 _test( rtn->noise() == sampler.noise() );
156
157 if( _test( (rtn->dist()).size() == sampler.dist().size() ) )
158 {
159 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
160 {
161 _test( (rtn->dist())[i].lowerBound() == sampler.dist()[i].lowerBound() );
162 _test( (rtn->dist())[i].upperBound() == sampler.dist()[i].upperBound() );
163 }
164 }
165
166 _test( rtn->lowerBound() == sampler.lowerBound() );
167 _test( rtn->upperBound() == sampler.upperBound() );
168
169 // clean up the dynamic memory
170 delete rtn;
171 }
172
testPrint()173 void TestDDaceOALHSampler::testPrint()
174 {
175 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
176
177 char buf[256];
178 char num[16];
179 string data;
180 string test_str;
181
182 // convert seed from 'long' to 'char*'
183 itoa( DistributionBase::seed(), num, 16 );
184
185 test_str = "<OrthogonalArrayLatinHypercube samples=\"4\" inputs=\"2\"";
186 test_str += " symbols=\"2\" strength=\"2\" frequency=\"1\"";
187 test_str += " randomize=\"false\" seed=\"";
188 test_str += string( num );
189 test_str += "\"/>";
190
191 ifstream fin;
192 ofstream fout;
193
194 // write data to file then read the data
195 // back from the file to verify it out
196 // what it was suppose to
197
198 fout.open( "TestDDaceOALHSampler_Log" );
199 if( fout )
200 {
201 sampler.print( fout );
202 fout << endl;
203 fout.close();
204 }
205 else
206 {
207 _test( false );
208 }
209
210 fin.open( "TestDDaceOALHSampler_Log" );
211 if( fin )
212 {
213 fin.getline( buf, 255 );
214 data = buf;
215
216 _test( data == test_str );
217
218 fin.close();
219 }
220 else
221 {
222 _test( false );
223 }
224 }
225
testTypeName()226 void TestDDaceOALHSampler::testTypeName()
227 {
228 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub);
229
230 // call typeName and test the results
231 _test( sampler.typeName() == std::string( "DDaceOALHSampler" ) );
232 }
233
testGetParameter()234 void TestDDaceOALHSampler::testGetParameter()
235 {
236 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
237
238 _test( sampler.getParameter( "SAMPLES" ) == 4 );
239 _test( sampler.getParameter( "INPUTS" ) == 2 );
240 _test( sampler.getParameter( "SYMBOLS" ) == 2 );
241 _test( sampler.getParameter( "STRENGTH" ) == 2 );
242 _test( sampler.getParameter( "FREQUENCY" ) == 1 );
243 _test( sampler.getParameter( "RANDOMIZED" ) == 0 );
244
245 try {
246 sampler.getParameter( "WRONG_INPUT" );
247
248 // if here then no exception was thrown, record failure
249 _test( false );
250 } catch( std::exception& e ) {
251 // if here then exception was thrown, record success
252 _test( true );
253 }
254 }
255
testGetDesign()256 void TestDDaceOALHSampler::testGetDesign()
257 {
258 int i,j;
259 DistributionBase::setSeed( seed );
260 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
261 DistributionBase::setSeed( seed );
262 std::vector<std::vector<int> > ret = sampler.getDesign();
263 DistributionBase::setSeed( seed );
264
265 if( _test( Arrcmp_i( ret, test_design ) == 0 ) == false )
266 {
267 std::vector<std::vector<int> > oa = sampler.getOA();
268
269 cout << "Design: " << endl;
270 for( i = 0; i < (int) ret.size(); i++ )
271 {
272 cout << "[";
273 for( j = 0; j < (int) ret[i].size(); j++ )
274 {
275 cout << "(" << ret[i][j] << "|" << test_design[i][j] << ")";
276 if( j != (int) ret[i].size()-1 ) cout << ", ";
277 }
278 cout << "]" << endl;
279 }
280 cout << "OA: " << endl;
281 for( i = 0; i < (int) oa.size(); i++ )
282 {
283 cout << "[";
284 for( j = 0; j < (int) oa[i].size(); j++ )
285 {
286 cout << oa[i][j];
287 if( j != (int) oa[i].size()-1 ) cout << ", ";
288 }
289 cout << "]" << endl;
290 }
291 }
292 }
293
testGetOA()294 void TestDDaceOALHSampler::testGetOA()
295 {
296 int i,j;
297
298 // due to the some randomness in the
299 // OA generation this particular sampler
300 // object uses a different OA than the
301 // others created in the tests
302 int pts[4][2] = { {0,0},
303 {0,1},
304 {1,0},
305 {1,1} };
306
307 DDaceOALHSampler sampler( 4, 2, 2, false, lb, ub );
308
309 std::vector<std::vector<int> > oa( 4 );
310 for( i = 0; i < 4; i++ )
311 {
312 oa[i].resize( 2 );
313 for( j = 0; j < 2; j++ )
314 {
315 oa[i][j] = pts[i][j];
316 }
317 }
318
319 std::vector<std::vector<int> > ret = sampler.getOA();
320 if( _test( Arrcmp_i( ret, oa ) == 0 ) == false )
321 {
322 cout << "OA: " << endl;
323 for( i = 0; i < (int) ret.size(); i++ )
324 {
325 cout << "[";
326 for( j = 0; j < (int) ret[i].size(); j++ )
327 {
328 cout << "(" << ret[i][j] << "|" << oa[i][j] << ")";
329 if( j != (int) ret[i].size()-1 ) cout << ", ";
330 }
331 cout << "]" << endl;
332 }
333 }
334 }
335
336
337