Fixed whitespace in all files (no tabs)
[senf.git] / Socket / SocketPolicy.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 <boost/test/auto_unit_test.hpp>
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     struct SocketBufferingPolicy : public BufferingPolicyBase {};
60
61     template <class Policy>
62     struct ConvertibleValue
63     {
64         ConvertibleValue() {}
65         ConvertibleValue(ConvertibleValue const & other) {}
66
67         template <class OtherPolicy>
68         ConvertibleValue(ConvertibleValue<OtherPolicy> const & other,
69                          typename boost::enable_if< SocketPolicyIsBaseOf<Policy,OtherPolicy> >::type * = 0)
70             {}
71
72         ConvertibleValue const & operator=(ConvertibleValue const & other)
73             { return *this; }
74
75         template <class OtherPolicy>
76         typename boost::enable_if< SocketPolicyIsBaseOf<Policy,OtherPolicy>,
77                                    ConvertibleValue >::type const &
78         operator=(ConvertibleValue<OtherPolicy> const & other)
79             { return *this; }
80     };
81
82 }
83
84 BOOST_AUTO_UNIT_TEST(socketPolicy)
85 {
86     // All these checks are really compile-time checks ...
87
88     typedef MakeSocketPolicy<
89         UnixAddressingPolicy,
90         ConnectedCommunicationPolicy,
91         ReadablePolicy>::policy Policy1;
92
93     typedef SocketPolicy<
94         UnixAddressingPolicy,
95         UnspecifiedFramingPolicy,
96         ConnectedCommunicationPolicy,
97         ReadablePolicy,
98         UnspecifiedWritePolicy,
99         UnspecifiedBufferingPolicy> Policy2;
100
101     BOOST_MPL_ASSERT(( boost::is_same<Policy1,Policy2> ));
102
103     typedef MakeSocketPolicy<
104         Policy1,
105         UnspecifiedCommunicationPolicy>::policy Policy3;
106
107     typedef SocketPolicy<
108         UnixAddressingPolicy,
109         UnspecifiedFramingPolicy,
110         UnspecifiedCommunicationPolicy,
111         ReadablePolicy,
112         UnspecifiedWritePolicy,
113         UnspecifiedBufferingPolicy> Policy4;
114
115     BOOST_MPL_ASSERT(( boost::is_same<Policy3,Policy4> ));
116     BOOST_MPL_ASSERT_NOT(( boost::is_same<Policy1, Policy3> ));
117
118     BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy3,Policy1> ));
119     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
120     BOOST_MPL_ASSERT_NOT(( SocketPolicyIsBaseOf<Policy1,int> ));
121
122     // The following should fail at compile time
123     // BOOST_MPL_ASSERT(( SocketPolicyIsBaseOf<Policy1,Policy3> ));
124
125     ConvertibleValue<Policy1> p1;
126     ConvertibleValue<Policy3> p3(p1);
127
128     p3 = p1;
129     // The following should fail at compile time
130     // p1 = p3;
131 }
132
133 ///////////////////////////////cc.e////////////////////////////////////////
134 #undef prefix_
135
136 \f
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // c-file-style: "senf"
141 // indent-tabs-mode: nil
142 // ispell-local-dictionary: "american"
143 // End: