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], 1);
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, numInstances = -1;
00072     fieldMask.getFieldEqnOffset(fieldIDs[j], offset, numInstances);
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, 1);
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, 1);
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   int numinstances=1;
00107 
00108   fei::FieldMask fm1(1, &fieldid1, &fieldsize, &numinstances);
00109   fei::FieldMask fm2(1, &fieldid2, &fieldsize, &numinstances);
00110   fei::FieldMask fm3(1, &fieldid3, &fieldsize, &numinstances);
00111 
00112   fei::FieldMask fm12(fm1);
00113   fm12.addField(fieldid2, fieldsize);
00114 
00115   fei::FieldMask fm123(fm1);
00116   fm123.addField(fieldid2, fieldsize);
00117   fm123.addField(fieldid3, fieldsize);
00118 
00119   if (fm1.getMaskID() == fm2.getMaskID()) {
00120     throw std::runtime_error("FieldMask getMaskID test failed.");
00121   }
00122 
00123   if (fm2.getMaskID() == fm12.getMaskID()) {
00124     throw std::runtime_error("FieldMask getMaskID2 test failed.");
00125   }
00126 
00127   if (fm12.getMaskID() !=
00128       fei::FieldMask::calculateMaskID(fm1, fieldid2, numinstances)){
00129     throw std::runtime_error("FieldMask getMaskID3 test failed.");
00130   }
00131 
00132   if (fm12.getMaskID() == fm3.getMaskID()) {
00133     throw std::runtime_error("FieldMask getMaskID4 test failed.");
00134   }
00135 
00136   if (fm123.getMaskID() != 
00137       fei::FieldMask::calculateMaskID(fm12, fieldid3, numinstances)){
00138     throw std::runtime_error("FieldMask getMaskID5 test failed.");
00139   }
00140 
00141   if (fm3.getMaskID() == fm123.getMaskID()) {
00142     throw std::runtime_error("FieldMask getMaskID6 test failed.");
00143   }
00144 
00145   FEI_COUT << "ok"<<FEI_ENDL;
00146 }
00147 
00148 void test_misc_RecordCollection()
00149 {
00150   FEI_COUT << "testing snl_fei::RecordCollection...";
00151 
00152   int fieldID0 = 0;
00153   int fieldID1 = 1;
00154   int fieldID2 = 2;
00155   int fieldSize = 1;
00156   int ID0 = 0;
00157   int ID1 = 1;
00158 
00159   std::vector<fei::FieldMask*> fieldMasks;
00160 
00161   snl_fei::RecordCollection recColl(0);
00162 
00163   fei::Record<int>** records = new fei::Record<int>*[1];
00164 
00165   recColl.initRecords(fieldID0, fieldSize, 1, 1, &ID0,
00166           fieldMasks, records);
00167 
00168   recColl.initRecords(fieldID1, fieldSize, 1, 1, &ID0,
00169           fieldMasks, records);
00170 
00171   recColl.initRecords(fieldID2, fieldSize, 1, 1, &ID0,
00172           fieldMasks, records);
00173 
00174   recColl.initRecords(fieldID0, fieldSize, 1, 1, &ID1,
00175           fieldMasks, records);
00176 
00177   recColl.initRecords(fieldID1, fieldSize, 1, 1, &ID1,
00178           fieldMasks, records);
00179 
00180   recColl.initRecords(fieldID2, fieldSize, 1, 1, &ID1,
00181           fieldMasks, records);
00182 
00183   if (fieldMasks.size() != 5) {
00184     throw std::runtime_error("RecordCollection fieldMasks.length test failed.");
00185   }
00186 
00187   snl_fei::RecordCollection::map_type& rmap = recColl.getRecords();
00188 
00189   snl_fei::RecordCollection::map_type::iterator
00190     r_iter = rmap.begin(),
00191     r_end = rmap.end();
00192 
00193   int numIndices = 0;
00194   for(; r_iter != r_end; ++r_iter) {
00195     numIndices += (*r_iter).second->getFieldMask()->getNumIndices();
00196   }
00197 
00198   if (numIndices != 6) {
00199     throw std::runtime_error("RecordCollection numIndices test failed.");
00200   }
00201 
00202   delete [] records;
00203   for(unsigned i=0; i<fieldMasks.size(); ++i) delete fieldMasks[i];
00204 
00205   FEI_COUT << "ok"<<FEI_ENDL;
00206 }
00207 
00208 int test_misc::runtests()
00209 {
00210   if (numProcs_ < 2) {
00211     test_misc_FieldMask();
00212     test_misc_RecordCollection();
00213 
00214     CHK_ERR( serialtest1() );
00215     CHK_ERR( serialtest2() );
00216     CHK_ERR( serialtest3() );
00217   }
00218 
00219   CHK_ERR( test1() );
00220   CHK_ERR( test2() );
00221   CHK_ERR( test3() );
00222   CHK_ERR( test4() );
00223   return(0);
00224 }
00225 
00226 int test_misc::serialtest1()
00227 {
00228   FEI_COUT << "testing fei_test_utils::within_percentage_margin...";
00229   double value1 = 65000.0;
00230   double value2 = 6500.0;
00231   bool result = fei_test_utils::within_percentage_margin(value1, value2, 10);
00232   if (result == true) {
00233     ERReturn(-1);
00234   }
00235 
00236   value1 = 6500.0;
00237   value2 = 6500.1;
00238   result = fei_test_utils::within_percentage_margin(value1,value2,1);
00239   if (result != true) {
00240     ERReturn(-1);
00241   }
00242 
00243   value1 = -10.0;
00244   value2 = 0.0;
00245   result = fei_test_utils::within_percentage_margin(value1,value2,30);
00246   if (result == true) {
00247     ERReturn(-1);
00248   }
00249 
00250   value1 = -1.e-18;
00251   value2 = 1.e-15;
00252   result = fei_test_utils::within_percentage_margin(value1,value2,10);
00253   if (result != true) {
00254     ERReturn(-1);
00255   }
00256 
00257   FEI_COUT << "ok"<<FEI_ENDL;
00258   return(0);
00259 }
00260 
00261 int test_misc::serialtest2()
00262 {
00263   FEI_COUT << "testing fei::lowerBound...";
00264   std::vector<int> list(5);
00265 
00266   list[0] = 1;
00267   list[1] = 4;
00268   list[2] = 6;
00269   list[3] = 7;
00270   list[4] = 11;
00271 
00272   int item = 0;
00273   int lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00274 
00275   if (lowerbound != 0) {
00276     throw std::runtime_error("failed test 1");
00277   }
00278 
00279   item = 1;
00280   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00281 
00282   if (lowerbound != 0) {
00283     throw std::runtime_error("failed test 2");
00284   }
00285 
00286   item = 2;
00287   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00288 
00289   if (lowerbound != 1) {
00290     throw std::runtime_error("failed test 3");
00291   }
00292 
00293   item = 7;
00294   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00295 
00296   if (lowerbound != 3) {
00297     throw std::runtime_error("failed test 4");
00298   }
00299 
00300   item = 9;
00301   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00302 
00303   if (lowerbound != 4) {
00304     throw std::runtime_error("failed test 5");
00305   }
00306 
00307   item = 11;
00308   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00309 
00310   if (lowerbound != 4) {
00311     throw std::runtime_error("failed test6");
00312   }
00313 
00314   item = 12;
00315   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00316 
00317   if (lowerbound != 5) {
00318     throw std::runtime_error("failed test 7");
00319   }
00320 
00321   lowerbound = fei::lowerBound<int>(item, (int*)0, (int)0);
00322 
00323   if (lowerbound != 0) {
00324     throw std::runtime_error("failed test 8");
00325   }
00326 
00327   std::vector<int> list2;
00328   list2.push_back(2);
00329 
00330   item = 2;
00331   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00332 
00333   if (lowerbound != 0) {
00334     throw std::runtime_error("failed test 9");
00335   }
00336 
00337   item = 5;
00338   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00339 
00340   if (lowerbound != 1) {
00341     throw std::runtime_error("failed test 10");
00342   }
00343 
00344   FEI_COUT << "ok"<<FEI_ENDL;
00345 
00346   return(0);
00347 }
00348 
00349 int test_misc::serialtest3()
00350 {
00351   FEI_COUT << "testing snl_fei::MapContig<fei::ctg_set<int>*>...";
00352 
00353   snl_fei::MapContig<fei::ctg_set<int>*> mc(1,5);
00354   fei_Pool_alloc<fei::ctg_set<int> > pool_alloc;
00355 
00356   static fei::ctg_set<int> dummy;
00357 
00358   for(int i=1; i<6; ++i) {
00359     fei::ctg_set<int>* newset = pool_alloc.allocate(1);
00360     pool_alloc.construct(newset,dummy);
00361   
00362 
00363     for(int j=0; j<3; ++j) {
00364       newset->insert2(j);
00365     }
00366 
00367     std::pair<int,fei::ctg_set<int>*> newpair(i,newset);
00368     mc.insert(newpair);
00369   }
00370   
00371   snl_fei::MapContig<fei::ctg_set<int>*> m_copy(mc);
00372 
00373   if (m_copy.size() != mc.size()) {
00374     throw std::runtime_error("failed test 1.");
00375   }
00376 
00377   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00378     mc_iter = mc.begin(),
00379     mc_end = mc.end();
00380 
00381   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00382     c_iter = m_copy.begin(),
00383     c_end = m_copy.end();
00384 
00385   for(; mc_iter != mc_end; ++mc_iter) {
00386     std::pair<int,fei::ctg_set<int>*> mc_pair = *mc_iter;
00387     std::pair<int,fei::ctg_set<int>*> c_pair = *c_iter;
00388 
00389     if (mc_pair.first != c_pair.first) {
00390       throw std::runtime_error("failed test 2.");
00391     }
00392 
00393     if (*(mc_pair.second) != *(c_pair.second)) {
00394       throw std::runtime_error("failed test 3.");
00395     }
00396     ++c_iter;
00397   }
00398 
00399   mc_iter = mc.begin();
00400   for(; mc_iter != mc_end; ++mc_iter) {
00401     pool_alloc.destroy((*mc_iter).second);
00402   }
00403 
00404   FEI_COUT << "ok" << FEI_ENDL;
00405 
00406   return(0);
00407 }
00408 
00409 int test_misc::test1()
00410 {
00411 
00412   return(0);
00413 }
00414 
00415 int test_misc::test2()
00416 {
00417 
00418  return(0);
00419 }
00420 
00421 int test_misc::test3()
00422 {
00423 
00424   return(0);
00425 }
00426 
00427 int test_misc::test4()
00428 {
00429   return(0);
00430 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends