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