9b807e6647520d20b2ae311155e7ce7d0c5c22c3
[senf.git] / 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.test unit tests */
25
26 //#include "VariantParser.test.hh"
27 //#include "VariantParser.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 BOOST_AUTO_UNIT_TEST(VariantParser)
39 {
40     typedef senf::ArrayParser<10, senf::UInt8Parser> Array10;
41     typedef senf::DirectVariantParser< senf::UInt8Parser, 1, senf::detail::VariantParser_IdentityTranslator,
42         senf::VoidPacketParser,
43         Array10, 
44         senf:: UInt32Parser 
45         >::parser Variant;
46     
47     unsigned char data[] = { 0x01, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
48                              0x19, 0x1A, 0x1B };
49     senf::DataPacket p (senf::DataPacket::create(data));
50     {
51         Variant v ( boost::next(p.data().begin()), & p.data() );
52         BOOST_REQUIRE_EQUAL( v.variant(), 1u );
53         BOOST_CHECK_EQUAL( senf::bytes(v), 10u );
54         BOOST_CHECK_EQUAL_COLLECTIONS( v.get<1>().begin(), v.get<1>().end(),
55                                        data+1, data+11 );
56         v.init();
57         // The container size should not change in this case but now variant should be 0 ...
58         BOOST_REQUIRE_EQUAL( p.data().size(), 11u );
59         BOOST_REQUIRE_EQUAL( v.variant(), 0u );
60         BOOST_CHECK_EQUAL( senf::bytes(v), 0u );
61         
62         v.init<2>();
63         // v invalidated
64     }
65     {
66         Variant v ( boost::next(p.data().begin()), & p.data() );
67
68         BOOST_CHECK_EQUAL( p.data()[0], 2u );
69         BOOST_REQUIRE_EQUAL( v.variant(), 2u );
70         BOOST_CHECK_EQUAL( senf::bytes(v), 4u );
71
72         unsigned char data2[] = { 0x02, 0x00, 0x00, 0x00, 0x00,
73                                   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
74                                   0x19, 0x1A, 0x1B };
75         BOOST_CHECK_EQUAL_COLLECTIONS( p.data().begin(), p.data().end(),
76                                        data2, data2+sizeof(data2) );
77         BOOST_CHECK_EQUAL( v.get<2>().value(), 0u );
78         BOOST_CHECK_EQUAL( p.data().size(), 15u );
79         p.data()[4] = 0x01u;
80         BOOST_CHECK_EQUAL( v.get<2>().value(), 1u );
81     };
82 }
83
84 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_SKIP_BITS( 4 );
101         SENF_PARSER_PRIVATE_BITFIELD( type_, 4, unsigned );
102         SENF_PARSER_PRIVATE_VARIANT( content_, type_, (senf::VoidPacketParser)(SubParser) );
103
104         bool hasContent() const { return content_().variant() == 1; }
105         void hasContent(bool v) const { if (v) content_().init<1>(); else content_().init<0>(); }
106         SubParser content() const { return content_().get<1>(); }
107
108         SENF_PARSER_FINALIZE(TestParser);
109     };
110     
111 }
112
113 BOOST_AUTO_UNIT_TEST(VariantParserMacro)
114 {
115     senf::DataPacket p (senf::DataPacket::create(senf::init_bytes<TestParser>::value));
116     
117     {
118         TestParser v (p.data().begin(), & p.data());
119     
120         BOOST_CHECK( ! v.hasContent() );
121         BOOST_CHECK_EQUAL( senf::bytes(v), 1u );
122         v.hasContent(true);
123         // Parser invalidated
124     }
125     {
126         TestParser v (p.data().begin(), & p.data());
127         BOOST_CHECK( v.hasContent() );
128         BOOST_CHECK_EQUAL( senf::bytes(v), 7u );
129         BOOST_CHECK_EQUAL( v.content().foo(), 0u );
130     }
131 }
132
133 ///////////////////////////////cc.e////////////////////////////////////////
134 #undef prefix_
135
136 \f
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // comment-column: 40
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // End: