stressIterators.h

Go to the documentation of this file.
00001 // @(#)root/test:$Id: stressIterators.h 33103 2010-04-20 10:43:37Z bellenot $
00002 // Author: Anar Manafov   18/04/2008
00003 #ifndef ROOT_stressIterators
00004 #define ROOT_stressIterators
00005 
00006 #ifdef WIN32
00007 #pragma warning(disable: 4290)
00008 #endif
00009 
00010 static Int_t gCount = 0;
00011 
00012 // Here we have a collection of functors and functions used by the test suit
00013 //______________________________________________________________________________
00014 template<class T>
00015 struct SEnumFunctor {
00016    bool operator()(TObject *aObj) const throw(std::exception) {
00017       if (!aObj)
00018          throw std::invalid_argument("SEnumFunctor: aObj is a NULL pointer");
00019 
00020       if ((aObj->IsA() == TObjString::Class())) {
00021          TObjString *str(dynamic_cast<TObjString*>(aObj));
00022          if (!str)
00023             throw std::runtime_error("SEnumFunctor: Container's element is not a TObjString object.");
00024 
00025          ++gCount;
00026          std::cout << str->String().Data() << std::endl;
00027       }
00028 
00029       return true;
00030    }
00031 };
00032 
00033 //______________________________________________________________________________
00034 template<>
00035 struct SEnumFunctor<TMap> {
00036    bool operator()(TObject *aObj) const throw(std::exception) {
00037       if (!aObj)
00038          throw std::invalid_argument("SEnumFunctor: aObj is a NULL pointer");
00039 
00040       if ((aObj->IsA() == TPair::Class())) {
00041          TPair *pair(dynamic_cast<TPair*>(aObj));
00042          if (!pair)
00043             throw std::runtime_error("SEnumFunctor: Container's element is not a TPair object.");
00044 
00045          TObjString *key(dynamic_cast<TObjString*>(pair->Key()));
00046          TObjString *value(dynamic_cast<TObjString*>(pair->Value()));
00047          if (!key || !value)
00048             throw std::runtime_error("SEnumFunctor: Can't retriev key/value of a pair");
00049 
00050          ++gCount;
00051          std::cout << key->String().Data() << " : " << value->String().Data() << std::endl;
00052       }
00053 
00054       return true;
00055    }
00056 };
00057 
00058 //______________________________________________________________________________
00059 template<class T>
00060 struct SFind : std::binary_function<TObject*, TString, bool> {
00061    bool operator()(TObject *_Obj, const TString &_ToFind) const {
00062       TObjString *str(dynamic_cast<TObjString*>(_Obj));
00063       if (!str)
00064          throw std::runtime_error("SFind: Container's element is not a TObString object.");
00065       return !str->String().CompareTo(_ToFind);
00066    }
00067 };
00068 
00069 //______________________________________________________________________________
00070 template<>
00071 struct SFind<TMap> : std::binary_function<TObject*, TString, bool> {
00072    bool operator()(TObject *_Obj, const TString &_ToFind) const {
00073       TPair *pair(dynamic_cast<TPair*>(_Obj));
00074       if (!pair)
00075          throw std::runtime_error("SFind: Container's element is not a TPair object.");
00076       // Checking the VALUE of the pair
00077       TObjString *str(dynamic_cast<TObjString*>(pair->Value()));
00078       return !str->String().CompareTo(_ToFind);
00079    }
00080 };
00081 
00082 //______________________________________________________________________________
00083 // Checking a given container with for_each algorithm
00084 // Full iteration: from Begin to End
00085 template<class T>
00086 void TestContainer_for_each(const T &container, Int_t aSize) throw(std::exception)
00087 {
00088    gCount = 0; // TODO: a use of gCount is a very bad method. Needs to be revised.
00089 
00090    TIterCategory<T> iter(&container);
00091    std::for_each(iter.Begin(), TIterCategory<T>::End(), SEnumFunctor<T>());
00092    if (aSize != gCount)
00093       throw std::runtime_error("Test case <TestList_for_each> has failed.");
00094    std::cout << "->> Ok." << std::endl;
00095 }
00096 
00097 //______________________________________________________________________________
00098 // Checking a given container with for_each algorithm
00099 // Partial iteration: from Begin to 3rd element
00100 template<class T>
00101 void TestContainer_for_each2(const T &container) throw(std::exception)
00102 {
00103    gCount = 0; // TODO: a use of gCount is a very bad method. Needs to be revised.
00104 
00105    TIterCategory<T> iter(&container);
00106    TIterCategory<T> iter_end(&container);
00107    // Artificially shifting the iterator to the 4th potision - a new End iterator
00108    iter_end();
00109    iter_end();
00110    iter_end();
00111    iter_end();
00112    std::for_each(iter.Begin(), iter_end, SEnumFunctor<T>());
00113    if (3 != gCount)
00114       throw std::runtime_error("Test case <TestList_for_each2> has failed.");
00115    std::cout << "->> Ok." << std::endl;
00116 }
00117 
00118 //______________________________________________________________________________
00119 // Checking a ROOT container with find_if algorithm
00120 template<class T>
00121 void TestContainer_find_if(const T &container, const TString &aToFind) throw(std::exception)
00122 {
00123    typedef TIterCategory<T> iterator_t;
00124 
00125    iterator_t iter(&container);
00126    iterator_t found(
00127       std::find_if(iter.Begin(), iterator_t::End(), bind2nd(SFind<T>(), aToFind))
00128    );
00129    if (!(*found))
00130       throw std::runtime_error("Test case <TestContainer_find_if> has failed. Can't find object.");
00131 
00132    // Checking whether element is a Pair or not
00133    if (((*found)->IsA() == TPair::Class())) {
00134       TPair *pair(dynamic_cast<TPair*>(*found));
00135       if (!pair)
00136          throw std::runtime_error("TestContainer_find_if: Container's element is not a TPair object.");
00137       TObjString *key(dynamic_cast<TObjString*>(pair->Key()));
00138       TObjString *val(dynamic_cast<TObjString*>(pair->Value()));
00139       std::cout << "I found: [" << key->String().Data() << " : " << val->String().Data() << "]" << std::endl;
00140       std::cout << "->> Ok." << std::endl;
00141       return;
00142    }
00143 
00144    TObjString *str(dynamic_cast<TObjString*>(*found));
00145    if (!str)
00146       throw std::runtime_error("Test case <TestContainer_find_if> has failed. String object is NULL");
00147 
00148    std::cout << "I found: " << str->String().Data() << std::endl;
00149    std::cout << "->> Ok." << std::endl;
00150 }
00151 
00152 //______________________________________________________________________________
00153 // Checking a ROOT container with count_if algorithm
00154 template<class T>
00155 void TestContainer_count_if(const T &container, const TString &aToFind, Int_t Count) throw(std::exception)
00156 {
00157    typedef TIterCategory<T> iterator_t;
00158 
00159    iterator_t iter(&container);
00160    typename iterator_t::difference_type cnt(
00161       std::count_if(iter.Begin(), iterator_t::End(), bind2nd(SFind<T>(), aToFind))
00162    );
00163 
00164    if (Count != cnt)
00165       throw std::runtime_error("Test case <TestContainer_count_if> has failed.");
00166 
00167    std::cout << "->> Ok." << std::endl;
00168 }
00169 
00170 #endif

Generated on Tue Jul 5 15:15:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1