Packets: Add PacketRegistry::begin()/end() and senf::dumpPacketRegistries() utility
[senf.git] / Packets / PacketRegistry.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
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 // Unit tests
24
25 //#include "PacketRegistry.test.hh"
26 //#include "PacketRegistry.test.ih"
27
28 // Custom includes
29 #include <string>
30 #include <sstream>
31 #include "Packets.hh"
32
33 #include "../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40
41     using namespace senf;
42
43     struct BaseTag {
44         typedef unsigned key_t;
45     };
46
47     struct StringTag {
48         typedef std::string key_t;
49     };
50
51     struct FooPacketType : public PacketTypeBase {};
52     typedef senf::ConcretePacket<FooPacketType> FooPacket;
53     struct BarPacketType : public PacketTypeBase {};
54     typedef senf::ConcretePacket<BarPacketType> BarPacket;
55     struct OtherPacketType : public PacketTypeBase {};
56     typedef senf::ConcretePacket<OtherPacketType> OtherPacket;
57
58     namespace reg {
59         PacketRegistry<StringTag>::RegistrationProxy<FooPacket> registerFoo ("foo");
60         PacketRegistry<StringTag>::RegistrationProxy<BarPacket> registerBar ("bar");
61     }
62
63 }
64
65 BOOST_AUTO_UNIT_TEST(packetRegistry_test)
66 {
67     PacketRegistry<BaseTag>::registerPacket<FooPacket>(1u);
68     PacketRegistry<BaseTag>::registerPacket<BarPacket>(2u);
69
70     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<FooPacket>(), 1u );
71     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<BarPacket>(), 2u );
72     BOOST_CHECK_THROW( PacketRegistry<BaseTag>::key<OtherPacket>(), 
73                        PacketTypeNotRegisteredException );
74
75     BOOST_CHECK_EQUAL( PacketRegistry<StringTag>::key<FooPacket>(), "foo" );
76     BOOST_CHECK( ! PacketRegistry<StringTag>::lookup("blub", senf::nothrow) );
77     BOOST_CHECK( PacketRegistry<BaseTag>::lookup(1u, senf::nothrow) );
78
79     unsigned elts1[] = { 1u, 2u };
80     BOOST_CHECK_EQUAL_COLLECTIONS( PacketRegistry<BaseTag>::begin(), PacketRegistry<BaseTag>::end(),
81                                    elts1+0, elts1+sizeof(elts1)/sizeof(elts1[0]) );
82
83     std::string elts2[] = { "bar", "foo" };
84     BOOST_CHECK_EQUAL_COLLECTIONS( PacketRegistry<StringTag>::begin(), PacketRegistry<StringTag>::end(),
85                                    elts2+0, elts2+sizeof(elts2)/sizeof(elts2[0]) );
86
87     std::stringstream s;
88     senf::dumpPacketRegistries(s);
89     BOOST_CHECK_EQUAL( s.str(),
90                        "(anonymous namespace)::BaseTag:\n"
91                        "1 senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
92                        "2 senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
93                        "\n"
94                        "(anonymous namespace)::RegTag:\n"
95                        "1 senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
96                        "2 senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
97                        "\n"
98                        "(anonymous namespace)::StringTag:\n"
99                        "bar senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
100                        "foo senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
101                        "\n" );
102
103 }
104
105 ///////////////////////////////cc.e////////////////////////////////////////
106 #undef prefix_
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // c-file-style: "senf"
113 // indent-tabs-mode: nil
114 // ispell-local-dictionary: "american"
115 // compile-command: "scons -u test"
116 // comment-column: 40
117 // End: