OKlibrary  0.2.1.6
Variables_Tests.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 26.5.2005 (Swansea)
00002 /* Copyright 2005 - 2007 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 
00020 #ifndef VARIABLESTESTS_kkgFre3
00021 #define VARIABLESTESTS_kkgFre3
00022 
00023 #include <OKlib/TestSystem/TestBaseClass.hpp>
00024 #include <OKlib/TestSystem/TestExceptions.hpp>
00025 
00026 #include <OKlib/Concepts/LibraryBasics.hpp>
00027 #include <OKlib/Concepts/Basics_Tests.hpp>
00028 
00029 #include <OKlib/traits/TypeTraits.hpp>
00030 
00031 #include <OKlib/Concepts/Variables.hpp>
00032 #include <OKlib/Satisfiability/ProblemInstances/Variables/traits/index_type.hpp>
00033 
00034 namespace OKlib {
00035   namespace Concepts {
00036 
00053     template <typename Var>
00054     struct Variables_Axiom_singular_variables_false : OKlib::TestSystem::Test {
00055       typedef Variables_Axiom_singular_variables_false test_type;
00056     private :
00057       void perform_test_trivial() {
00058         if (Var())
00059           OKLIB_THROW("not not bool(Var())");
00060       }
00061     };
00062 
00063     template <typename Var>
00064     struct Variables_Axiom_singular_variables_identity : OKlib::TestSystem::Test {
00065       typedef Variables_Axiom_singular_variables_identity test_type;
00066       Variables_Axiom_singular_variables_identity(const Var& v, const Var& w) : v(v), w(w) {
00067         assert(not v);
00068         assert(not w);
00069       }
00070     private :
00071       const Var& v; const Var& w;
00072       void perform_test_trivial() {
00073         if (v != w)
00074           OKLIB_THROW("v != w");
00075       }
00076     };
00077 
00085     template <typename Var>
00086     struct Variables_basic_test : OKlib::TestSystem::TestBase {
00087       typedef Variables_basic_test test_type;
00088       Variables_basic_test() {
00089         insert(this);
00090       }
00091     private :
00092       void perform_test_trivial() {
00093         OKLIB_MODELS_CONCEPT_REQUIRES(Var, OKlib::Concepts::Variables);
00094         OKLIB_MODELS_CONCEPT_TAG(Var, OKlib::Concepts::Variables);
00095         OKLIB_TESTTRIVIAL_RETHROW(LinearOrder_basic_test_one_object<Var>(Var()));
00096         OKLIB_TESTTRIVIAL_RETHROW(Variables_Axiom_singular_variables_false<Var>());
00097         OKLIB_TESTTRIVIAL_RETHROW(Variables_Axiom_singular_variables_identity<Var>(Var(), Var()));
00098       }
00099     };
00100 
00101     // ---------------------------------------------------------------------------------------------------------------------
00102 
00109     template <typename Var>
00110     struct VariablesWithIndex_Axiom_index_nonnegative : OKlib::TestSystem::Test {
00111       typedef VariablesWithIndex_Axiom_index_nonnegative test_type;
00112       VariablesWithIndex_Axiom_index_nonnegative(const Var& v) : v(v) {}
00113     private :
00114       const Var& v;
00115       typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00116       void perform_test_trivial() {
00117         if (index_type(v) < 0)
00118           OKLIB_THROW("index(v) < 0");
00119       }
00120     };
00121 
00128     template <typename Var>
00129     struct VariablesWithIndex_Axiom_index_zero_positive : OKlib::TestSystem::Test {
00130       typedef VariablesWithIndex_Axiom_index_zero_positive test_type;
00131       VariablesWithIndex_Axiom_index_zero_positive(const Var& v) : v(v) {
00132         assert(not v);
00133       }
00134     private :
00135       const Var& v;
00136       typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00137       void perform_test_trivial() {
00138         if (index_type(v) != 0)
00139           OKLIB_THROW("index(v) != 0");
00140       }
00141     };
00142 
00149     template <typename Var>
00150     struct VariablesWithIndex_Axiom_index_zero_negative : OKlib::TestSystem::Test {
00151       typedef VariablesWithIndex_Axiom_index_zero_negative test_type;
00152       VariablesWithIndex_Axiom_index_zero_negative(const Var& v) : v(v) {
00153         typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00154         assert(index_type(v) == 0);
00155       }
00156     private :
00157       const Var& v;
00158       void perform_test_trivial() {
00159         if (bool(v))
00160           OKLIB_THROW("not not bool(v)");
00161       }
00162     };
00163 
00173     template <typename Var>
00174     struct VariablesWithIndex_Axiom_index_identity : OKlib::TestSystem::Test {
00175       typedef VariablesWithIndex_Axiom_index_identity test_type;
00176       VariablesWithIndex_Axiom_index_identity(const Var& v, const Var& w) : v(v), w(w) {
00177         typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00178         assert(index_type(v) == index_type(w));
00179       }
00180     private :
00181       const Var& v; const Var& w;
00182       void perform_test_trivial() {
00183         if (v != w)
00184           OKLIB_THROW("v != w");
00185       }
00186     };
00187 
00198     template <typename Var>
00199     struct VariablesWithIndex_basic_test : OKlib::TestSystem::TestBase {
00200       typedef VariablesWithIndex_basic_test test_type;
00201       VariablesWithIndex_basic_test() {
00202         insert(this);
00203       }
00204     private :
00205       void perform_test_trivial() {
00206         OKLIB_MODELS_CONCEPT_REQUIRES(Var, OKlib::Concepts::VariablesWithIndex);
00207         OKLIB_MODELS_CONCEPT_TAG(Var, OKlib::Concepts::VariablesWithIndex);
00208         OKLIB_TESTTRIVIAL_RETHROW(Variables_basic_test<Var>());
00209         OKLIB_TESTTRIVIAL_RETHROW(VariablesWithIndex_Axiom_index_zero_positive<Var>(Var()));
00210       }
00211     };
00212 
00213     // ---------------------------------------------------------------------------------------------------------------------
00214 
00215     namespace TestIndex {
00216       template <typename index_type, bool is_signed>
00217       struct assert_index {
00218         assert_index(const index_type& i) {}
00219       };
00220       template <typename index_type>
00221       struct assert_index<index_type, true> {
00222         assert_index(const index_type& i) {
00223           assert(i >= 0);
00224         }
00225       };
00226       template <typename index_type>
00227       void assert_ge0(const index_type& i) {
00228         (assert_index<index_type, OKlib::traits::is_signed_integral<index_type>::value>(i));
00229       }
00230     }
00231     
00238     template <typename Var>
00239     struct VariablesAsIndex_Axiom : OKlib::TestSystem::Test {
00240       typedef VariablesAsIndex_Axiom test_type;
00241       typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00242       VariablesAsIndex_Axiom(const index_type& i) : i(i) {
00243         TestIndex::assert_ge0(i);
00244       }
00245     private :
00246       const index_type& i;
00247       void perform_test_trivial() {
00248         if (index_type(Var(i)) != i)
00249           OKLIB_THROW("index(Var(i)) != i");
00250       }
00251     };
00252 
00260     template <typename Var>
00261     struct VariablesAsIndex_basic_test : OKlib::TestSystem::TestBase {
00262       typedef VariablesAsIndex_basic_test test_type;
00263       VariablesAsIndex_basic_test() {
00264         insert(this);
00265       }
00266     private :
00267       typedef typename OKlib::Variables::traits::index_type<Var>::type index_type;
00268       void perform_test_trivial() {
00269         OKLIB_MODELS_CONCEPT_REQUIRES(Var, OKlib::Concepts::VariablesAsIndex);
00270         OKLIB_MODELS_CONCEPT_TAG(Var, OKlib::Concepts::VariablesAsIndex);
00271         OKLIB_TESTTRIVIAL_RETHROW(VariablesWithIndex_basic_test<Var>());
00272         {
00273           const index_type max_index = 2;
00274           for (index_type i = 0; i <= max_index; ++i)
00275             OKLIB_TESTTRIVIAL_RETHROW(VariablesAsIndex_Axiom<Var>(i));
00276         }
00277         // XXXXXXXXXXXXXXXXXXXXXXXXXX
00278       }
00279     };
00280 
00281   }
00282 
00283 }
00284 
00285 #endif