FEI Version of the Day
test_misc.cpp
00001 /*
00002 // @HEADER
00003 // ************************************************************************
00004 //             FEI: Finite Element Interface to Linear Solvers
00005 //                  Copyright (2005) Sandia Corporation.
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the
00008 // U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Alan Williams (william@sandia.gov) 
00038 //
00039 // ************************************************************************
00040 // @HEADER
00041 */
00042 
00043 #include <fei_macros.hpp>
00044 
00045 #include <test_utils/fei_test_utils.hpp>
00046 
00047 #include <test_utils/test_misc.hpp>
00048 
00049 #include <test_utils/test_Factory_helper.hpp>
00050 
00051 #include <fei_FieldMask.hpp>
00052 #include <snl_fei_RecordCollection.hpp>
00053 
00054 #include <fei_VectorSpace.hpp>
00055 #include <fei_Vector_Impl.hpp>
00056 
00057 #undef fei_file
00058 #define fei_file "test_misc.cpp"
00059 #include <fei_ErrMacros.hpp>
00060 
00061 test_misc::test_misc(MPI_Comm comm)
00062   : tester(comm)
00063 {
00064 }
00065 
00066 test_misc::~test_misc()
00067 {
00068 }
00069 
00070 void test_misc_FieldMask()
00071 {
00072   FEI_COUT << "testing fei::FieldMask...";
00073 
00074   //A general test of fei::FieldMask.
00075 
00076   unsigned numFields = 5;
00077   std::vector<int> fieldIDs(numFields);
00078   std::vector<int> fieldSizes(numFields);
00079   int checkNumIndices = 0;
00080   for(unsigned i=0; i<numFields; ++i) {
00081     fieldIDs[i] = i;
00082     fieldSizes[i] = i;
00083     checkNumIndices += i;
00084   }
00085 
00086   fei::FieldMask fieldMask;
00087 
00088   for(int i=fieldIDs.size()-1; i>= 0; --i) {
00089     fieldMask.addField(fieldIDs[i], fieldSizes[i]);
00090   }
00091 
00092   std::vector<int>& maskFields = fieldMask.getFieldIDs();
00093   std::vector<int>& maskFieldSizes = fieldMask.getFieldSizes();
00094 
00095   if (maskFields != fieldIDs) {
00096     throw std::runtime_error("FieldMask test failed.");
00097   }
00098 
00099   if (maskFieldSizes != fieldSizes) {
00100     throw std::runtime_error("FieldMask size test failed.");
00101   }
00102 
00103   int checkOffset = 0;
00104   for(unsigned j=0; j<fieldIDs.size(); ++j) {
00105     int offset = -1;
00106     fieldMask.getFieldEqnOffset(fieldIDs[j], offset);
00107     if (offset != checkOffset) {
00108       throw std::runtime_error("FieldMask offset test failed.");
00109     }
00110     checkOffset += j;
00111   }
00112 
00113   int numIndices = fieldMask.getNumIndices();
00114   if (numIndices != checkNumIndices) {
00115     throw std::runtime_error("FieldMask numIndices test failed.");
00116   }
00117 
00118   bool exc_caught = false;
00119   try {
00120     fieldMask.addField(-1, 0);
00121   }
00122   catch (...) {
00123     exc_caught = true;
00124   }
00125 
00126   if (!exc_caught) {
00127     throw std::runtime_error("FieldMask failed to throw on negative fieldID.");
00128   }
00129 
00130   fieldMask.addField(2, 2);
00131 
00132   if (fieldMask.getNumFields() != numFields) {
00133     throw std::runtime_error("FieldMask getNumFields test failed.");
00134   }
00135 
00136   int fieldid1 = 0;
00137   int fieldid2 = 1;
00138   int fieldid3 = 2;
00139   int fieldsize = 1;
00140 
00141   fei::FieldMask fm1(1, &fieldid1, &fieldsize);
00142   fei::FieldMask fm2(1, &fieldid2, &fieldsize);
00143   fei::FieldMask fm3(1, &fieldid3, &fieldsize);
00144 
00145   fei::FieldMask fm12(fm1);
00146   fm12.addField(fieldid2, fieldsize);
00147 
00148   fei::FieldMask fm123(fm1);
00149   fm123.addField(fieldid2, fieldsize);
00150   fm123.addField(fieldid3, fieldsize);
00151 
00152   if (fm1.getMaskID() == fm2.getMaskID()) {
00153     throw std::runtime_error("FieldMask getMaskID test failed.");
00154   }
00155 
00156   if (fm2.getMaskID() == fm12.getMaskID()) {
00157     throw std::runtime_error("FieldMask getMaskID2 test failed.");
00158   }
00159 
00160   if (fm12.getMaskID() !=
00161       fei::FieldMask::calculateMaskID(fm1, fieldid2)){
00162     throw std::runtime_error("FieldMask getMaskID3 test failed.");
00163   }
00164 
00165   if (fm12.getMaskID() == fm3.getMaskID()) {
00166     throw std::runtime_error("FieldMask getMaskID4 test failed.");
00167   }
00168 
00169   if (fm123.getMaskID() != 
00170       fei::FieldMask::calculateMaskID(fm12, fieldid3)){
00171     throw std::runtime_error("FieldMask getMaskID5 test failed.");
00172   }
00173 
00174   if (fm3.getMaskID() == fm123.getMaskID()) {
00175     throw std::runtime_error("FieldMask getMaskID6 test failed.");
00176   }
00177 
00178   FEI_COUT << "ok"<<FEI_ENDL;
00179 }
00180 
00181 void test_misc_RecordCollection()
00182 {
00183   FEI_COUT << "testing snl_fei::RecordCollection...";
00184 
00185   int fieldID0 = 0;
00186   int fieldID1 = 1;
00187   int fieldID2 = 2;
00188   int fieldSize = 1;
00189   int ID0 = 0;
00190   int ID1 = 1;
00191 
00192   std::vector<fei::FieldMask*> fieldMasks;
00193 
00194   snl_fei::RecordCollection recColl(0);
00195 
00196   int* records = new int[1];
00197 
00198   recColl.initRecords(fieldID0, fieldSize, 1, &ID0,
00199           fieldMasks, records);
00200 
00201   recColl.initRecords(fieldID1, fieldSize, 1, &ID0,
00202           fieldMasks, records);
00203 
00204   recColl.initRecords(fieldID2, fieldSize, 1, &ID0,
00205           fieldMasks, records);
00206 
00207   recColl.initRecords(fieldID0, fieldSize, 1, &ID1,
00208           fieldMasks, records);
00209 
00210   recColl.initRecords(fieldID1, fieldSize, 1, &ID1,
00211           fieldMasks, records);
00212 
00213   recColl.initRecords(fieldID2, fieldSize, 1, &ID1,
00214           fieldMasks, records);
00215 
00216   if (fieldMasks.size() != 5) {
00217     throw std::runtime_error("RecordCollection fieldMasks.length test failed.");
00218   }
00219 
00220   std::vector<fei::Record<int> >& rvec = recColl.getRecords();
00221 
00222   std::vector<fei::Record<int> >::iterator
00223     r_iter = rvec.begin(),
00224     r_end = rvec.end();
00225 
00226   int numIndices = 0;
00227   for(; r_iter != r_end; ++r_iter) {
00228     numIndices += (*r_iter).getFieldMask()->getNumIndices();
00229   }
00230 
00231   if (numIndices != 6) {
00232     throw std::runtime_error("RecordCollection numIndices test failed.");
00233   }
00234 
00235   delete [] records;
00236   for(unsigned i=0; i<fieldMasks.size(); ++i) delete fieldMasks[i];
00237 
00238   FEI_COUT << "ok"<<FEI_ENDL;
00239 }
00240 
00241 int test_misc::runtests()
00242 {
00243   if (numProcs_ < 2) {
00244     test_misc_FieldMask();
00245     test_misc_RecordCollection();
00246 
00247     CHK_ERR( serialtest1() );
00248     CHK_ERR( serialtest2() );
00249     CHK_ERR( serialtest3() );
00250   }
00251 
00252   CHK_ERR( test1() );
00253   CHK_ERR( test2() );
00254   CHK_ERR( test3() );
00255   CHK_ERR( test4() );
00256   return(0);
00257 }
00258 
00259 int test_misc::serialtest1()
00260 {
00261   FEI_COUT << "testing fei_test_utils::within_percentage_margin...";
00262   double value1 = 65000.0;
00263   double value2 = 6500.0;
00264   bool result = fei_test_utils::within_percentage_margin(value1, value2, 10);
00265   if (result == true) {
00266     ERReturn(-1);
00267   }
00268 
00269   value1 = 6500.0;
00270   value2 = 6500.1;
00271   result = fei_test_utils::within_percentage_margin(value1,value2,1);
00272   if (result != true) {
00273     ERReturn(-1);
00274   }
00275 
00276   value1 = -10.0;
00277   value2 = 0.0;
00278   result = fei_test_utils::within_percentage_margin(value1,value2,30);
00279   if (result == true) {
00280     ERReturn(-1);
00281   }
00282 
00283   value1 = -1.e-18;
00284   value2 = 1.e-15;
00285   result = fei_test_utils::within_percentage_margin(value1,value2,10);
00286   if (result != true) {
00287     ERReturn(-1);
00288   }
00289 
00290   FEI_COUT << "ok"<<FEI_ENDL;
00291   return(0);
00292 }
00293 
00294 int test_misc::serialtest2()
00295 {
00296   FEI_COUT << "testing fei::lowerBound...";
00297   std::vector<int> list(5);
00298 
00299   list[0] = 1;
00300   list[1] = 4;
00301   list[2] = 6;
00302   list[3] = 7;
00303   list[4] = 11;
00304 
00305   int item = 0;
00306   int lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00307 
00308   if (lowerbound != 0) {
00309     throw std::runtime_error("failed test 1");
00310   }
00311 
00312   item = 1;
00313   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00314 
00315   if (lowerbound != 0) {
00316     throw std::runtime_error("failed test 2");
00317   }
00318 
00319   item = 2;
00320   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00321 
00322   if (lowerbound != 1) {
00323     throw std::runtime_error("failed test 3");
00324   }
00325 
00326   item = 7;
00327   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00328 
00329   if (lowerbound != 3) {
00330     throw std::runtime_error("failed test 4");
00331   }
00332 
00333   item = 9;
00334   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00335 
00336   if (lowerbound != 4) {
00337     throw std::runtime_error("failed test 5");
00338   }
00339 
00340   item = 11;
00341   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00342 
00343   if (lowerbound != 4) {
00344     throw std::runtime_error("failed test6");
00345   }
00346 
00347   item = 12;
00348   lowerbound = fei::lowerBound<int>(item, &list[0], list.size());
00349 
00350   if (lowerbound != 5) {
00351     throw std::runtime_error("failed test 7");
00352   }
00353 
00354   lowerbound = fei::lowerBound<int>(item, (int*)0, (int)0);
00355 
00356   if (lowerbound != 0) {
00357     throw std::runtime_error("failed test 8");
00358   }
00359 
00360   std::vector<int> list2;
00361   list2.push_back(2);
00362 
00363   item = 2;
00364   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00365 
00366   if (lowerbound != 0) {
00367     throw std::runtime_error("failed test 9");
00368   }
00369 
00370   item = 5;
00371   lowerbound = fei::lowerBound<int>(item, &list2[0], list2.size());
00372 
00373   if (lowerbound != 1) {
00374     throw std::runtime_error("failed test 10");
00375   }
00376 
00377   FEI_COUT << "ok"<<FEI_ENDL;
00378 
00379   return(0);
00380 }
00381 
00382 int test_misc::serialtest3()
00383 {
00384   FEI_COUT << "testing snl_fei::MapContig<fei::ctg_set<int>*>...";
00385 
00386   snl_fei::MapContig<fei::ctg_set<int>*> mc(1,5);
00387   fei_Pool_alloc<fei::ctg_set<int> > pool_alloc;
00388 
00389   static fei::ctg_set<int> dummy;
00390 
00391   for(int i=1; i<6; ++i) {
00392     fei::ctg_set<int>* newset = pool_alloc.allocate(1);
00393     pool_alloc.construct(newset,dummy);
00394   
00395 
00396     for(int j=0; j<3; ++j) {
00397       newset->insert2(j);
00398     }
00399 
00400     std::pair<int,fei::ctg_set<int>*> newpair(i,newset);
00401     mc.insert(newpair);
00402   }
00403   
00404   snl_fei::MapContig<fei::ctg_set<int>*> m_copy(mc);
00405 
00406   if (m_copy.size() != mc.size()) {
00407     throw std::runtime_error("failed test 1.");
00408   }
00409 
00410   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00411     mc_iter = mc.begin(),
00412     mc_end = mc.end();
00413 
00414   snl_fei::MapContig<fei::ctg_set<int>*>::iterator
00415     c_iter = m_copy.begin(),
00416     c_end = m_copy.end();
00417 
00418   for(; mc_iter != mc_end; ++mc_iter) {
00419     std::pair<int,fei::ctg_set<int>*> mc_pair = *mc_iter;
00420     std::pair<int,fei::ctg_set<int>*> c_pair = *c_iter;
00421 
00422     if (mc_pair.first != c_pair.first) {
00423       throw std::runtime_error("failed test 2.");
00424     }
00425 
00426     if (*(mc_pair.second) != *(c_pair.second)) {
00427       throw std::runtime_error("failed test 3.");
00428     }
00429     ++c_iter;
00430   }
00431 
00432   mc_iter = mc.begin();
00433   for(; mc_iter != mc_end; ++mc_iter) {
00434     pool_alloc.destroy((*mc_iter).second);
00435   }
00436 
00437   FEI_COUT << "ok" << FEI_ENDL;
00438 
00439   return(0);
00440 }
00441 
00442 int test_misc::test1()
00443 {
00444 
00445   return(0);
00446 }
00447 
00448 int test_misc::test2()
00449 {
00450 
00451  return(0);
00452 }
00453 
00454 int test_misc::test3()
00455 {
00456 
00457   return(0);
00458 }
00459 
00460 int test_misc::test4()
00461 {
00462   return(0);
00463 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends