FEI Version of the Day
test_Set.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 #include <fei_mpi.h>
00045 
00046 #include <test_utils/test_Set.hpp>
00047 
00048 #include <snl_fei_Utils.hpp>
00049 #include <fei_ctg_set.hpp>
00050 
00051 #undef fei_file
00052 #define fei_file "test_Set.cpp"
00053 #include <fei_ErrMacros.hpp>
00054 
00055 test_Set::test_Set(MPI_Comm comm)
00056  : tester(comm)
00057 {
00058 }
00059 
00060 test_Set::~test_Set()
00061 {
00062 }
00063 
00064 template<typename SET_TYPE>
00065 void set_test1(SET_TYPE& set_obj)
00066 {
00067   if (set_obj.size() < 1) {
00068     typename SET_TYPE::const_iterator
00069       s_beg = set_obj.begin(),
00070       s_end = set_obj.end();
00071 
00072     if (s_beg != s_end) {
00073       throw std::runtime_error("failed test 1");
00074     }
00075   }
00076   else set_obj.clear();
00077 
00078   std::pair<typename SET_TYPE::const_iterator,bool> result = set_obj.insert(5);
00079 
00080   if (!result.second) {
00081     throw std::runtime_error("failed test 2");
00082   }
00083 
00084   result = set_obj.insert(4);
00085 
00086   if (!result.second) {
00087     throw std::runtime_error("failed test 3");
00088   }
00089 
00090   result = set_obj.insert(7);
00091 
00092   ++(result.first);
00093   if (result.first != set_obj.end()) {
00094     throw std::runtime_error("failed test 4");
00095   }
00096 
00097   result = set_obj.insert(6);
00098 
00099   if (!result.second) {
00100     throw std::runtime_error("failed test 5");
00101   }
00102 
00103   ++(result.first);
00104 
00105   if (*(result.first) != 7) {
00106     throw std::runtime_error("failed test 6");
00107   }
00108 
00109   ++(result.first);
00110   if (result.first != set_obj.end()) {
00111     throw std::runtime_error("failed test 7");
00112   }
00113 
00114   result = set_obj.insert(2);
00115   result = set_obj.insert(3);
00116 
00117   ++(result.first);
00118   if (*(result.first) != 4) {
00119     throw std::runtime_error("failed test 8");
00120   }
00121 
00122   SET_TYPE set_copy(set_obj);
00123 
00124   if (set_copy.size() != set_obj.size()) {
00125     throw std::runtime_error("failed test 9");
00126   }
00127 
00128   typename SET_TYPE::const_iterator
00129     s_iter = set_obj.begin(),
00130     s_end = set_obj.end();
00131 
00132   typename SET_TYPE::const_iterator
00133     c_iter = set_copy.begin(),
00134     c_end = set_copy.end();
00135 
00136   for(; s_iter != s_end; ++s_iter) {
00137     if (*s_iter != *c_iter) {
00138       throw std::runtime_error("failed test 10");
00139     }
00140     ++c_iter;
00141   }
00142 
00143   if (c_iter != c_end) {
00144     throw std::runtime_error("failed test 11");
00145   }
00146 }
00147 
00148 template<typename SET_TYPE>
00149 void set_test2(SET_TYPE& set_obj)
00150 {
00151   if (set_obj.size() < 1) {
00152     typename SET_TYPE::const_iterator
00153       s_beg = set_obj.begin(),
00154       s_end = set_obj.end();
00155 
00156     if (s_beg != s_end) {
00157       throw std::runtime_error("failed test2 1");
00158     }
00159   }
00160   else set_obj.clear();
00161 
00162   set_obj.insert2(5);
00163   set_obj.insert2(4);
00164   set_obj.insert2(7);
00165   set_obj.insert2(6);
00166   set_obj.insert2(2);
00167   set_obj.insert2(3);
00168 
00169   SET_TYPE set_copy(set_obj);
00170 
00171   if (set_copy.size() != set_obj.size()) {
00172     throw std::runtime_error("failed test2 2");
00173   }
00174 
00175   typename SET_TYPE::const_iterator
00176     s_iter = set_obj.begin(),
00177     s_end = set_obj.end();
00178 
00179   typename SET_TYPE::const_iterator
00180     c_iter = set_copy.begin(),
00181     c_end = set_copy.end();
00182 
00183   for(; s_iter != s_end; ++s_iter) {
00184     if (*s_iter != *c_iter) {
00185       throw std::runtime_error("failed test2 3");
00186     }
00187     ++c_iter;
00188   }
00189 
00190   if (c_iter != c_end) {
00191     throw std::runtime_error("failed test2 4");
00192   }
00193 }
00194 
00195 int test_Set::runtests()
00196 {
00197   if (numProcs_ > 1) return(0);
00198 
00199   CHK_ERR( test1() );
00200   CHK_ERR( test2() );
00201   CHK_ERR( test3() );
00202   CHK_ERR( test4() );
00203   CHK_ERR( test5() );
00204   CHK_ERR( test6() );
00205   CHK_ERR( test7() );
00206   CHK_ERR( test8() );
00207   CHK_ERR( test9() );
00208 
00209   return(0);
00210 }
00211 
00212 int test_Set::test1()
00213 {
00214   return(0);
00215 }
00216 
00217 int test_Set::test2()
00218 {
00219   FEI_COUT << "testing fei::ctg_set<int> insert,insert2,find,iterate...";
00220   fei::ctg_set<int> sset2;
00221 
00222   sset2.insert(5);
00223   sset2.insert(8);
00224   sset2.insert(3);
00225   sset2.insert(0);
00226   sset2.insert(4);
00227   sset2.insert(1);
00228   sset2.insert(2);
00229 
00230   fei::ctg_set<int>::const_iterator
00231     ss2_iter = sset2.begin(),
00232     ss2_end = sset2.end();
00233 
00234   int i=0;
00235   for(; ss2_iter != ss2_end; ++ss2_iter) {
00236     if (*ss2_iter != i && *ss2_iter != 8) {
00237       return(-1);
00238     }
00239     ++i;
00240   }
00241 
00242   int size2 = sset2.size();
00243   if (size2 != 7) {
00244     return(-1);
00245   }
00246 
00247   fei::ctg_set<int>::const_iterator iter4 = sset2.find(4);
00248   if (*iter4 != 4) {
00249     return(-2);
00250   }
00251 
00252   ++iter4;
00253   if (*iter4 != 5) {
00254     return(-3);
00255   }
00256 
00257   fei::ctg_set<int>::const_iterator iter8 = sset2.find(8);
00258   if (*iter8 != 8) {
00259     return(-4);
00260   }
00261 
00262   set_test2(sset2);
00263 
00264   fei::ctg_set<int> sset3;
00265 
00266   sset3.insert2(1);
00267   sset3.insert2(3);
00268   sset3.insert2(6);
00269   sset3.insert2(8);
00270   sset3.insert2(0);
00271   sset3.insert2(2);
00272   sset3.insert2(9);
00273   sset3.insert2(11);
00274   sset3.insert2(4);
00275   sset3.insert2(10);
00276 
00277   int size3 = sset3.size();
00278   if (size3 != 10) {
00279     return(-1);
00280   }
00281 
00282   fei::ctg_set<int>::const_iterator ss3_iter4 = sset3.find(4);
00283   if (*ss3_iter4 != 4) {
00284     return(-2);
00285   }
00286 
00287   ++ss3_iter4;
00288   if (*ss3_iter4 != 6) {
00289     return(-3);
00290   }
00291 
00292   fei::ctg_set<int>::const_iterator ss3_iter8 = sset3.find(8);
00293   if (*ss3_iter8 != 8) {
00294     return(-4);
00295   }
00296 
00297   FEI_COUT << "ok"<<FEI_ENDL;
00298   return(0);
00299 }
00300 
00301 int test_Set::test3()
00302 {
00303   return(0);
00304 }
00305 
00306 int test_Set::test4()
00307 {
00308 
00309   return(0);
00310 }
00311 
00312 int test_Set::test5()
00313 {
00314   FEI_COUT << "testing fei::binarySearch(...,start,end,...)...";
00315 
00316   std::vector<int> array;
00317   for(int i=0; i<10; ++i) array.push_back(i);
00318 
00319   int start = 2;
00320   int end = 6;
00321   int insertPoint = -1;
00322   int offset = fei::binarySearch(9, &array[0], array.size(),
00323              start, end, insertPoint);
00324   if (offset >= 0) {
00325     return(-1);
00326   }
00327 
00328   offset = fei::binarySearch(4, &array[0], array.size(),
00329          start, end, insertPoint);
00330 
00331   if (offset < 0) {
00332     return(-1);
00333   }
00334 
00335   fei::ctg_set<int> sset;
00336   sset.insert2(1);
00337   sset.insert2(5);
00338   sset.insert2(9);
00339   sset.insert2(0);
00340   sset.insert2(4);
00341   sset.insert2(8);
00342 
00343   if (sset.size() != 6) {
00344     ERReturn(-1);
00345   }
00346 
00347   if (sset.find(0) == sset.end()) {
00348     ERReturn(-1);
00349   }
00350 
00351   FEI_COUT << "ok"<<FEI_ENDL;
00352 
00353   return(0);
00354 }
00355 
00356 int test_Set::test6()
00357 {
00358   return(0);
00359 }
00360 
00361 int test_Set::test7()
00362 {
00363   return(0);
00364 }
00365 
00366 
00367 int test_Set::test8()
00368 {
00369   return(0);
00370 }
00371 
00372 int test_Set::test9()
00373 {
00374 
00375   return(0);
00376 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends