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