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