removed some useless spaces; not very important, I know :)
[senf.git] / Socket / SocketPolicy.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
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 // Unit tests
24
25 //#include "SocketPolicy.test.hh"
26 //#include "SocketPolicy.test.ih"
27
28 // Custom includes
29 #include "SocketPolicy.hh"
30 #include "SocketPolicy.test.hh"
31
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34 #include <boost/mpl/assert.hpp>
35 #include <boost/concept_check.hpp>
36 #include <boost/utility.hpp> // enable_if
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 using namespace senf;
42
43 namespace {
44     struct INetAddressingPolicy : public AddressingPolicyBase {};
45     struct UnixAddressingPolicy : public AddressingPolicyBase {};
46
47     struct StreamFramingPolicy : public FramingPolicyBase {};
48     struct DgramFramingPolicy : public FramingPolicyBase {};
49
50     struct ConnectedCommunicationPolicy : public CommunicationPolicyBase {};
51     struct UnconnectedCommunicationPolicy : public CommunicationPolicyBase {};
52
53     struct ReadablePolicy : public ReadPolicyBase {};
54     struct UnreadablePolicy : public ReadPolicyBase {};
55
56     struct WritablePolicy : public WritePolicyBase {};
57     struct UnwritablePolicy : public WritePolicyBase {};
58
59     template <class SPolicy>
60     struct ConvertibleValue
61     {
62         ConvertibleValue() {}
63         ConvertibleValue(ConvertibleValue const & other) {}
64
65         template <class OtherPolicy>
66         ConvertibleValue(ConvertibleValue<OtherPolicy> const & other,
67                          typename boost::enable_if< SocketPolicyIsBaseOf<SPolicy,OtherPolicy> >::type * = 0)
68             {}
69
70         ConvertibleValue const & operator=(ConvertibleValue const & other)
71             { return *this; }
72
73         template <class OtherPolicy>
74         typename boost::enable_if< SocketPolicyIsBaseOf<SPolicy,OtherPolicy>,
75                                    ConvertibleValue >::type const &
76         operator=(ConvertibleValue<OtherPolicy> const & other)
77             { return *this; }
78     };
79
80 }
81
82 BOOST_AUTO_UNIT_TEST(socketPolicy)
83 {
84     // Most of these checks are really compile-time checks ...
85
86     typedef MakeSocketPolicy<
87         UnixAddressingPolicy,
88         ConnectedCommunicationPolicy,
89         ReadablePolicy>::policy Policy1;
90
91     typedef SocketPolicy<
92         UnixAddressingPolicy,
93         UnspecifiedFramingPolicy,
94         ConnectedCommunicationPolicy,
95         ReadablePolicy,
96         UnspecifiedWritePolicy> Policy2;
97
98     BOOST_MPL_ASSERT(( boost::is_same<Policy1,Policy2> ));
99
100     typedef MakeSocketPolicy<
101         Policy1,
102         UnspecifiedCommunicationPolicy>::policy Policy3;
103
104     typedef SocketPolicy<
105         UnixAddressingPolicy,
106         UnspecifiedFramingPolicy,
107         UnspecifiedCommunicationPolicy,
108         ReadablePolicy,
109         UnspecifiedWritePolicy> Policy4;
110
111     BOOST_MPL_ASSERT(( boost::is_same<Policy3,Policy4> ));
112     BOOST_MPL_ASSERT_NOT(( boost::is_same<Policy1, Policy3> ));
113
114     BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy3,Policy1> ));
115     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
116     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,int> ));
117
118     // The following should fail at compile time
119     // BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
120
121     {
122         ConvertibleValue<Policy1> p1;
123         ConvertibleValue<Policy3> p3(p1);
124
125         p3 = p1;
126         // The following should fail at compile time
127         // p1 = p3;
128     }
129     
130     {
131         Policy1 p1;
132         Policy3 p3;
133
134         BOOST_CHECK_THROW( Policy1::checkBaseOf(p3), std::bad_cast );
135         BOOST_CHECK_NO_THROW( Policy3::checkBaseOf(p1) );
136     }
137 }
138
139 ///////////////////////////////cc.e////////////////////////////////////////
140 #undef prefix_
141
142 \f
143 // Local Variables:
144 // mode: c++
145 // fill-column: 100
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // comment-column: 40
151 // End: