removed some tabs
[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 }
59
60 SENF_PACKET_REGISTRY_REGISTER(StringTag, "foo", FooPacket);
61 SENF_PACKET_REGISTRY_REGISTER(StringTag, "bar", BarPacket);
62
63 BOOST_AUTO_UNIT_TEST(packetRegistry_test)
64 {
65     PacketRegistry<BaseTag>::registerPacket<FooPacket>(1u);
66     PacketRegistry<BaseTag>::registerPacket<BarPacket>(2u);
67
68     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<FooPacket>(), 1u );
69     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<BarPacket>(), 2u );
70     BOOST_CHECK_THROW( PacketRegistry<BaseTag>::key<OtherPacket>(), 
71                        PacketTypeNotRegisteredException );
72
73     BOOST_CHECK_EQUAL( PacketRegistry<StringTag>::key<FooPacket>(), "foo" );
74     BOOST_CHECK( ! PacketRegistry<StringTag>::lookup("blub", senf::nothrow) );
75     BOOST_CHECK( PacketRegistry<BaseTag>::lookup(1u, senf::nothrow) );
76
77     unsigned elts1[] = { 1u, 2u };
78     BOOST_CHECK_EQUAL_COLLECTIONS( PacketRegistry<BaseTag>::begin(), PacketRegistry<BaseTag>::end(),
79                                    elts1+0, elts1+sizeof(elts1)/sizeof(elts1[0]) );
80
81     std::string elts2[] = { "bar", "foo" };
82     BOOST_CHECK_EQUAL_COLLECTIONS( PacketRegistry<StringTag>::begin(), PacketRegistry<StringTag>::end(),
83                                    elts2+0, elts2+sizeof(elts2)/sizeof(elts2[0]) );
84
85     std::stringstream s;
86     senf::dumpPacketRegistries(s);
87     BOOST_CHECK_EQUAL( s.str(),
88                        "(anonymous namespace)::BaseTag:\n"
89                        "1 senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
90                        "2 senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
91                        "\n"
92                        "(anonymous namespace)::RegTag:\n"
93                        "1 senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
94                        "2 senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
95                        "\n"
96                        "(anonymous namespace)::StringTag:\n"
97                        "bar senf::ConcretePacket<(anonymous namespace)::BarPacketType>\n"
98                        "foo senf::ConcretePacket<(anonymous namespace)::FooPacketType>\n"
99                        "\n" );
100
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: