9 #include <fei_macros.hpp>
11 #include <test_utils/test_Utils.hpp>
13 #include <fei_ArrayUtils.hpp>
14 #include <fei_utils.hpp>
15 #include <fei_CommUtils.hpp>
16 #include <snl_fei_Utils.hpp>
17 #include <fei_Param.hpp>
18 #include <fei_ParameterSet.hpp>
19 #include <fei_SharedPtr.hpp>
23 #define fei_file "test_Utils.cpp"
24 #include <fei_ErrMacros.hpp>
26 test_Utils::test_Utils(MPI_Comm comm)
31 test_Utils::~test_Utils()
35 void test_Utils_binarySearch()
37 std::vector<int> intarray;
38 intarray.push_back(1);
39 intarray.push_back(2);
40 intarray.push_back(5);
41 intarray.push_back(6);
42 intarray.push_back(9);
47 FEI_COUT <<
"testing correctness of fei::binarySearch(int,int*,int,int)...";
51 if (offset != -1 || insertPoint != 0) {
52 throw std::runtime_error(
"fei::binarySearch test failed 1.");
58 throw std::runtime_error(
"fei::binarySearch test failed 2.");
63 if (offset != -1 || insertPoint != 2) {
64 throw std::runtime_error(
"fei::binarySearch test failed 3.");
69 if (offset != -1 || insertPoint != 2) {
70 throw std::runtime_error(
"fei::binarySearch test failed 4.");
76 throw std::runtime_error(
"fei::binarySearch test failed 5.");
81 if (offset != -1 || insertPoint != 4) {
82 throw std::runtime_error(
"fei::binarySearch test failed 6.");
87 if (offset != -1 || insertPoint != 5) {
88 throw std::runtime_error(
"fei::binarySearch test failed 7.");
91 FEI_COUT <<
"ok"<<FEI_ENDL;
94 int test_Utils::runtests()
97 test_Utils_binarySearch();
99 CHK_ERR( serialtest1() );
100 CHK_ERR( serialtest2() );
101 CHK_ERR( serialtest3() );
111 int test_Utils::serialtest1()
113 FEI_COUT <<
"testing snl_fei::leading_substring_length...";
115 static char string1[] =
"test ";
117 if (snl_fei::leading_substring_length(string1) != 4) {
121 static char string2[] =
"second test";
122 if (snl_fei::leading_substring_length(string2) != 6) {
126 static char string3[] =
"third test";
128 if (snl_fei::leading_substring_length(string3) != 5) {
132 FEI_COUT <<
"ok"<<FEI_ENDL;
137 int test_Utils::serialtest2()
139 FEI_COUT <<
"testing snl_fei::getDoubleParamValue...";
141 static char string1[] =
"DOUBLE1 1.0";
142 static char string2[] =
"DOUBLE2 1.0e+0";
143 static char string3[] =
"DOUBLE3 1.0E+0";
144 static char string4[] =
"DOUBLE4 1";
146 std::vector<char*> params;
147 params.push_back(string1);
148 params.push_back(string2);
149 params.push_back(string3);
150 params.push_back(string4);
154 CHK_ERR( snl_fei::getDoubleParamValue(
"DOUBLE1",
155 params.size(), ¶ms[0],d1));
156 CHK_ERR( snl_fei::getDoubleParamValue(
"DOUBLE2",
157 params.size(), ¶ms[0],d2));
158 CHK_ERR( snl_fei::getDoubleParamValue(
"DOUBLE3",
159 params.size(), ¶ms[0],d3));
160 CHK_ERR( snl_fei::getDoubleParamValue(
"DOUBLE4",
161 params.size(), ¶ms[0],d4));
163 if (std::abs(d1 - 1.0) > 1.e-49 || std::abs(d2 - 1.0) > 1.e-49 ||
164 std::abs(d3 - 1.0) > 1.e-49 || std::abs(d4 - 1.0) > 1.e-49) {
168 FEI_COUT <<
"ok"<<FEI_ENDL;
173 int test_Utils::serialtest3()
175 FEI_COUT <<
"testing fei::Param and fei::ParameterSet...";
177 fei::Param param1(
"string-param",
"garbage value");
181 if (param1.getType() != fei::Param::STRING) {
185 if (param2.getType() != fei::Param::DOUBLE) {
189 if (param3.getType() != fei::Param::INT) {
195 paramset.
add(param2);
196 paramset.
add(param3);
198 if (paramset.
size() != 3) {
203 iter = paramset.
begin(),
204 iter_end = paramset.
end();
207 for(; iter != iter_end; ++iter) {
214 if (paramset.
get(
"int-param") == NULL) {
228 std::string dummychars;
234 if (
"garbage value" != dummychars) {
246 std::vector<std::string> stdstrings;
249 tempstr =
"string-param garbage value";
250 stdstrings.push_back(tempstr);
252 tempstr =
"int-param 58";
253 stdstrings.push_back(tempstr);
255 tempstr =
"real-param 45.e-2";
256 stdstrings.push_back(tempstr);
262 if (
"garbage value" != dummychars) {
273 if (std::abs(ddummy - 45.e-2) > 1.e-49) {
277 FEI_COUT <<
"ok"<<FEI_ENDL;
282 void test_Utils_function_that_throws()
284 throw std::runtime_error(
"testing...");
287 int test_Utils::test1()
289 FEI_COUT <<
"testing std::runtime_error...";
291 bool exc_thrown_and_caught =
false;
294 test_Utils_function_that_throws();
296 catch(std::runtime_error& exc) {
297 std::string str(exc.what());
298 if (str ==
"testing...") {
299 exc_thrown_and_caught =
true;
303 if (!exc_thrown_and_caught) {
307 FEI_COUT <<
"ok"<<FEI_ENDL;
311 bool test_Utils_dummy_destroyed =
true;
313 class test_Utils_dummy {
315 test_Utils_dummy() {test_Utils_dummy_destroyed =
false;}
318 test_Utils_dummy_destroyed =
true;
322 int test_Utils_test_SharedPtr()
331 test_Utils_dummy_destroyed =
true;
333 if (test_Utils_dummy_destroyed ==
true)
return(-1);
337 int test_Utils::test2()
339 FEI_COUT <<
"testing fei::SharedPtr...";
340 int err = test_Utils_test_SharedPtr();
345 if (test_Utils_dummy_destroyed !=
true) {
349 FEI_COUT <<
"ok"<<FEI_ENDL;
353 int test_Utils::test3()
355 FEI_COUT <<
"testing snl_fei::copy2DToColumnContig...";
363 int len1 = numrows1*numcols1;
364 int len2 = numrows2*numcols2;
366 double** table2d_1 =
new double*[numrows1];
367 for(i=0; i<numrows1; ++i) {
368 table2d_1[i] =
new double[numcols1];
369 for(j=0; j<numcols1; ++j) {
370 table2d_1[i][j] = j*numrows1+i;
374 double** table2d_2 =
new double*[numcols2];
375 for(j=0; j<numcols2; ++j) {
376 table2d_2[j] =
new double[numrows2];
377 for(i=0; i<numrows2; ++i) {
378 table2d_2[j][i] = j*numrows2+i;
382 double* cc1 =
new double[len1];
383 double* cc2 =
new double[len2];
385 snl_fei::copy2DToColumnContig(numrows1, numcols1, table2d_1,
388 snl_fei::copy2DToColumnContig(numrows2, numcols2, table2d_2,
391 for(i=0; i<len1; ++i) {
392 if (std::abs(cc1[i] - cc2[i]) > 1.e-49) {
393 throw std::runtime_error(
"column-contig arrays not equal.");
397 for(j=0; j<numrows1; ++j)
delete [] table2d_1[j];
402 FEI_COUT <<
"ok"<<FEI_ENDL;
404 FEI_COUT <<
"testing snl_fei::copy2DBlockDiagToColumnContig...";
408 int* blockSizes =
new int[numBlocks];
409 for(i=0; i<numBlocks; ++i) {
413 table2d_1 =
new double*[numrows1];
414 for(i=0; i<numrows1; ++i) {
415 table2d_1[i] =
new double[4];
417 table2d_1[i][j] = 1.0*i*4+j;
422 cc1 =
new double[len1];
424 snl_fei::copy2DBlockDiagToColumnContig(numBlocks, blockSizes, table2d_1,
425 FEI_BLOCK_DIAGONAL_ROW, cc1);
427 for(i=0; i<len1; ++i) {
428 if (std::abs(1.0*i - cc1[i]) > 1.e-49) {
429 throw std::runtime_error(
"copy2DBlockDiagToColumnContig row test failed.");
433 for(j=0; j<numrows1; ++j)
delete [] table2d_1[j];
435 for(j=0; j<numcols2; ++j)
delete [] table2d_2[j];
439 delete [] blockSizes;
441 FEI_COUT <<
"ok"<<FEI_ENDL;
445 int test_Utils::test4()
const Param * get(const char *name) const
int getStringParamValue(const char *name, std::string ¶mValue) const
int binarySearch(const T &item, const T *list, int len)
void add(const Param ¶m, bool maintain_unique_keys=true)
int getDoubleParamValue(const char *name, double ¶mValue) const
void parse_strings(std::vector< std::string > &stdstrings, const char *separator_string, fei::ParameterSet ¶mset)
const_iterator end() const
const_iterator begin() const
int getIntParamValue(const char *name, int ¶mValue) const