OKlibrary  0.2.1.6
std_Basics_Tests.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 24.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 
00021 #ifndef STDBASICSTESTS_8455tGb
00022 #define STDBASICSTESTS_8455tGb
00023 
00024 #include <cassert>
00025 #include <functional>
00026 
00027 #include <OKlib/Programming/Utilities/OrderRelations/DerivedRelations.hpp>
00028 
00029 #include <OKlib/TestSystem/TestBaseClass.hpp>
00030 #include <OKlib/TestSystem/TestExceptions.hpp>
00031 
00032 namespace OKlib {
00033   namespace Concepts {
00034 
00042     template <typename T>
00043     struct EqualityComparable_Axiom_reflexivity : OKlib::TestSystem::Test {
00044       typedef EqualityComparable_Axiom_reflexivity test_type;
00045       EqualityComparable_Axiom_reflexivity(const T& a) : a(a) {}
00046     private :
00047       const T& a;
00048       void perform_test_trivial() {
00049         if (not (a == a))
00050           OKLIB_THROW("not a == a");
00051       }
00052     };
00053 
00062     template <typename T>
00063     struct EqualityComparable_Axiom_symmetry : OKlib::TestSystem::Test {
00064       typedef EqualityComparable_Axiom_symmetry test_type;
00065       EqualityComparable_Axiom_symmetry(const T& a, const T& b) : a(a), b(b) {}
00066     private :
00067       const T& a, b;
00068       void perform_test_trivial() {
00069         const bool ab = (a == b);
00070         const bool ba = (b == a);
00071         switch (ab) {
00072         case true :
00073           if (not ba)
00074             OKLIB_THROW("a == b and not b == a");
00075           break;
00076         case false :
00077           if (ba)
00078             OKLIB_THROW("not a == b and b == a");
00079           break;
00080         }
00081       }
00082     };
00083 
00096     template <typename T>
00097     struct EqualityComparable_Axiom_transitivity : OKlib::TestSystem::Test {
00098       typedef EqualityComparable_Axiom_transitivity test_type;
00099       EqualityComparable_Axiom_transitivity(const T& a, const T& b, const T& c) : a(a), b(b), c(c) {}
00100     private :
00101       const T& a, b, c;
00102       void perform_test_trivial() {
00103         const bool ab = (a == b);
00104         const bool ac = (a == c);
00105         const bool bc = (b == c);
00106         const int count_equal = ab + ac + bc;
00107         if (count_equal == 2)
00108           OKLIB_THROW("one equality missing");
00109       }
00110     };
00111 
00121     template <typename T>
00122     struct EqualityComparable_basic_test_one_object : OKlib::TestSystem::Test {
00123       typedef EqualityComparable_basic_test_one_object test_type;
00124       EqualityComparable_basic_test_one_object(const T& x) : x(x) {}
00125     private :
00126       const T& x;
00127       void perform_test_trivial() {
00128         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_Axiom_reflexivity<T>(x));
00129       }
00130     };
00131 
00139     template <typename T>
00140     struct EqualityComparable_basic_test_two_objects : OKlib::TestSystem::Test {
00141       typedef EqualityComparable_basic_test_two_objects test_type;
00142       EqualityComparable_basic_test_two_objects(const T& x, const T& y) : x(x), y(y) {}
00143     private :
00144       const T& x, y;
00145       void perform_test_trivial() {
00146         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_basic_test_one_object<T>(x));
00147         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_basic_test_one_object<T>(y));
00148         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_Axiom_symmetry<T>(x, y));
00149       }
00150     };
00151 
00159     template <typename T>
00160     struct EqualityComparable_basic_test_three_objects : OKlib::TestSystem::Test {
00161       typedef EqualityComparable_basic_test_three_objects test_type;
00162       EqualityComparable_basic_test_three_objects(const T& x, const T& y) : x(x), y(y), z(z) {}
00163     private :
00164       const T& x, y, z;
00165       void perform_test_trivial() {
00166         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_basic_test_two_objects<T>(x, y));
00167         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_basic_test_two_objects<T>(x, z));
00168         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_basic_test_two_objects<T>(y, z));
00169         OKLIB_TESTTRIVIAL_RETHROW(EqualityComparable_Axiom_transitivity<T>(x, y, z));
00170       }
00171     };
00172 
00173     // ############################################################
00174 
00182     template <typename T>
00183     struct LessThanComparable_Axiom_irreflexivity : OKlib::TestSystem::Test {
00184       typedef LessThanComparable_Axiom_irreflexivity test_type;
00185       LessThanComparable_Axiom_irreflexivity(const T& a) : a(a) {}
00186     private :
00187       const T& a;
00188       void perform_test_trivial() {
00189         if (a < a)
00190           OKLIB_THROW("a < a");
00191       }
00192     };
00193 
00201     template <typename T>
00202     struct LessThanComparable_Axiom_asymmetry : OKlib::TestSystem::Test {
00203       typedef LessThanComparable_Axiom_asymmetry test_type;
00204       LessThanComparable_Axiom_asymmetry(const T& a, const T& b) : a(a), b(b) {}
00205     private :
00206       const T& a, b;
00207       void perform_test_trivial() {
00208         if (a < b and b < a)
00209             OKLIB_THROW("a < b and b < a");
00210       }
00211     };
00212 
00223     template <typename T>
00224     struct LessThanComparable_Axiom_transitivity : OKlib::TestSystem::Test {
00225       typedef LessThanComparable_Axiom_transitivity test_type;
00226       LessThanComparable_Axiom_transitivity(const T& a, const T& b, const T& c) : a(a), b(b), c(c) {
00227         assert(a < b);
00228         assert(b < c);
00229       }
00230     private :
00231       const T& a, b, c;
00232       void perform_test_trivial() {
00233         if (not (a < c))
00234           OKLIB_THROW("not a < c");
00235       }
00236     };
00237 
00245     template <typename T>
00246     struct LessThanComparable_Axiom_equivalence_reflexivity : OKlib::TestSystem::Test {
00247       typedef LessThanComparable_Axiom_equivalence_reflexivity test_type;
00248       LessThanComparable_Axiom_equivalence_reflexivity(const T& a) : a(a) {}
00249     private :
00250       const T& a;
00251       typedef OKlib::OrderRelations::Equivalence<std::less<T> > equivalence;
00252       equivalence equivalent;
00253       void perform_test_trivial() {
00254         if (not equivalent(a,a))
00255           OKLIB_THROW("not a ~ a");
00256       }
00257     };
00258 
00267     template <typename T>
00268     struct LessThanComparable_Axiom_equivalence_symmetry : OKlib::TestSystem::Test {
00269       typedef LessThanComparable_Axiom_equivalence_symmetry test_type;
00270       LessThanComparable_Axiom_equivalence_symmetry(const T& a, const T& b) : a(a), b(b) {}
00271     private :
00272       const T& a, b;
00273       typedef OKlib::OrderRelations::Equivalence<std::less<T> > equivalence;
00274       equivalence equivalent;
00275       void perform_test_trivial() {
00276         const bool ab = equivalent(a,b);
00277         const bool ba = equivalent(b,a);
00278         switch (ab) {
00279         case true :
00280           if (not ba)
00281             OKLIB_THROW("a ~ b and not b ~ a");
00282           break;
00283         case false :
00284           if (ba)
00285             OKLIB_THROW("not a ~ b and b ~ a");
00286           break;
00287         }
00288       }
00289     };
00290 
00300     template <typename T>
00301     struct LessThanComparable_Axiom_equivalence_transitivity : OKlib::TestSystem::Test {
00302       typedef LessThanComparable_Axiom_equivalence_transitivity test_type;
00303       LessThanComparable_Axiom_equivalence_transitivity(const T& a, const T& b, const T& c) : a(a), b(b), c(c) {}
00304     private :
00305       const T& a, b, c;
00306       typedef OKlib::OrderRelations::Equivalence<std::less<T> > equivalence;
00307       equivalence equivalent;
00308       void perform_test_trivial() {
00309         if (equivalent(a,b) and equivalent(b,c) and not equivalent(a,c))
00310           OKLIB_THROW("a ~ b and b ~ c and not a ~ c");
00311       }
00312     };
00313 
00323     template <typename T>
00324     struct LessThanComparable_basic_test_one_object : OKlib::TestSystem::Test {
00325       typedef LessThanComparable_basic_test_one_object test_type;
00326       LessThanComparable_basic_test_one_object(const T& x) : x(x) {}
00327     private :
00328       const T& x;
00329       void perform_test_trivial() {
00330         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_Axiom_irreflexivity<T>(x));
00331       }
00332     };
00333 
00341     template <typename T>
00342     struct LessThanComparable_basic_test_two_objects : OKlib::TestSystem::Test {
00343       typedef LessThanComparable_basic_test_two_objects test_type;
00344       LessThanComparable_basic_test_two_objects(const T& x, const T& y) : x(x), y(y) {}
00345     private :
00346       const T& x, y;
00347       void perform_test_trivial() {
00348         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_basic_test_one_object<T>(x));
00349         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_basic_test_one_object<T>(y));
00350         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_Axiom_asymmetry<T>(x, y));
00351       }
00352     };
00353 
00364     template <typename T>
00365     struct LessThanComparable_basic_test_three_objects : OKlib::TestSystem::Test {
00366       typedef LessThanComparable_basic_test_three_objects test_type;
00367       LessThanComparable_basic_test_three_objects(const T& x, const T& y, const T& z) : x(x), y(y), z(z) {}
00368     private :
00369       const T& x, y, z;
00370       void perform_test_trivial() {
00371         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_basic_test_two_objects<T>(x, y));
00372         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_basic_test_two_objects<T>(x, z));
00373         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_basic_test_two_objects<T>(y, z));
00374         OKLIB_TESTTRIVIAL_RETHROW(LessThanComparable_Axiom_transitivity<T>(x, y, z));
00375       }
00376     };
00377 
00378 
00379   }
00380 
00381 }
00382 
00383 #endif