98dd09e9f4ebd07d5c577e39c3280d69c724097e
[senf.git] / senf / Utils / mpl.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief mpl unit tests */
25
26 //#include "mpl.test.hh"
27 //#include "mpl.test.ih"
28
29 // Custom includes
30 #include "mpl.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39
40     template <unsigned _>
41     struct select {
42         static bool const has_int_value = false;
43         static bool const has_class_value = false;
44     };
45     template <class T>
46     senf::mpl::rv<0> select_(...);
47
48
49     template <>
50     struct select<1> {
51         static bool const has_int_value = true;
52         static bool const has_class_value = false;
53     };
54     template <class T>
55     senf::mpl::rv<1> select_(int, senf::mpl::take_int<T::value> * = 0);
56
57     template <>
58     struct select<2> {
59         static bool const has_int_value = false;
60         static bool const has_class_value = true;
61     };
62     template <class T>
63     senf::mpl::rv<2> select_(int, senf::mpl::take_class<typename T::value> * = 0);
64
65     template <class T>
66     struct choice : public select<SENF_MPL_RV( select_<T>(0) )> {};
67
68     struct A { static const int value = 0; };
69     struct B { struct value {}; };
70     struct C {};
71 }
72
73 SENF_AUTO_UNIT_TEST(senfmpl)
74 {
75     BOOST_CHECK( choice<A>::has_int_value );
76     BOOST_CHECK( ! choice<A>::has_class_value );
77
78     BOOST_CHECK( ! choice<B>::has_int_value );
79     BOOST_CHECK( choice<B>::has_class_value );
80
81     BOOST_CHECK( ! choice<C>::has_int_value );
82     BOOST_CHECK( ! choice<C>::has_class_value );
83 }
84
85 namespace {
86
87 #   define Begin() SENF_MPL_SLOT_DEF(accum, 0)
88 #   define Add(n) SENF_MPL_SLOT_SET(accum, SENF_MPL_SLOT_GET(accum)+n)
89 #   define End() BOOST_STATIC_CONSTANT(unsigned, total = SENF_MPL_SLOT_GET(accum))
90
91     struct Test
92     {
93         Begin();
94
95         Add(2);
96         Add(2);
97
98         // Here some comment ...
99
100         Add(4);
101
102         End();
103     };
104
105 }
106
107 SENF_AUTO_UNIT_TEST(mplSlot)
108 {
109     BOOST_CHECK_EQUAL( unsigned(Test::total), 8u );
110 }
111
112 ///////////////////////////////cc.e////////////////////////////////////////
113 #undef prefix_
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: