stressIterators.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: stressIterators.cxx 23532 2008-04-24 16:24:31Z rdm $
00002 // Author: Anar Manafov   01/04/2008
00003 
00004 //----------------------------------------------------------------
00005 // This is a tests ROOT Iterators and STL algorithms.
00006 // The test project covers the following cases:
00007 // 1  - TList with std::for_each (Full iteration: from the Begin up to the End)
00008 // 2  - TList with std::find_if
00009 // 3  - TList with std::count_if
00010 // 4  - TObjArray with std::for_each (Full iteration: from the Begin up to the End)
00011 // 5  - TObjArray with std::find_if
00012 // 6  - TObjArray with std::count_if
00013 // 7  - TMap with std::for_each (Full iteration: from the Begin up to the End)
00014 // 8  - TMap with std::for_each (Partial iteration: from the Begin up to the 3rd element)
00015 // 9  - TMap with std::find_if
00016 // 10 - TMap with std::count_if
00017 // 11 - TBtree with std::for_each (Full iteration: from the Begin up to the End)
00018 // 12 - TBtree with std::find_if
00019 // 13 - TBtree with std::count_if
00020 // 14 - TOrdCollection with std::for_each (Full iteration: from the Begin up to the End)
00021 // 15 - TOrdCollection with std::find_if
00022 // 16 - TOrdCollection with std::count_if
00023 // 17 - TRefArray with std::for_each (Full iteration: from the Begin up to the End)
00024 // 18 - TRefArray with std::find_if
00025 // 19 - TRefArray with std::count_if
00026 
00027 
00028 // STD
00029 #include <iostream>
00030 #include <algorithm>
00031 #include <sstream>
00032 #include <stdexcept>
00033 #include <functional>
00034 // ROOT
00035 #include "TList.h"
00036 #include "TObjString.h"
00037 #include "TObjArray.h"
00038 #include "TMap.h"
00039 #include "TBtree.h"
00040 #include "TOrdCollection.h"
00041 #include "TRefArray.h"
00042 // Local
00043 #include "stressIterators.h"
00044 
00045 const char * const cszValue("value");
00046 
00047 using namespace std;
00048 
00049 template<class __T>
00050 void fill_container(__T* _container, Int_t _count)
00051 {
00052    _container->SetOwner();
00053 
00054    ostringstream ss;
00055    for (int i = 0; i < _count; ++i) {
00056       ss << "test string #" << i;
00057       TObjString *s(new TObjString(ss.str().c_str()));
00058       _container->Add(s);
00059       ss.str("");
00060    }
00061 }
00062 
00063 template<>
00064 void fill_container<TMap>(TMap* _container, Int_t _count)
00065 {
00066    _container->SetOwner();
00067 
00068    ostringstream ss;
00069    for (int i = 0; i < _count; ++i) {
00070       ss << "test string #" << i;
00071       TObjString *s(new TObjString(ss.str().c_str()));
00072       _container->Add(s, new TObjString(cszValue));
00073       ss.str("");
00074    }
00075 }
00076 
00077 //______________________________________________________________________________
00078 void stressIterators() throw(exception)
00079 {
00080    const Int_t size = 15;
00081 
00082    ostringstream ss;
00083 
00084    {
00085       // TList
00086       TList list;
00087       fill_container(&list, size);
00088 
00089       cout << "#1 ====================================" << endl;
00090       cout << "-----> " << "TestContainer_for_each<TList>(list, list.GetSize())" << endl;
00091       TestContainer_for_each<TList>(list, list.GetSize());
00092 
00093       cout << "\n#2 ====================================" << endl;
00094       cout << "-----> " << "TestContainer_find_if<TList>(list, \"test string #3\")" << endl;
00095       TestContainer_find_if<TList>(list, "test string #3");
00096 
00097       cout << "\n#3 ====================================" << endl;
00098       cout << "-----> " << "TestContainer_count_if<TList>(list, \"test string #3\", 1)" << endl;
00099       // we suppose to find exactly one match
00100       TestContainer_count_if<TList>(list, "test string #3", 1);
00101    }
00102 
00103    {
00104       // TObjArray
00105       TObjArray obj_array(size);
00106       fill_container(&obj_array, size);
00107 
00108       cout << "\n#4 ====================================" << endl;
00109       cout << "-----> " << "TestContainer_for_each<TObjArray>(obj_array, obj_array.GetSize())" << endl;
00110       TestContainer_for_each<TObjArray>(obj_array, obj_array.GetSize());
00111 
00112       cout << "\n#5 ====================================" << endl;
00113       cout << "-----> " << "TestContainer_find_if<TObjArray>(obj_array, \"test string #3\")" << endl;
00114       TestContainer_find_if<TObjArray>(obj_array, "test string #3");
00115 
00116       cout << "\n#6 ====================================" << endl;
00117       cout << "-----> " << "TestContainer_count_if<TObjArray>(obj_array, \"test string #3\", 1)" << endl;
00118       // we suppose to find exactly one match
00119       TestContainer_count_if<TObjArray>(obj_array, "test string #3", 1);
00120    }
00121 
00122    {
00123       // TMap
00124       TMap map_container(size);
00125       fill_container(&map_container, size);
00126 
00127       cout << "\n#7 ====================================" << endl;
00128       cout << "-----> " << "TestContainer_for_each<TMap>(map_container, map_container.GetSize())" << endl;
00129       TestContainer_for_each<TMap>(map_container, map_container.GetSize());
00130       cout << "\n#8 ====================================" << endl;
00131       cout << "-----> " << "TestContainer_for_each2<TMap>(map_container)" << endl;
00132       TestContainer_for_each2<TMap>(map_container);
00133       cout << "\n#9 ====================================" << endl;
00134       cout << "-----> " << "TestContainer_find_if<TMap>(map_container, cszValue)" << endl;
00135       TestContainer_find_if<TMap>(map_container, cszValue);
00136       cout << "\n#10 ====================================" << endl;
00137       cout << "-----> " << "TestContainer_count_if<TMap>(map_container, cszValue, map_container.GetSize())" << endl;
00138       TestContainer_count_if<TMap>(map_container, cszValue, map_container.GetSize());
00139    }
00140 
00141    {
00142       // TBtree
00143       TBtree btree_container;
00144       fill_container(&btree_container, size);
00145 
00146       cout << "\n#11 ====================================" << endl;
00147       cout << "-----> " << "TestContainer_for_each<TBtree>(btree_container, btree_container.GetSize())" << endl;
00148       TestContainer_for_each<TBtree>(btree_container, btree_container.GetSize());
00149       cout << "\n#12 ====================================" << endl;
00150       cout << "-----> " << "TestContainer_find_if<TBtree>(btree_container, \"test string #3\")" << endl;
00151       TestContainer_find_if<TBtree>(btree_container, "test string #3");
00152       cout << "\n#13 ====================================" << endl;
00153       cout << "-----> " << "TestContainer_count_if<TBtree>(btree_container, \"test string #3\", 1)" << endl;
00154       TestContainer_count_if<TBtree>(btree_container, "test string #3", 1);
00155    }
00156 
00157    {
00158       // TOrdCollection
00159       TOrdCollection container;
00160       fill_container(&container, size);
00161 
00162       cout << "\n#14 ====================================" << endl;
00163       cout << "-----> " << "TestContainer_for_each<TOrdCollection>(container, container.GetSize())" << endl;
00164       TestContainer_for_each<TOrdCollection>(container, container.GetSize());
00165       cout << "\n#15 ====================================" << endl;
00166       cout << "-----> " << "TestContainer_find_if<TOrdCollection>(container, \"test string #3\");" << endl;
00167       TestContainer_find_if<TOrdCollection>(container, "test string #3");
00168       cout << "\n#16 ====================================" << endl;
00169       cout << "-----> " << "TestContainer_count_if<TOrdCollection>(container, \"test string #3\", 1)" << endl;
00170       TestContainer_count_if<TOrdCollection>(container, "test string #3", 1);
00171    }
00172 
00173    {
00174       // TRefArray
00175      TRefArray container;
00176      fill_container(&container, size);
00177 
00178      cout << "\n#17 ====================================" << endl;
00179      cout << "-----> " << "TestContainer_for_each<TRefArray>(container, container.GetSize())" << endl;
00180      TestContainer_for_each<TRefArray>(container, container.GetLast()+1); // TODO: why container.GetSize() returns 16 instead of 15
00181      cout << "\n#18 ====================================" << endl;
00182      cout << "-----> " << "TestContainer_find_if<TOrdCollection>(container, \"test string #3\");" << endl;
00183      TestContainer_find_if<TRefArray>(container, "test string #3");
00184      cout << "\n#19 ====================================" << endl;
00185      cout << "-----> " << "TestContainer_count_if<TOrdCollection>(container, \"test string #3\", 1)" << endl;
00186      TestContainer_count_if<TRefArray>(container, "test string #3", 1);
00187    }
00188 
00189 }
00190 
00191 //______________________________________________________________________________
00192 // return 0 on success and 1 otherwise
00193 int main()
00194 {
00195    try {
00196 
00197       stressIterators();
00198 
00199    } catch (const exception &e) {
00200       cerr << "Test has failed!" << endl;
00201       cerr << "Detailes: " << e.what() << endl;
00202       return 1;
00203    } catch (...) {
00204       cerr << "Test has failed!" << endl;
00205       cerr << "Unexpected error occurred." << endl;
00206       return 1;
00207    }
00208 
00209    cout << "\nTest successfully finished!" << endl;
00210    return 0;
00211 }

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