switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / AnnotationRouter.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
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 AnnotationRouter unit tests */
30
31 //#include "AnnotationRouter.test.hh"
32 //#include "AnnotationRouter.test.ih"
33
34 // Custom includes
35 #include "AnnotationRouter.hh"
36 #include "DebugModules.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 namespace ppi = senf::ppi;
44 namespace connector = ppi::connector;
45 namespace module = ppi::module;
46 namespace debug = module::debug;
47
48 namespace {
49     struct IntAnnotation {
50         int value;
51         bool operator<(IntAnnotation const & other) const { return value < other.value; }
52         bool operator==(IntAnnotation const & other) const { return value == other.value; }
53         IntAnnotation() {}
54         IntAnnotation(int v) : value(v) {}
55         IntAnnotation & operator=(int v) { value=v; return *this; }
56         operator int () const { return value; }
57     };
58
59     std::ostream & operator<<(std::ostream & os, IntAnnotation const & value)
60     { os << value.value; return os; }
61
62     struct AnnotationRouter : public module::AnnotationRouter<IntAnnotation>
63     {
64         using module::AnnotationRouter<IntAnnotation>::connectors;
65     };
66 }
67
68 SENF_AUTO_UNIT_TEST(annotationRouter)
69 {
70     debug::ActiveSource source;
71     debug::PassiveSink sink1;
72     debug::PassiveSink sink2;
73
74     AnnotationRouter router;
75
76     ppi::connect(source, router);
77     ppi::connect(router, 1, sink1);
78     ppi::connect(router, 2, sink2);
79
80     BOOST_CHECK_THROW( connect(router, 2, sink2),
81             module::AnnotationRouter<IntAnnotation>::DuplicateKeyException);
82
83     ppi::init();
84
85     senf::Packet p1 (senf::DataPacket::create());
86     p1.annotation<IntAnnotation>() = 1;
87     senf::Packet p2 (senf::DataPacket::create());
88     p2.annotation<IntAnnotation>() = 2;
89
90     source.submit(p1);
91     source.submit(p2);
92     source.submit(senf::DataPacket::create());
93
94     BOOST_CHECK_EQUAL( sink1.size(), 1u );
95     BOOST_CHECK_EQUAL( sink2.size(), 1u );
96     BOOST_CHECK( sink1.front() == p1 );
97     BOOST_CHECK( sink2.front() == p2 );
98     BOOST_CHECK_EQUAL(router.connectors().size(), 2u);
99
100     sink1.input.disconnect();
101     BOOST_CHECK_EQUAL(router.connectors().size(), 1u);
102
103     senf::ppi::connect(router, 1, sink1);
104     senf::ppi::init();
105     source.submit(p1);
106
107     BOOST_CHECK_EQUAL( sink1.size(), 2u );
108     BOOST_CHECK_EQUAL( sink2.size(), 1u );
109 }
110
111 //-/////////////////////////////////////////////////////////////////////////////////////////////////
112 #undef prefix_
113
114 \f
115 // Local Variables:
116 // mode: c++
117 // fill-column: 100
118 // comment-column: 40
119 // c-file-style: "senf"
120 // indent-tabs-mode: nil
121 // ispell-local-dictionary: "american"
122 // compile-command: "scons -u test"
123 // End: