1 #include "TestDDaceUserInputSampler.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 using namespace std;
12 
TestDDaceUserInputSampler()13 TestDDaceUserInputSampler::TestDDaceUserInputSampler()
14 {
15   // set up the getSamples test data
16   int i,j;
17   std::vector<double> tmp( 0 );
18   double   pts[3][3] = { {2, 5, 9},
19                          {8, 2, 4},
20                          {6, 6, 3} };
21 
22   test_data.resize( 0 );
23 
24   for( i = 0; i < 3; i++ )
25   {
26      test_data.push_back( tmp );
27      for( j = 0; j < 3; j++ )
28      {
29         test_data[i].push_back( pts[i][j] );
30      }
31   }
32 }
33 
~TestDDaceUserInputSampler()34 TestDDaceUserInputSampler::~TestDDaceUserInputSampler()
35 {
36 }
37 
run()38 void TestDDaceUserInputSampler::run()
39 {
40    testDDaceUserInputSampler();
41    testGetSamples();
42    testClone();
43    testPrint();
44    testTypeName();
45    testGetParameter();
46 }
47 
testDDaceUserInputSampler()48 void TestDDaceUserInputSampler::testDDaceUserInputSampler()
49 {
50    // call constructor DDaceUserInputSampler(const String& ptFilename);
51    DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
52 
53    _test( sampler.nSamples() == 3 );
54    _test( sampler.nInputs() == 3 );
55 }
56 
testGetSamples()57 void TestDDaceUserInputSampler::testGetSamples()
58 {
59   int i,j;
60 
61   DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
62 
63   std::vector<DDaceSamplePoint> pass( 0 );
64   std::vector<DDaceSamplePoint> ret( 0 );
65 
66   // call getSamples and test the results
67   //ret = sampler.getSamples( pass );
68   sampler.getSamples( pass );
69   ret = pass;
70 
71   if( _test( Arrcmp_ad_est( pass, test_data, 0.001) == 0 ) == false )
72   {
73     for( i = 0; i < 3; i++ )
74     {
75        cout << "[";
76        for( j = 0; j < 3; j++ )
77        {
78          cout << "(" << pass[i][j] << "|" << test_data[i][j] << "), ";
79        }
80        cout << "]" << endl;
81     }
82   }
83   _test( Arrcmp_ad_est( ret, test_data, 0.001 ) == 0 );
84 }
85 
testClone()86 void TestDDaceUserInputSampler::testClone()
87 {
88    DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
89    std::vector<double> a;
90    std::vector<double> b;
91 
92    // call clone and test the results
93    DDaceUserInputSampler* rtn = (DDaceUserInputSampler*)sampler.clone();
94 
95    // check internal values to see if clone() worked
96    _test( rtn->typeName() == sampler.typeName() );
97    _test( rtn->nSamples() == sampler.nSamples() );
98    _test( rtn->nInputs() == sampler.nInputs() );
99 
100    a = rtn->lowerBounds();
101    b = sampler.lowerBounds();
102    _test( Arrcmp_d( a, b ) == 0 );
103    a.resize( 0 );
104    b.resize( 0 );
105 
106    a = rtn->upperBounds();
107    b = sampler.upperBounds();
108    _test( Arrcmp_d( a, b ) == 0 );
109 
110    // clean up the dynamic memory
111    delete rtn;
112 }
113 
testPrint()114 void TestDDaceUserInputSampler::testPrint()
115 {
116   DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
117 
118   char    buf[256];
119   char    num[16];
120   string  data;
121   string  test_str;
122 
123   // convert seed from 'long' to 'char*'
124   itoa( sampler.nSamples(), num, 16 );
125 
126   test_str = "<UserInputSampler filename=\"TestDDaceUserInputSamplerData\" samples=\"";
127   test_str += string( num );
128   test_str += "\"/>";
129 
130   ifstream fin;
131   ofstream fout;
132 
133   // write data to file then read the data
134   // back from the file to verify it out
135   // what it was suppose to
136 
137   fout.open( "TestDDaceUserInputsampler_Log" );
138   if( fout )
139   {
140      sampler.print( fout );
141      fout << endl;
142      fout.close();
143   }
144   else
145   {
146      _test( false );
147   }
148 
149   fin.open( "TestDDaceUserInputsampler_Log" );
150   if( fin )
151   {
152      fin.getline( buf, 255 );
153      data = buf;
154 
155      _test( data == test_str );
156 
157      fin.close();
158   }
159   else
160   {
161      _test( false );
162   }
163 }
164 
testTypeName()165 void TestDDaceUserInputSampler::testTypeName()
166 {
167   DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
168 
169   // call typeName and test the results
170   _test( sampler.typeName() == std::string( "DDaceUserInputSampler" ) );
171 }
172 
testGetParameter()173 void TestDDaceUserInputSampler::testGetParameter()
174 {
175   DDaceUserInputSampler sampler( std::string( "TestDDaceUserInputSamplerData" ) );
176 
177   // call getParamter, should throw an exception
178   try {
179      sampler.getParameter( std::string( "INPUT" ) );
180 
181      // if here then exception was not thrown record failure
182      _test(false);
183   } catch( std::exception& e ) {
184     // if here then exception was thrown record success
185     _test( true );
186   }
187 }
188 
189