Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[senf.git] / senf / PPI / MultiConnectorMixin.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 MultiConnectorMixin.test unit tests */
25
26 //#include "MultiConnectorMixin.test.hh"
27 //#include "MultiConnectorMixin.test.ih"
28
29 // Custom includes
30 #include "PPI.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 namespace ppi = senf::ppi;
38 namespace connector = ppi::connector;
39 namespace module = ppi::module;
40 namespace debug = module::debug;
41
42 namespace {
43
44     // Primitive join
45     class SequenceContainerModule
46         : public module::Module,
47           public module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> >
48     {
49         SENF_PPI_MODULE(SequenceContainerModule);
50         typedef module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> > base;
51
52     public:
53         connector::ActiveOutput<> output;
54
55         using base::connectors;
56
57         SequenceContainerModule() : count (0u)
58         {
59             noroute(output);
60         }
61
62         unsigned count;
63
64     private:
65         void connectorSetup(connector::PassiveInput<> & c)
66         {
67             route(c, output);
68             c.onRequest(boost::bind(&SequenceContainerModule::request, this, boost::ref(c)));
69             ++ count;
70         }
71
72         void connectorDestroy(connector::PassiveInput<> const & c)
73         {
74             -- count;
75         }
76
77         void request(connector::PassiveInput<> & c)
78         {
79             output(c());
80         }
81
82         friend class module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> >;
83     };
84
85     // Primitive duplicator
86     class UserContainerModule
87         : public module::Module,
88           public module::MultiConnectorMixin<UserContainerModule, connector::ActiveOutput<>, void, void>
89     {
90         SENF_PPI_MODULE(UserContainerModule);
91         typedef std::vector< boost::shared_ptr<UserContainerModule::ConnectorType> > Connectors;
92     public:
93         connector::PassiveInput<> input;
94
95         UserContainerModule()
96         {
97             noroute(input);
98             input.onRequest(&UserContainerModule::request);
99         }
100
101         Connectors const & connectors() const
102         { return connectors_; }
103
104     private:
105         void connectorSetup(std::auto_ptr<ConnectorType> c)
106         {
107             route(input, *c);
108             connectors_.push_back(boost::shared_ptr<ConnectorType>(c));
109         }
110
111         void connectorDestroy(ConnectorType const * c)
112         {
113             Connectors::iterator i (
114                 std::find_if(connectors_.begin(), connectors_.end(),
115                              boost::bind(&Connectors::value_type::get,_1) == c));
116             if (i != connectors_.end())
117                 connectors_.erase(i);
118         }
119
120         void request()
121         {
122             senf::Packet p (input());
123             for (Connectors::iterator i (connectors_.begin()), i_end (connectors_.end());
124                     i != i_end; ++i)
125                 (**i)(p);
126         }
127
128         Connectors connectors_;
129
130         friend class module::MultiConnectorMixin<UserContainerModule, connector::ActiveOutput<>, void, void>;
131     };
132
133     struct IntAnnotation {
134         int value;
135         bool operator<(IntAnnotation const & other) const { return value < other.value; }
136         IntAnnotation() {}
137         IntAnnotation(int v) : value(v) {}
138     };
139
140     std::ostream & operator<<(std::ostream & os, IntAnnotation const & value)
141     { os << value.value; return os; }
142 }
143
144 SENF_AUTO_UNIT_TEST(multiConnectorMixin_sequenceContainer)
145 {
146     debug::ActiveSource source1;
147     debug::ActiveSource source2;
148     SequenceContainerModule module;
149     debug::PassiveSink sink;
150
151     ppi::connect(source1, module);
152     ppi::connect(source2, module);
153     ppi::connect(module, sink);
154     ppi::init();
155
156     senf::Packet p (senf::DataPacket::create());
157
158     source1.submit(p);
159     BOOST_CHECK_EQUAL( sink.size(), 1u );
160     BOOST_CHECK( sink.pop_front() == p );
161     source2.submit(p);
162     BOOST_CHECK_EQUAL( sink.size(), 1u );
163     BOOST_CHECK( sink.pop_front() == p );
164
165     BOOST_CHECK_EQUAL( module.connectors().size(), 2u );
166     BOOST_CHECK_EQUAL( module.count, 2u );
167     source1.output.disconnect();
168     BOOST_CHECK_EQUAL( module.connectors().size(), 1u );
169     BOOST_CHECK_EQUAL( module.count, 1u );
170 }
171
172 SENF_AUTO_UNIT_TEST(multiConnectorMixin_userContainer)
173 {
174     debug::ActiveSource source;
175     UserContainerModule module;
176     debug::PassiveSink sink1;
177     debug::PassiveSink sink2;
178
179     ppi::connect(source, module);
180     ppi::connect(module, sink1);
181     ppi::connect(module, sink2);
182     ppi::init();
183
184     senf::Packet p (senf::DataPacket::create());
185
186     source.submit(p);
187     BOOST_CHECK_EQUAL( sink1.size(), 1u );
188     BOOST_CHECK_EQUAL( sink2.size(), 1u );
189     BOOST_CHECK( sink1.pop_front() == p );
190     BOOST_CHECK( sink2.pop_front() == p );
191
192     BOOST_CHECK_EQUAL( module.connectors().size(), 2u );
193     sink1.input.disconnect();
194     BOOST_CHECK_EQUAL( module.connectors().size(), 1u );
195 }
196
197 SENF_AUTO_UNIT_TEST(multiConnectorMixin_multipleModules)
198 {
199     debug::ActiveSource source;
200     debug::PassiveSink sink;
201     module::PassiveJoin join1;
202     module::PassiveJoin join2;
203     module::AnnotationRouter<IntAnnotation> router;
204     UserContainerModule module;
205
206     ppi::connect(source, join1);
207     ppi::connect(join1, router);
208     ppi::connect(router, 1, join2);
209     ppi::connect(join2, module);
210     ppi::connect(module, sink);
211
212     senf::Packet p (senf::DataPacket::create());
213     p.annotation<IntAnnotation>().value = 1;
214
215     source.submit(p);
216     BOOST_CHECK_EQUAL( sink.size(), 1u );
217     BOOST_CHECK( sink.pop_front() == p );
218 }
219
220 ///////////////////////////////cc.e////////////////////////////////////////
221 #undef prefix_
222
223 \f
224 // Local Variables:
225 // mode: c++
226 // fill-column: 100
227 // comment-column: 40
228 // c-file-style: "senf"
229 // indent-tabs-mode: nil
230 // ispell-local-dictionary: "american"
231 // compile-command: "scons -u test"
232 // End: