OKlibrary  0.2.1.6
AssociativeContainers_Tests.hpp
Go to the documentation of this file.
00001 // Matthew Henderson, 6.9.2005 (Swansea)
00002 /* Copyright 2005 - 2007, 2008 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00017 #ifndef ASSOCIATIVECONTAINERSTESTS_okMMnh
00018 #define ASSOCIATIVECONTAINERSTESTS_okMMnh
00019 
00020 #include <string>
00021 #include <set>
00022 
00023 #include <boost/assign/std/set.hpp>
00024 #include <boost/assign/std/vector.hpp>
00025 #include <boost/tuple/tuple.hpp>
00026 
00027 #include <OKlib/TestSystem/TestBaseClass.hpp>
00028 #include <OKlib/TestSystem/TestExceptions.hpp>
00029 
00030 namespace OKlib {
00031   namespace SearchDataStructures {
00032 
00033     // ##############################################################
00034 
00045     class BaseTestData {
00046     public:
00047       typedef std::string string_type;
00048     };
00049 
00058     class PrefixTestData : BaseTestData {
00059       
00060       typedef std::vector<string_type> prefix_vector_type;
00061       
00062     public:
00063 
00064       typedef prefix_vector_type::const_iterator const_iterator;
00065       static prefix_vector_type prefix_vector;
00066       static prefix_vector_type prefix_vector_2;
00067 
00068 
00069       PrefixTestData() {
00070 
00071         using namespace boost::assign;
00072         typedef string_type el_t;
00073 
00074         prefix_vector_2 +=
00075           el_t("AnalyseTotalAssignment.hpp/Autarkies/Search/OKsystem");
00076 
00077         prefix_vector +=
00078           el_t("test001.hpp/dir001/"),
00079           el_t("test001.hpp/dir001/"),
00080           el_t("test002.hpp/dir001/dir002/"), 
00081           el_t("test002.hpp/dir001/dir002/dir003/");
00082       }
00083       
00084     };
00085 
00086     PrefixTestData::prefix_vector_type PrefixTestData::prefix_vector;
00087     PrefixTestData::prefix_vector_type PrefixTestData::prefix_vector_2;
00088 
00089     // ##############################################################
00090 
00101     class ExtensionTestData : BaseTestData {
00102      
00103       typedef boost::tuple<string_type, bool, string_type, bool> tuple_type;
00104       typedef std::vector<tuple_type> extension_vector_type;
00105 
00106     public:
00107 
00108       typedef extension_vector_type::const_iterator const_iterator;
00109       static extension_vector_type extension_vector;
00110       static extension_vector_type extension_vector_2;
00111 
00112       ExtensionTestData() {
00113 
00114         using namespace boost::assign;
00115         typedef tuple_type el_t;
00116 
00117         extension_vector_2 +=
00118           el_t("AnalyseTotalAssignment.hpp",true,"AnalyseTotalAssignment.hpp/Autarkies/Search/OKsystem",true);
00119 
00120         extension_vector +=
00121           el_t("test001.hpp",true,"test001.hpp/dir001/",true),
00122           el_t("test002.hpp",true,"test002.hpp/dir001/dir002/",false),
00123           el_t("test003.hpp",false,"",true);
00124 
00125       }
00126 
00127       string_type header(const_iterator i) {
00128         return i -> get<0>();
00129       }
00130 
00131       bool has_extension(const_iterator i) {
00132         return i -> get<1>();
00133       }
00134 
00135       string_type extension_value(const_iterator i) {
00136         return i -> get<2>();
00137       }
00138 
00139       bool uniqueness(const_iterator i) {
00140         return i -> get<3>();
00141       }
00142 
00143     };
00144 
00145     ExtensionTestData::extension_vector_type ExtensionTestData::extension_vector;
00146     ExtensionTestData::extension_vector_type ExtensionTestData::extension_vector_2;
00147 
00148     // ##############################################################
00149 
00160     template <template <class Range> class PrefixContainer>
00161     class Test_AssociativePrefixContainer : public ::OKlib::TestSystem::TestBase {
00162     public :
00163 
00164       typedef Test_AssociativePrefixContainer test_type;
00165       Test_AssociativePrefixContainer() {
00166         insert(this);
00167       }
00168 
00169     private :
00170 
00171       PrefixTestData prefix_test_data;
00172       ExtensionTestData extension_test_data;
00173 
00174       typedef typename BaseTestData::string_type string_type;
00175 
00186       void test_assign_function() {
00187         typedef PrefixTestData::prefix_vector_type vector_type;
00188         typedef AssociativePrefixContainer<string_type> prefix_container_type;
00189 
00190         const vector_type& prefix_vector(prefix_test_data.prefix_vector);      
00191         prefix_container_type prefix_container;
00192         prefix_container.assign(prefix_vector);
00193 
00194         std::set<string_type> expected_prefix_set(prefix_vector.begin(),prefix_vector.end());
00195         std::set<string_type> actual_prefix_set(prefix_container.begin(),prefix_container.end());
00196 
00197         if (not (expected_prefix_set == actual_prefix_set))
00198           OKLIB_THROW("not (expected_prefix_set == actual_prefix_set)");
00199       }
00200 
00201       // #############################
00202 
00213       void test_begin_end() {
00214         typedef AssociativePrefixContainer<string_type> prefix_container_type;
00215         typedef PrefixTestData::prefix_vector_type vector_type;
00216         prefix_container_type prefix_container;
00217         const vector_type& prefix_vector(prefix_test_data.prefix_vector);
00218 
00219         if (not (prefix_container.begin() == prefix_container.end()))
00220           OKLIB_THROW("not (prefix_container.begin() == prefix_container.end())");
00221         prefix_container.assign(prefix_vector);
00222         if (prefix_container.begin() == prefix_container.end())
00223           OKLIB_THROW("(prefix_container.begin() == prefix_container.end())");
00224 
00225       }
00226 
00227       // #############################
00228 
00242       void test_insert() {
00243         typedef AssociativePrefixContainer<string_type> container_type;
00244         typedef PrefixTestData::prefix_vector_type vector_type;
00245         typedef vector_type::const_iterator const_iterator;
00246 
00247         const vector_type& prefix_vector(prefix_test_data.prefix_vector);
00248         container_type prefix_container_1;
00249         container_type prefix_container_2;
00250 
00251         prefix_container_1.assign(prefix_vector);
00252         const const_iterator& end(prefix_vector.end());
00253 
00254         for(const_iterator begin(prefix_vector.begin()); begin != end; ++begin) {
00255           prefix_container_2.insert(*begin);
00256         }
00257         std::set<string_type> prefix_set_1(prefix_container_1.begin(),prefix_container_1.end());
00258         std::set<string_type> prefix_set_2(prefix_container_2.begin(),prefix_container_2.end());
00259         if (not (prefix_set_1 == prefix_set_2))
00260           OKLIB_THROW("not (prefix_set_1 == prefix_set_2)");
00261 
00262       }
00263 
00264       // #############################
00265 
00281       void test_first_extension() {
00282         typedef AssociativePrefixContainer<string_type> prefix_container_type;
00283         typedef ExtensionTestData::const_iterator iterator;
00284         typedef prefix_container_type::const_iterator prefix_iterator_type;
00285 
00286         prefix_container_type prefix_container(prefix_test_data.prefix_vector);
00287         const iterator& end(extension_test_data.extension_vector.end());
00288 
00289         for (iterator begin(extension_test_data.extension_vector.begin()); begin != end; ++begin) {
00290           string_type header(extension_test_data.header(begin));
00291           if (extension_test_data.has_extension(begin)) {
00292             string_type expected_extension(extension_test_data.extension_value(begin));
00293             prefix_iterator_type first_extension(prefix_container.first_extension(header));
00294             prefix_iterator_type end(prefix_container.end());
00295             if (first_extension == end) 
00296               OKLIB_THROW("first_extension == end"); 
00297             OKLIB_TEST_EQUAL(*first_extension,expected_extension);
00298           }
00299           else {
00300             if (not (prefix_container.first_extension(header) == prefix_container.end()))
00301               OKLIB_THROW("not (prefix_container.first_extension(header) == prefix_container.end())");
00302           }
00303         }
00304       }
00305 
00306       // #############################
00307 
00331       void test_first_extension_uniqueness_checked() {
00332         typedef AssociativePrefixContainer<string_type> prefix_container_type;
00333         typedef ExtensionTestData::const_iterator iterator;
00334         typedef prefix_container_type::const_iterator prefix_iterator_type;
00335         typedef prefix_container_type::checked_iterator_type checked_iterator_type;
00336         prefix_container_type prefix_container(prefix_test_data.prefix_vector);
00337         const iterator& end(extension_test_data.extension_vector.end());
00338         for (iterator begin(extension_test_data.extension_vector.begin()); begin != end; ++begin) {
00339           string_type header(extension_test_data.header(begin));
00340           if (extension_test_data.has_extension(begin)) {
00341             string_type expected_extension(extension_test_data.extension_value(begin));
00342             checked_iterator_type first_extension(prefix_container.first_extension_uniqueness_checked(header));
00343             prefix_iterator_type end(prefix_container.end());
00344             if (first_extension.first == end)
00345               OKLIB_THROW("first_extension.first == end");
00346             OKLIB_TEST_EQUAL(*(first_extension.first),expected_extension);
00347             OKLIB_TEST_EQUAL(first_extension.second,extension_test_data.uniqueness(begin));
00348           }
00349           else {
00350             checked_iterator_type first_extension(prefix_container.first_extension_uniqueness_checked(header));
00351             if (not (first_extension.first == prefix_container.end()))
00352               OKLIB_THROW("not (first_extension == prefix_container.end())");
00353             OKLIB_TEST_EQUAL(first_extension.second,extension_test_data.uniqueness(begin));
00354           }
00355         }
00356         
00357       }
00358 
00359       // ##############################################################
00360 
00361       void perform_test_trivial() {
00362         test_assign_function();
00363         test_begin_end();
00364         test_insert();
00365         test_first_extension();
00366         test_first_extension_uniqueness_checked();
00367       }
00368      
00369     };   
00370   }  
00371 }
00372 
00373 #endif