Packets/DefaultBundle: made length field in IPv6HopByHopOptionsPacketParser read...
[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     // We only test the user-collection case, all other cases are already handled by
44     // existing modules
45
46     // Primitive duplicator
47     class MyModule
48         : public module::Module,
49           public module::MultiConnectorMixin<MyModule, connector::ActiveOutput<>, void, void>
50     {
51         SENF_PPI_MODULE(MyModule);
52         typedef std::vector< boost::shared_ptr<MyModule::ConnectorType> > Connectors;
53     public:
54         connector::PassiveInput<> input;
55
56         MyModule()
57         {
58             noroute(input); 
59             input.onRequest(&MyModule::request);
60         }
61
62         Connectors const & connectors() const
63         { return connectors_; }
64
65     private:
66         void connectorSetup(std::auto_ptr<ConnectorType> c)
67         {
68             route(input, *c);
69             connectors_.push_back(boost::shared_ptr<ConnectorType>(c));
70         }
71
72         void connectorDestroy(ConnectorType const * c)
73         {
74             Connectors::iterator i (
75                 std::find_if(connectors_.begin(), connectors_.end(), 
76                              boost::bind(&Connectors::value_type::get,_1) == c));
77             if (i != connectors_.end())
78                 connectors_.erase(i);
79         }
80
81         void request()
82         {
83             senf::Packet p (input());
84             for (Connectors::iterator i (connectors_.begin()), i_end (connectors_.end());
85                     i != i_end; ++i)
86                 (**i)(p);
87         }
88
89         Connectors connectors_;
90                 
91         friend class module::MultiConnectorMixin<MyModule, connector::ActiveOutput<>, void, void>;
92     };
93         
94     struct IntAnnotation {
95         int value;
96         bool operator<(IntAnnotation const & other) const { return value < other.value; }
97         IntAnnotation() {}
98         IntAnnotation(int v) : value(v) {}
99     };
100
101     std::ostream & operator<<(std::ostream & os, IntAnnotation const & value)
102     { os << value.value; return os; }
103 }
104
105 BOOST_AUTO_UNIT_TEST(multiConnectorMixin_userContainer)
106 {
107     debug::ActiveSource source;
108     MyModule module;
109     debug::PassiveSink sink1;
110     debug::PassiveSink sink2;
111
112     ppi::connect(source, module);
113     ppi::connect(module, sink1);
114     ppi::connect(module, sink2);
115     ppi::init();
116
117     senf::Packet p (senf::DataPacket::create());
118
119     source.submit(p);
120     BOOST_CHECK_EQUAL( sink1.size(), 1u );
121     BOOST_CHECK_EQUAL( sink2.size(), 1u );
122     BOOST_CHECK( sink1.pop_front() == p );
123     BOOST_CHECK( sink2.pop_front() == p );
124
125     BOOST_CHECK_EQUAL( module.connectors().size(), 2u );
126     sink1.input.disconnect();
127     BOOST_CHECK_EQUAL( module.connectors().size(), 1u );
128 }
129
130 BOOST_AUTO_UNIT_TEST(multiConnectorMixin_multipleModules)
131 {
132     debug::ActiveSource source;
133     debug::PassiveSink sink;
134     module::PassiveJoin join1;
135     module::PassiveJoin join2;
136     module::AnnotationRouter<IntAnnotation> router;
137     MyModule module;
138     
139     ppi::connect(source, join1);
140     ppi::connect(join1, router);
141     ppi::connect(router, 1, join2);
142     ppi::connect(join2, module);
143     ppi::connect(module, sink);
144     
145     senf::Packet p (senf::DataPacket::create());
146     p.annotation<IntAnnotation>().value = 1;
147
148     source.submit(p);
149     BOOST_CHECK_EQUAL( sink.size(), 1u );
150     BOOST_CHECK( sink.pop_front() == p );
151 }
152
153 ///////////////////////////////cc.e////////////////////////////////////////
154 #undef prefix_
155
156 \f
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // End: