switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / SocketPolicy.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief SocketPolicy unit tests */
30
31 #include "SocketPolicy.test.hh"
32 //#include "SocketPolicy.test.ih"
33
34 // Custom includes
35 #include <boost/mpl/assert.hpp>
36 #include <boost/concept_check.hpp>
37 #include <boost/utility.hpp> // enable_if
38
39 #include "SocketPolicy.hh"
40
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46
47 using namespace senf;
48
49 namespace {
50     struct INetAddressingPolicy : public AddressingPolicyBase {};
51     struct UnixAddressingPolicy : public AddressingPolicyBase {};
52
53     struct StreamFramingPolicy : public FramingPolicyBase {};
54     struct DgramFramingPolicy : public FramingPolicyBase {};
55
56     struct ConnectedCommunicationPolicy : public CommunicationPolicyBase {};
57     struct UnconnectedCommunicationPolicy : public CommunicationPolicyBase {};
58
59     struct ReadablePolicy : public ReadPolicyBase {};
60     struct UnreadablePolicy : public ReadPolicyBase {};
61
62     struct WritablePolicy : public WritePolicyBase {};
63     struct UnwritablePolicy : public WritePolicyBase {};
64
65     template <class SPolicy>
66     struct ConvertibleValue
67     {
68         ConvertibleValue() {}
69         ConvertibleValue(ConvertibleValue const & other) {}
70
71         template <class OtherPolicy>
72         ConvertibleValue(ConvertibleValue<OtherPolicy> const & other,
73                          typename boost::enable_if< SocketPolicyIsBaseOf<SPolicy,OtherPolicy> >::type * = 0)
74             {}
75
76         ConvertibleValue const & operator=(ConvertibleValue const & other)
77             { return *this; }
78
79         template <class OtherPolicy>
80         typename boost::enable_if< SocketPolicyIsBaseOf<SPolicy,OtherPolicy>,
81                                    ConvertibleValue >::type const &
82         operator=(ConvertibleValue<OtherPolicy> const & other)
83             { return *this; }
84     };
85
86 }
87
88 SENF_AUTO_UNIT_TEST(socketPolicy)
89 {
90     // Most of these checks are really compile-time checks ...
91
92     typedef MakeSocketPolicy<
93         UnixAddressingPolicy,
94         ConnectedCommunicationPolicy,
95         ReadablePolicy>::policy Policy1;
96
97     typedef SocketPolicy<
98         UnixAddressingPolicy,
99         UnspecifiedFramingPolicy,
100         ConnectedCommunicationPolicy,
101         ReadablePolicy,
102         UnspecifiedWritePolicy> Policy2;
103
104     BOOST_MPL_ASSERT(( boost::is_same<Policy1,Policy2> ));
105
106     typedef MakeSocketPolicy<
107         Policy1,
108         UnspecifiedCommunicationPolicy>::policy Policy3;
109
110     typedef SocketPolicy<
111         UnixAddressingPolicy,
112         UnspecifiedFramingPolicy,
113         UnspecifiedCommunicationPolicy,
114         ReadablePolicy,
115         UnspecifiedWritePolicy> Policy4;
116
117     BOOST_MPL_ASSERT(( boost::is_same<Policy3,Policy4> ));
118     BOOST_MPL_ASSERT_NOT(( boost::is_same<Policy1, Policy3> ));
119
120     BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy3,Policy1> ));
121     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
122     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,int> ));
123
124     // The following should fail at compile time
125     // BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
126
127     {
128         ConvertibleValue<Policy1> p1;
129         ConvertibleValue<Policy3> p3(p1);
130
131         p3 = p1;
132         // The following should fail at compile time
133         // p1 = p3;
134     }
135
136     {
137         Policy1 p1;
138         Policy3 p3;
139
140         BOOST_CHECK_THROW( Policy1::checkBaseOf(p3), std::bad_cast );
141         SENF_CHECK_NO_THROW( Policy3::checkBaseOf(p1) );
142     }
143 }
144
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 #undef prefix_
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"
156 // comment-column: 40
157 // End: