FEI Version of the Day
test_misc.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/fei_test_utils.hpp>
00012 
00013 #include <test_utils/test_misc.hpp>
00014 
00015 #include <test_utils/test_Factory_helper.hpp>
00016 
00017 #include <fei_FieldMask.hpp>
00018 #include <snl_fei_RecordCollection.hpp>
00019 
00020 #include <fei_VectorSpace.hpp>
00021 #include <fei_Vector_Impl.hpp>
00022 
00023 #undef fei_file
00024 #define fei_file "test_misc.cpp"
00025 #include <fei_ErrMacros.hpp>
00026 
00027 test_misc::test_misc(MPI_Comm comm)
00028   : tester(comm)
00029 {
00030 }
00031 
00032 test_misc::~test_misc()
00033 {
00034 }
00035 
00036 void test_misc_FieldMask()
00037 {
00038   FEI_COUT << "testing fei::FieldMask...";
00039 
00040   //A general test of fei::FieldMask.
00041 
00042   unsigned numFields = 5;
00043   std::vector<int> fieldIDs(numFields);
00044   std::vector<int> fieldSizes(numFields);
00045   int checkNumIndices = 0;
00046   for(unsigned i=0; i<numFields; ++i) {
00047     fieldIDs[i] = i;
00048     fieldSizes[i] = i;
00049     checkNumIndices += i;
00050   }
00051 
00052   fei::FieldMask fieldMask;
00053 
00054   for(int i=fieldIDs.size()-1; i>= 0; --i) {
00055     fieldMask.addField(fieldIDs[i], fieldSizes[i]);
00056   }
00057 
00058   std::vector<int>& maskFields = fieldMask.getFieldIDs();
00059   std::vector<int>& maskFieldSizes = fieldMask.getFieldSizes();
00060 
00061   if (maskFields != fieldIDs) {
00062     throw std::runtime_error("FieldMask test failed.");
00063   }
00064 
00065   if (maskFieldSizes != fieldSizes) {
00066     throw std::runtime_error("FieldMask size test failed.");
00067   }
00068 
00069   int checkOffset = 0;
00070   for(unsigned j=0; j<fieldIDs.size(); ++j) {
00071     int offset = -1;
00072     fieldMask.getFieldEqnOffset(fieldIDs[j], offset);
00073     if (offset != checkOffset) {
00074       throw std::runtime_error("FieldMask offset test failed.");
00075     }
00076     checkOffset += j;
00077   }
00078 
00079   int numIndices = fieldMask.getNumIndices();
00080   if (numIndices != checkNumIndices) {
00081     throw std::runtime_error("FieldMask numIndices test failed.");
00082   }
00083 
00084   bool exc_caught = false;
00085   try {
00086     fieldMask.addField(-1, 0);
00087   }
00088   catch (...) {
00089     exc_caught = true;
00090   }
00091 
00092   if (!exc_caught) {
00093     throw std::runtime_error("FieldMask failed to throw on negative fieldID.");
00094   }
00095 
00096   fieldMask.addField(2, 2);
00097 
00098   if (fieldMask.getNumFields() != numFields) {
00099     throw std::runtime_error("FieldMask getNumFields test failed.");
00100   }
00101 
00102   int fieldid1 = 0;
00103   int fieldid2 = 1;
00104   int fieldid3 = 2;
00105   int fieldsize = 1;
00106 
00107   fei::FieldMask fm1(1, &fieldid1, &fieldsize);
00108   fei::FieldMask fm2(1, &fieldid2, &fieldsize);
00109   fei::FieldMask fm3(1, &fieldid3, &fieldsize);
00110 
00111   fei::FieldMask fm12(fm1);
00112   fm12.addField(fieldid2, fieldsize);
00113 
00114   fei::FieldMask fm123(fm1);
00115   fm123.addField(fieldid2, fieldsize);
00116   fm123.addField(fieldid3, fieldsize);
00117 
00118   if (fm1.getMaskID() == fm2.getMaskID()) {
00119     throw std::runtime_error("FieldMask getMaskID test failed.");
00120   }
00121 
00122   if (fm2.getMaskID() == fm12.getMaskID()) {
00123     throw std::runtime_error("FieldMask getMaskID2 test failed.");
00124   }
00125 
00126   if (fm12.getMaskID() !=
00127       fei::FieldMask::calculateMaskID(fm1, fieldid2)){
00128     throw std::runtime_error("FieldMask getMaskID3 test failed.");
00129   }
00130 
00131   if (fm12.getMaskID() == fm3.getMaskID()) {
00132     throw std::runtime_error("FieldMask getMaskID4 test failed.");
00133   }
00134 
00135   if (fm123.getMaskID() != 
00136       fei::FieldMask::calculateMaskID(fm12, fieldid3)){
00137     throw std::runtime_error("FieldMask getMaskID5 test failed.");
00138   }
00139 
00140   if (fm3.getMaskID() == fm123.getMaskID()) {
00141     throw std::runtime_error("FieldMask getMaskID6 test failed.");
00142   }
00143 
00144   FEI_COUT << "ok"<<FEI_ENDL;
00145 }
00146 
00147 void test_misc_RecordCollection()
00148 {
00149   FEI_COUT << "testing snl_fei::RecordCollection...";
00150 
00151   int fieldID0 = 0;
00152   int fieldID1 = 1;
00153   int fieldID2 = 2;
00154   int fieldSize = 1;
00155   int ID0 = 0;
00156   int ID1 = 1;
00157 
00158   std::vector<fei::FieldMask*> fieldMasks;
00159 
00160   snl_fei::RecordCollection recColl(0);
00161 
00162   int* records = new int[1];
00163 
00164   recColl.initRecords(fieldID0, fieldSize, 1, &ID0,
00165           fieldMasks, records);
00166 
00167   recColl.initRecords(fieldID1, fieldSize, 1, &ID0,
00168           fieldMasks, records);
00169 
00170   recColl.initRecords(fieldID2, fieldSize, 1, &ID0,
00171           fieldMasks, records);
00172 
00173   recColl.initRecords(fieldID0, fieldSize, 1, &ID1,
00174           fieldMasks, records);
00175 
00176   recColl.initRecords(fieldID1, fieldSize, 1, &ID1,
00177           fieldMasks, records);
00178 
00179   recColl.initRecords(fieldID2, fieldSize, 1, &ID1,
00180           fieldMasks, records);
00181 
00182   if (fieldMasks.size() != 5) {
00183     throw std::runtime_error("RecordCollection fieldMasks.length test failed.");
00184   }
00185 
00186   std::vector<fei::Record<int> >& rvec = recColl.getRecords();
00187 
00188   std::vector<fei::Record<int> >::iterator
00189     r_iter = rvec.begin(),
00190     r_end = rvec.end();
00191 
00192   int numIndices = 0;
00193   for(; r_iter != r_end; ++r_iter) {
00194     numIndices += (*r_iter).getFieldMask()->getNumIndices();
00195   }
00196 
00197   if (numIndices != 6) {
00198     throw std::runtime_error("RecordCollection numIndices test failed.");
00199   }
00200 
00201   delete [] records;
00202   for(unsigned i=0; i<fieldMasks.size(); ++i) delete fieldMasks[i];
00203 
00204   FEI_COUT << "ok"<<FEI_ENDL;
00205 }
00206 
00207 int test_misc::runtests()
00208 {
00209   if (numProcs_ < 2) {
00210     test_misc_FieldMask();
00211     test_misc_RecordCollection();
00212 
00213     CHK_ERR( serialtest1() );
00214     CHK_ERR( serialtest2() );
00215     CHK_ERR( serialtest3() );
00216   }
00217 
00218   CHK_ERR( test1() );
00219   CHK_ERR( test2() );
00220   CHK_ERR( test3() );
00221   CHK_ERR( test4() );
00222   return(0);
00223 }
00224 
00225 int test_misc::serialtest1()
00226 {
00227   FEI_COUT << "testing fei_test_utils::within_percentage_margin...";
00228   double value1 = 65000.0;
00229   double value2 = 6500.0;
00230   bool result = fei_test_utils::within_percentage_margin(value1, value2, 10);
00231   if (result == true) {
00232     ERReturn(-1);
00233   }
00234 
00235   value1 = 6500.0;
00236   value2 = 6500.1;
00237   result = fei_test_utils::within_percentage_margin(value1,value2,1);
00238   if (result != true) {
00239     ERReturn(-1);
00240   }
00241 
00242   value1 = -10.0;
00243   value2 = 0.0;
00244   result = fei_test_utils::within_percentage_margin(value1,value2,30);
00245   if (result == true) {
00246     ERReturn(-1);
00247   }
00248 
00249   value1 = -1.e-18;
00250   value2 = 1.e-15;
00251   result = fei_test_utils::within_percentage_margin(value1,value2,10);
00252   if (result != true) {
00253     ERReturn(-1);
00254   }
00255 
00256   FEI_COUT << "ok"<<FEI_ENDL;
00257   return(0);
00258 }
00259 
00260 int test_misc::serialtest2()
00261 {
00262   FEI_COUT << "testing fei::lowerBound...";
00263   std::vector<int> list(5);
00264 
00265   list[0] = 1;
00266   list[1] = 4;
00267   list[2] = 6;
00268   list[3] = 7;
00269   list[4] = 11;
00270 
00271   int item = 0;
00272   int lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00273 
00274   if (lowerbound != 0) {
00275     throw std::runtime_error("failed test 1");
00276   }
00277 
00278   item = 1;
00279   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00280 
00281   if (lowerbound != 0) {
00282     throw std::runtime_error("failed test 2");
00283   }
00284 
00285   item = 2;
00286   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00287 
00288   if (lowerbound != 1) {
00289     throw std::runtime_error("failed test 3");
00290   }
00291 
00292   item = 7;
00293   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00294 
00295   if (lowerbound != 3) {
00296     throw std::runtime_error("failed test 4");
00297   }
00298 
00299   item = 9;
00300   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00301 
00302   if (lowerbound != 4) {
00303     throw std::runtime_error("failed test 5");
00304   }
00305 
00306   item = 11;
00307   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00308 
00309   if (lowerbound != 4) {
00310     throw std::runtime_error("failed test6");
00311   }
00312 
00313   item = 12;
00314   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00315 
00316   if (lowerbound != 5) {
00317     throw std::runtime_error("failed test 7");
00318   }
00319 
00320   lowerbound = fei::lowerBound<int>(item, (int*)0, (int)0);
00321 
00322   if (lowerbound != 0) {
00323     throw std::runtime_error("failed test 8");
00324   }
00325 
00326   std::vector<int> list2;
00327   list2.push_back(2);
00328 
00329   item = 2;
00330   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00331 
00332   if (lowerbound != 0) {
00333     throw std::runtime_error("failed test 9");
00334   }
00335 
00336   item = 5;
00337   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00338 
00339   if (lowerbound != 1) {
00340     throw std::runtime_error("failed test 10");
00341   }
00342 
00343   FEI_COUT << "ok"<<FEI_ENDL;
00344 
00345   return(0);
00346 }
00347 
00348 int test_misc::serialtest3()
00349 {
00350   FEI_COUT << "testing snl_fei::MapContig<fei::ctg_set<int>*>...";
00351 
00352   snl_fei::MapContig<fei::ctg_set<int>*> mc(1,5);
00353   fei_Pool_alloc<fei::ctg_set<int> > pool_alloc;
00354 
00355   static fei::ctg_set<int> dummy;
00356 
00357   for(int i=1; i<6; ++i) {
00358     fei::ctg_set<int>* newset = pool_alloc.allocate(1);
00359     pool_alloc.construct(newset,dummy);
00360   
00361 
00362     for(int j=0; j<3; ++j) {
00363       newset->insert2(j);
00364     }
00365 
00366     std::pair<int,fei::ctg_set<int>*> newpair(i,newset);
00367     mc.insert(newpair);
00368   }
00369   
00370   snl_fei::MapContig<fei::ctg_set<int>*> m_copy(mc);
00371 
00372   if (m_copy.size() != mc.size()) {
00373     throw std::runtime_error("failed test 1.");
00374   }
00375 
00376   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00377     mc_iter = mc.begin(),
00378     mc_end = mc.end();
00379 
00380   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00381     c_iter = m_copy.begin(),
00382     c_end = m_copy.end();
00383 
00384   for(; mc_iter != mc_end; ++mc_iter) {
00385     std::pair<int,fei::ctg_set<int>*> mc_pair = *mc_iter;
00386     std::pair<int,fei::ctg_set<int>*> c_pair = *c_iter;
00387 
00388     if (mc_pair.first != c_pair.first) {
00389       throw std::runtime_error("failed test 2.");
00390     }
00391 
00392     if (*(mc_pair.second) != *(c_pair.second)) {
00393       throw std::runtime_error("failed test 3.");
00394     }
00395     ++c_iter;
00396   }
00397 
00398   mc_iter = mc.begin();
00399   for(; mc_iter != mc_end; ++mc_iter) {
00400     pool_alloc.destroy((*mc_iter).second);
00401   }
00402 
00403   FEI_COUT << "ok" << FEI_ENDL;
00404 
00405   return(0);
00406 }
00407 
00408 int test_misc::test1()
00409 {
00410 
00411   return(0);
00412 }
00413 
00414 int test_misc::test2()
00415 {
00416 
00417  return(0);
00418 }
00419 
00420 int test_misc::test3()
00421 {
00422 
00423   return(0);
00424 }
00425 
00426 int test_misc::test4()
00427 {
00428   return(0);
00429 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends