1 #include "TestDDaceCentralCompositeSampler.h"
2 //#include "ArgumentMisMatchException.h"
3 #include "arrcmp.h"
4 #include <stdlib.h>
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8
9 using namespace std;
10
TestDDaceCentralCompositeSampler()11 TestDDaceCentralCompositeSampler::TestDDaceCentralCompositeSampler()
12 {
13 // create distributions need by sampler
14 dists.resize( 0 );
15 dists.push_back( Distribution( UniformDistribution( 6, 7 ) ) );
16 dists.push_back( Distribution( UniformDistribution( 7, 9 ) ) );
17 dists.push_back( Distribution( UniformDistribution( 0, 5 ) ) );
18
19 // set up the getSamples test data
20 int i,j;
21 std::vector<double> tmp( 0 );
22 double pts[15][3] = { {6.5,8,2.5},
23 {6,8,2.5},
24 {7,8,2.5},
25 {6.5,7,2.5},
26 {6.5,9,2.5},
27 {6.5,8,0},
28 {6.5,8,5},
29 {6,7,0},
30 {7,7,0},
31 {6,9,0},
32 {7,9,0},
33 {6,7,5},
34 {7,7,5},
35 {6,9,5},
36 {7,9,5} };
37
38 test_data.resize( 0 );
39
40 for( i = 0; i < 15; i++ )
41 {
42 test_data.push_back( tmp );
43 for( j = 0; j < 3; j++ )
44 {
45 test_data[i].push_back( pts[i][j] );
46
47 }
48 }
49 }
50
~TestDDaceCentralCompositeSampler()51 TestDDaceCentralCompositeSampler::~TestDDaceCentralCompositeSampler()
52 {
53 }
54
run()55 void TestDDaceCentralCompositeSampler::run()
56 {
57 testDDaceCentralCompositeSampler();
58 testGetSamples();
59 testClone();
60 testPrint();
61 testTypeName();
62 }
63
testDDaceCentralCompositeSampler()64 void TestDDaceCentralCompositeSampler::testDDaceCentralCompositeSampler()
65 {
66 // create the sampler
67 try {
68 DDaceCentralCompositeSampler sampler( 15, 3, dists );
69
70 // if here then no exception was thrown, record success
71 _test( true );
72 _test( sampler.nSamples() == 15 );
73 _test( sampler.nInputs() == 3 );
74 _test( sampler.dist().size() == 3 );
75
76 for( int i = 0; i < (int) sampler.dist().size(); i++ )
77 {
78 _test( dists[i].lowerBound() == sampler.dist()[i].lowerBound() );
79 _test( dists[i].upperBound() == sampler.dist()[i].upperBound() );
80 }
81
82 } catch( std::exception& e ) {
83 e.what();
84 // if here then an exception was thrown, record failure
85 _test( false );
86 }
87 catch (...) {
88 _test(false);
89 }
90 }
91
testGetSamples()92 void TestDDaceCentralCompositeSampler::testGetSamples()
93 {
94 DDaceCentralCompositeSampler sampler( 15, 3, dists );
95 std::vector<DDaceSamplePoint> pass( 0 );
96 std::vector<DDaceSamplePoint> ret( 0 );
97
98 // call getSamples and test the results
99 //ret = sampler.getSamples( pass );
100 sampler.getSamples( pass );
101 ret = pass;
102
103 _test( Arrcmp_ad( pass, test_data ) == 0 );
104 _test( Arrcmp_ad( ret, test_data ) == 0 );
105 }
106
testClone()107 void TestDDaceCentralCompositeSampler::testClone()
108 {
109 DDaceCentralCompositeSampler sampler( 15, 3, dists );
110 std::vector<double> a;
111 std::vector<double> b;
112
113 // call clone and test the results
114 DDaceCentralCompositeSampler* rtn = (DDaceCentralCompositeSampler*)sampler.clone();
115
116 // check internal values to see if clone() worked
117 _test( rtn->typeName() == sampler.typeName() );
118 _test( rtn->nSamples() == sampler.nSamples() );
119 _test( rtn->nInputs() == sampler.nInputs() );
120 _test( rtn->noise() == sampler.noise() );
121
122 if( _test( (rtn->dist()).size() == sampler.dist().size() ) )
123 {
124 for( int i = 0; i < (int) (rtn->dist()).size(); i++ )
125 {
126 _test( (rtn->dist())[i].lowerBound() == sampler.dist()[i].lowerBound() );
127 _test( (rtn->dist())[i].upperBound() == sampler.dist()[i].upperBound() );
128 }
129 }
130
131
132 a = rtn->lowerBounds();
133 b = sampler.lowerBounds();
134 _test( Arrcmp_d( a, b ) == 0 );
135 a.resize( 0 );
136 b.resize( 0 );
137
138 a = rtn->upperBounds();
139 b = sampler.upperBounds();
140 _test( Arrcmp_d( a, b ) == 0 );
141
142 // clean up the dynamic memory
143 delete rtn;
144 }
145
testPrint()146 void TestDDaceCentralCompositeSampler::testPrint()
147 {
148 DDaceCentralCompositeSampler sampler( 15, 3, dists );
149 char buf[256];
150 string data;
151 string test_str[2];
152
153 test_str[0] = "METHOD Central Composite Design";
154 test_str[1] = "SAMPLES 15";
155
156 ifstream fin;
157 ofstream fout;
158
159 // write data to file then read the data
160 // back from the file to verify it out
161 // what it was suppose to
162
163 fout.open( "TestDDaceCentralCompositeSampler_Log" );
164 if( fout )
165 {
166 sampler.print( fout );
167 fout << endl;
168 fout.close();
169 }
170 else
171 {
172 _test( false );
173 }
174
175 fin.open( "TestDDaceCentralCompositeSampler_Log" );
176 if( fin )
177 {
178 for( int i = 0; i < 2; i++ )
179 {
180 fin.getline( buf, 255 );
181 data = buf;
182
183 _test( data == test_str[i] );
184 }
185
186 fin.close();
187 }
188 else
189 {
190 _test( false );
191 }
192 }
193
testTypeName()194 void TestDDaceCentralCompositeSampler::testTypeName()
195 {
196 DDaceCentralCompositeSampler sampler( 15, 3, dists );
197
198 // call type name and check the results
199 _test( sampler.typeName() == "DDaceCentralCompositeSampler" );
200 }
201
202