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