Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Packets / VariantParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 VariantParser unit tests */
25
26 //#include "VariantParser.test.hh"
27 //#include "VariantParser.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 SENF_AUTO_UNIT_TEST(VariantParser)
39 {
40     typedef senf::ArrayParser<10, senf::UInt8Parser> Array10;
41     typedef senf::VariantParser< senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1>,
42         boost::mpl::vector<senf::VoidPacketParser, Array10, senf:: UInt32Parser> > Variant;
43
44     unsigned char data[] = { 0x01, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
45                              0x19, 0x1A, 0x1B };
46     senf::DataPacket p (senf::DataPacket::create(data));
47     {
48         Variant v ( boost::next(p.data().begin()), & p.data() );
49         BOOST_REQUIRE_EQUAL( v.variant(), 1u );
50         BOOST_CHECK_EQUAL( senf::bytes(v), 10u );
51         BOOST_CHECK_EQUAL_COLLECTIONS( v.get<1>().begin(), v.get<1>().end(),
52                                        data+1, data+11 );
53         v.init();
54         // The container size should not change in this case but now variant should be 0 ...
55         BOOST_REQUIRE_EQUAL( p.data().size(), 11u );
56         BOOST_REQUIRE_EQUAL( v.variant(), 0u );
57         BOOST_CHECK_EQUAL( senf::bytes(v), 0u );
58
59         v.init<2>();
60         // v invalidated
61     }
62     {
63         Variant v ( boost::next(p.data().begin()), & p.data() );
64
65         BOOST_CHECK_EQUAL( p.data()[0], 2u );
66         BOOST_REQUIRE_EQUAL( v.variant(), 2u );
67         BOOST_CHECK_EQUAL( senf::bytes(v), 4u );
68
69         unsigned char data2[] = { 0x02, 0x00, 0x00, 0x00, 0x00,
70                                   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
71                                   0x19, 0x1A, 0x1B };
72         BOOST_CHECK_EQUAL_COLLECTIONS( p.data().begin(), p.data().end(),
73                                        data2, data2+sizeof(data2) );
74         BOOST_CHECK_EQUAL( v.get<2>().value(), 0u );
75         BOOST_CHECK_EQUAL( p.data().size(), 15u );
76         p.data()[4] = 0x01u;
77         BOOST_CHECK_EQUAL( v.get<2>().value(), 1u );
78     };
79 }
80
81 // We can't use the unnamed namespace here since there's a bug in gcc-4.2.3 which is
82 // the default version of gcc on ubuntu hardy :-(
83 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34213
84 namespace VariantParser_test_cc_anon_namespace {
85
86     struct SubParser : public senf::PacketParserBase
87     {
88 #       include SENF_FIXED_PARSER()
89
90         SENF_PARSER_FIELD( foo, senf::UInt32Parser );
91         SENF_PARSER_FIELD( bar, senf::Int16Parser );
92
93         SENF_PARSER_FINALIZE(SubParser);
94     };
95
96     struct TestParser : public senf::PacketParserBase
97     {
98 #       include SENF_PARSER()
99
100         SENF_PARSER_BITFIELD_RO( len,  4, unsigned );
101         SENF_PARSER_BITFIELD_RO( type, 4, unsigned );
102         // just here so the second variant is 'var'
103         SENF_PARSER_VARIANT( value, len,
104                                  (senf::VoidPacketParser)
105                                  (senf::UInt8Parser)
106                                  (senf::UInt16Parser)
107                                  (senf::UInt32Parser)
108             );
109         SENF_PARSER_VARIANT( content_, type,
110                                  ( novalue( nocontent, key(10, senf::VoidPacketParser) ) )
111                                  (      id( content,           SubParser               ) )
112             );
113
114         SENF_PARSER_FINALIZE(TestParser);
115     };
116
117 }
118 using namespace VariantParser_test_cc_anon_namespace;
119
120 SENF_AUTO_UNIT_TEST(VariantParserMacro)
121 {
122     senf::DataPacket p (senf::DataPacket::create(senf::init_bytes<TestParser>::value));
123
124     {
125         TestParser v (p.data().begin(), & p.data());
126         v.init();
127         BOOST_CHECK( ! v.has_content() );
128         BOOST_CHECK_EQUAL( senf::bytes(v), 1u );
129         BOOST_CHECK_EQUAL( v.type(), 10u );
130         v.init_content();
131         // Parser invalidated
132     }
133     {
134         TestParser v (p.data().begin(), & p.data());
135         BOOST_CHECK( v.has_content() );
136         BOOST_CHECK_EQUAL( senf::bytes(v), 7u );
137         BOOST_CHECK_EQUAL( v.content().foo(), 0u );
138         BOOST_CHECK_EQUAL( v.type(), 1u );
139         v.nocontent();
140         // Parser invalidated
141     }
142 }
143
144 //-/////////////////////////////////////////////////////////////////////////////////////////////////
145 #undef prefix_
146
147 \f
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"
156 // End: