test_Utils.cpp

00001 /*--------------------------------------------------------------------*/
00002 /*    Copyright 2005 Sandia Corporation.                              */
00003 /*    Under the terms of Contract DE-AC04-94AL85000, there is a       */
00004 /*    non-exclusive license for use of this work by or on behalf      */
00005 /*    of the U.S. Government.  Export of this program may require     */
00006 /*    a license from the United States Government.                    */
00007 /*--------------------------------------------------------------------*/
00008 
00009 #include <fei_macros.hpp>
00010 
00011 #include <test_utils/test_Utils.hpp>
00012 
00013 #include <fei_ArrayUtils.hpp>
00014 #include <fei_utils.hpp>
00015 #include <fei_CommUtils.hpp>
00016 #include <snl_fei_Utils.hpp>
00017 #include <fei_Param.hpp>
00018 #include <fei_ParameterSet.hpp>
00019 #include <fei_SharedPtr.hpp>
00020 #include <cmath>
00021 
00022 #undef fei_file
00023 #define fei_file "test_Utils.cpp"
00024 #include <fei_ErrMacros.hpp>
00025 
00026 test_Utils::test_Utils(MPI_Comm comm)
00027   : tester(comm)
00028 {
00029 }
00030 
00031 test_Utils::~test_Utils()
00032 {
00033 }
00034 
00035 void test_Utils_binarySearch()
00036 {
00037   std::vector<int> intarray;
00038   intarray.push_back(1);
00039   intarray.push_back(2);
00040   intarray.push_back(5);
00041   intarray.push_back(6);
00042   intarray.push_back(9);
00043 
00044   int offset = 0;
00045   int insertPoint = -1;
00046 
00047   FEI_COUT << "testing correctness of fei::binarySearch(int,int*,int,int)...";
00048 
00049   offset = fei::binarySearch(0, &intarray[0], intarray.size(),
00050          insertPoint);
00051   if (offset != -1 || insertPoint != 0) {
00052     throw std::runtime_error("fei::binarySearch test failed 1.");
00053   }
00054 
00055   offset = fei::binarySearch(2, &intarray[0], intarray.size(),
00056          insertPoint);
00057   if (offset != 1) {
00058     throw std::runtime_error("fei::binarySearch test failed 2.");
00059   }
00060 
00061   offset = fei::binarySearch(3, &intarray[0], intarray.size(),
00062          insertPoint);
00063   if (offset != -1 || insertPoint != 2) {
00064     throw std::runtime_error("fei::binarySearch test failed 3.");
00065   }
00066 
00067   offset = fei::binarySearch(4, &intarray[0], intarray.size(),
00068          insertPoint);
00069   if (offset != -1 || insertPoint != 2) {
00070     throw std::runtime_error("fei::binarySearch test failed 4.");
00071   }
00072 
00073   offset = fei::binarySearch(9, &intarray[0], intarray.size(),
00074          insertPoint);
00075   if (offset != 4) {
00076     throw std::runtime_error("fei::binarySearch test failed 5.");
00077   }
00078 
00079   offset = fei::binarySearch(8, &intarray[0], intarray.size(),
00080          insertPoint);
00081   if (offset != -1 || insertPoint != 4) {
00082     throw std::runtime_error("fei::binarySearch test failed 6.");
00083   }
00084 
00085   offset = fei::binarySearch(10, &intarray[0], intarray.size(),
00086          insertPoint);
00087   if (offset != -1 || insertPoint != 5) {
00088     throw std::runtime_error("fei::binarySearch test failed 7.");
00089   }
00090 
00091   FEI_COUT << "ok"<<FEI_ENDL;
00092 }
00093 
00094 int test_Utils::runtests()
00095 {
00096   if (numProcs_ < 2) {
00097     test_Utils_binarySearch();
00098 
00099     CHK_ERR( serialtest1() );
00100     CHK_ERR( serialtest2() );
00101     CHK_ERR( serialtest3() );
00102   }
00103 
00104   CHK_ERR( test1() );
00105   CHK_ERR( test2() );
00106   CHK_ERR( test3() );
00107   CHK_ERR( test4() );
00108   return(0);
00109 }
00110 
00111 int test_Utils::serialtest1()
00112 {
00113   FEI_COUT << "testing snl_fei::leading_substring_length...";
00114 
00115   static char string1[] = "test ";
00116   string1[4] = '\0';
00117   if (snl_fei::leading_substring_length(string1) != 4) {
00118     ERReturn(-1);
00119   }
00120 
00121   static char string2[] = "second test";
00122   if (snl_fei::leading_substring_length(string2) != 6) {
00123     ERReturn(-1);
00124   }
00125 
00126   static char string3[] = "third test";
00127   string3[5] = '\t';
00128   if (snl_fei::leading_substring_length(string3) != 5) {
00129     ERReturn(-1);
00130   }
00131 
00132   FEI_COUT << "ok"<<FEI_ENDL;
00133 
00134   return(0);
00135 }
00136 
00137 int test_Utils::serialtest2()
00138 {
00139   FEI_COUT << "testing snl_fei::getDoubleParamValue...";
00140 
00141   static char string1[] = "DOUBLE1 1.0";
00142   static char string2[] = "DOUBLE2 1.0e+0";
00143   static char string3[] = "DOUBLE3 1.0E+0";
00144   static char string4[] = "DOUBLE4 1";
00145 
00146   std::vector<char*> params;
00147   params.push_back(string1);
00148   params.push_back(string2);
00149   params.push_back(string3);
00150   params.push_back(string4);
00151 
00152   double d1,d2,d3,d4;
00153 
00154   CHK_ERR( snl_fei::getDoubleParamValue("DOUBLE1",
00155           params.size(), &params[0],d1));
00156   CHK_ERR( snl_fei::getDoubleParamValue("DOUBLE2",
00157           params.size(), &params[0],d2));
00158   CHK_ERR( snl_fei::getDoubleParamValue("DOUBLE3",
00159           params.size(), &params[0],d3));
00160   CHK_ERR( snl_fei::getDoubleParamValue("DOUBLE4",
00161           params.size(), &params[0],d4));
00162 
00163   if (std::abs(d1 - 1.0) > 1.e-49 || std::abs(d2 - 1.0) > 1.e-49 ||
00164       std::abs(d3 - 1.0) > 1.e-49 || std::abs(d4 - 1.0) > 1.e-49) {
00165     ERReturn(-1);
00166   }
00167 
00168   FEI_COUT <<"ok"<<FEI_ENDL;
00169 
00170   return(0);
00171 }
00172 
00173 int test_Utils::serialtest3()
00174 {
00175   FEI_COUT << "testing fei::Param and fei::ParameterSet...";
00176 
00177   fei::Param param1("string-param", "garbage value");
00178   fei::Param param2("double-param", 2.5);
00179   fei::Param param3("int-param", 1);
00180 
00181   if (param1.getType() != fei::Param::STRING) {
00182     ERReturn(-1);
00183   }
00184 
00185   if (param2.getType() != fei::Param::DOUBLE) {
00186     ERReturn(-1);
00187   }
00188 
00189   if (param3.getType() != fei::Param::INT) {
00190     ERReturn(-1);
00191   }
00192 
00193   fei::ParameterSet paramset;
00194   paramset.add(fei::Param("string-param", "garbage value"));
00195   paramset.add(param2);
00196   paramset.add(param3);
00197 
00198   if (paramset.size() != 3) {
00199     ERReturn(-1);
00200   }
00201 
00202   fei::ParameterSet::const_iterator
00203     iter = paramset.begin(),
00204     iter_end = paramset.end();
00205 
00206   int i=0;
00207   for(; iter != iter_end; ++iter) {
00208     if (i==3) {
00209       ERReturn(-1);
00210     }
00211     ++i;
00212   }
00213  
00214   if (paramset.get("int-param") == NULL) {
00215     ERReturn(-1);
00216   }
00217 
00218   int dummy;
00219   int err = paramset.getIntParamValue("int-param", dummy);
00220   if (err != 0) {
00221     ERReturn(-1);
00222   }
00223 
00224   if (dummy != 1) {
00225     ERReturn(-1);
00226   }
00227 
00228   std::string dummychars;
00229   err = paramset.getStringParamValue("string-param", dummychars);
00230   if (err != 0) {
00231     ERReturn(-1);
00232   }
00233 
00234   if ("garbage value" != dummychars) {
00235     ERReturn(-1);
00236   }
00237 
00238   //if (!snl_fei::leadingSubstring("garbage-value", "garbage")) {
00239   //  ERReturn(-1);
00240   //}
00241 
00242   //if (snl_fei::leadingSubstring("garb-value", "garbage")) {
00243   //  ERReturn(-1);
00244   //}
00245 
00246   std::vector<std::string> stdstrings;
00247   std::string tempstr;
00248 
00249   tempstr = "string-param garbage value";
00250   stdstrings.push_back(tempstr);
00251 
00252   tempstr = "int-param 58";
00253   stdstrings.push_back(tempstr);
00254 
00255   tempstr = "real-param 45.e-2";
00256   stdstrings.push_back(tempstr);
00257 
00258   fei::ParameterSet pset;
00259   fei::utils::parse_strings(stdstrings, " ", pset);
00260 
00261   err = pset.getStringParamValue("string-param", dummychars);
00262   if ("garbage value" != dummychars) {
00263     ERReturn(-1);
00264   }
00265 
00266   err = pset.getIntParamValue("int-param", dummy);
00267   if (dummy != 58) {
00268     ERReturn(-1);
00269   }
00270 
00271   double ddummy;
00272   err = pset.getDoubleParamValue("real-param", ddummy);
00273   if (std::abs(ddummy - 45.e-2) > 1.e-49) {
00274     ERReturn(-1);
00275   }
00276 
00277   FEI_COUT << "ok"<<FEI_ENDL;
00278 
00279   return(0);
00280 }
00281 
00282 void test_Utils_function_that_throws()
00283 {
00284   throw std::runtime_error("testing...");
00285 }
00286 
00287 int test_Utils::test1()
00288 {
00289   FEI_COUT << "testing std::runtime_error...";
00290 
00291   bool exc_thrown_and_caught = false;
00292 
00293   try {
00294     test_Utils_function_that_throws();
00295   }
00296   catch(std::runtime_error& exc) {
00297     std::string str(exc.what());
00298     if (str == "testing...") {
00299       exc_thrown_and_caught = true;
00300     }
00301   }
00302 
00303   if (!exc_thrown_and_caught) {
00304     ERReturn(-1);
00305   }
00306 
00307   FEI_COUT << "ok"<<FEI_ENDL;
00308   return(0);
00309 }
00310 
00311 bool test_Utils_dummy_destroyed = true;
00312 
00313 class test_Utils_dummy {
00314 public:
00315   test_Utils_dummy() {test_Utils_dummy_destroyed = false;}
00316   ~test_Utils_dummy()
00317   {
00318     test_Utils_dummy_destroyed = true;
00319   }
00320 };
00321 
00322 int test_Utils_test_SharedPtr()
00323 {
00324   //In this function, make sure the global bool is set to true, then create
00325   //the fei::SharedPtr and make sure that the global bool has been set to false.
00326   //If so, return 0, otherwise return -1.
00327   //When we return, the SharedPtr goes out of scope which should destroy the
00328   //test-dummy and cause the global bool to get set back to true. The code 
00329   //that's calling this function will verify that.
00330 
00331   test_Utils_dummy_destroyed = true;
00332   fei::SharedPtr<test_Utils_dummy> ptr(new test_Utils_dummy);
00333   if (test_Utils_dummy_destroyed == true) return(-1);
00334   else return(0);
00335 }
00336 
00337 int test_Utils::test2()
00338 {
00339   FEI_COUT << "testing fei::SharedPtr...";
00340   int err = test_Utils_test_SharedPtr();
00341   if (err != 0) {
00342     ERReturn(-1);
00343   }
00344 
00345   if (test_Utils_dummy_destroyed != true) {
00346     ERReturn(-1);
00347   }
00348 
00349   FEI_COUT << "ok"<<FEI_ENDL;
00350  return(0);
00351 }
00352 
00353 int test_Utils::test3()
00354 {
00355   FEI_COUT << "testing snl_fei::copy2DToColumnContig...";
00356 
00357   int numrows1 = 3;
00358   int numcols1 = 4;
00359   int numrows2 = 4;
00360   int numcols2 = 3;
00361 
00362   int i, j;
00363   int len1 = numrows1*numcols1;
00364   int len2 = numrows2*numcols2;
00365 
00366   double** table2d_1 = new double*[numrows1];
00367   for(i=0; i<numrows1; ++i) {
00368     table2d_1[i] = new double[numcols1];
00369     for(j=0; j<numcols1; ++j) {
00370       table2d_1[i][j] = j*numrows1+i;
00371     }
00372   }
00373 
00374   double** table2d_2 = new double*[numcols2];
00375   for(j=0; j<numcols2; ++j) {
00376     table2d_2[j] = new double[numrows2];
00377     for(i=0; i<numrows2; ++i) {
00378       table2d_2[j][i] = j*numrows2+i;
00379     }
00380   }
00381 
00382   double* cc1 = new double[len1];
00383   double* cc2 = new double[len2];
00384 
00385   snl_fei::copy2DToColumnContig(numrows1, numcols1, table2d_1,
00386         FEI_DENSE_ROW, cc1);
00387 
00388   snl_fei::copy2DToColumnContig(numrows2, numcols2, table2d_2,
00389         FEI_DENSE_COL, cc2);
00390 
00391   for(i=0; i<len1; ++i) {
00392     if (std::abs(cc1[i] - cc2[i]) > 1.e-49) {
00393       throw std::runtime_error("column-contig arrays not equal.");
00394     }
00395   }
00396 
00397   for(j=0; j<numrows1; ++j) delete [] table2d_1[j];
00398   delete [] table2d_1;
00399   delete [] cc1;
00400   delete [] cc2;
00401 
00402   FEI_COUT << "ok"<<FEI_ENDL;
00403 
00404   FEI_COUT << "testing snl_fei::copy2DBlockDiagToColumnContig...";
00405 
00406   numrows1 = 12;
00407   int numBlocks = 3;
00408   int* blockSizes = new int[numBlocks];
00409   for(i=0; i<numBlocks; ++i) {
00410     blockSizes[i] = 4;
00411   }
00412 
00413   table2d_1 = new double*[numrows1];
00414   for(i=0; i<numrows1; ++i) {
00415     table2d_1[i] = new double[4];
00416     for(j=0; j<4; ++j) {
00417       table2d_1[i][j] = 1.0*i*4+j;
00418     }
00419   }
00420 
00421   len1 = numrows1*4;
00422   cc1 = new double[len1];
00423 
00424   snl_fei::copy2DBlockDiagToColumnContig(numBlocks, blockSizes, table2d_1,
00425            FEI_BLOCK_DIAGONAL_ROW, cc1);
00426 
00427   for(i=0; i<len1; ++i) {
00428     if (std::abs(1.0*i - cc1[i]) > 1.e-49) {
00429       throw std::runtime_error("copy2DBlockDiagToColumnContig row test failed.");
00430     }
00431   }
00432 
00433   for(j=0; j<numrows1; ++j) delete [] table2d_1[j];
00434   delete [] table2d_1;
00435   for(j=0; j<numcols2; ++j) delete [] table2d_2[j];
00436   delete [] table2d_2;
00437 
00438   delete [] cc1;
00439   delete [] blockSizes;
00440 
00441   FEI_COUT << "ok"<<FEI_ENDL;
00442   return(0);
00443 }
00444 
00445 int test_Utils::test4()
00446 {
00447   return(0);
00448 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
Generated on Wed Apr 13 10:08:25 2011 for FEI by  doxygen 1.6.3