switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / VariantParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief VariantParser unit tests */
30
31 //#include "VariantParser.test.hh"
32 //#include "VariantParser.test.ih"
33
34 // Custom includes
35 #include "Packets.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 SENF_AUTO_UNIT_TEST(VariantParser)
44 {
45     typedef senf::ArrayParser<10, senf::UInt8Parser> Array10;
46     typedef senf::VariantParser< senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1>,
47         boost::mpl::vector<senf::VoidPacketParser, Array10, senf:: UInt32Parser> > Variant;
48
49     unsigned char data[] = { 0x01, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
50                              0x19, 0x1A, 0x1B };
51     senf::DataPacket p (senf::DataPacket::create(data));
52     {
53         Variant v ( boost::next(p.data().begin()), & p.data() );
54         BOOST_REQUIRE_EQUAL( v.variant(), 1u );
55         BOOST_CHECK_EQUAL( senf::bytes(v), 10u );
56         BOOST_CHECK_EQUAL_COLLECTIONS( v.get<1>().begin(), v.get<1>().end(),
57                                        data+1, data+11 );
58         v.init();
59         // The container size should not change in this case but now variant should be 0 ...
60         BOOST_REQUIRE_EQUAL( p.data().size(), 11u );
61         BOOST_REQUIRE_EQUAL( v.variant(), 0u );
62         BOOST_CHECK_EQUAL( senf::bytes(v), 0u );
63
64         v.init<2>();
65         // v invalidated
66     }
67     {
68         Variant v ( boost::next(p.data().begin()), & p.data() );
69
70         BOOST_CHECK_EQUAL( p.data()[0], 2u );
71         BOOST_REQUIRE_EQUAL( v.variant(), 2u );
72         BOOST_CHECK_EQUAL( senf::bytes(v), 4u );
73
74         unsigned char data2[] = { 0x02, 0x00, 0x00, 0x00, 0x00,
75                                   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
76                                   0x19, 0x1A, 0x1B };
77         BOOST_CHECK_EQUAL_COLLECTIONS( p.data().begin(), p.data().end(),
78                                        data2, data2+sizeof(data2) );
79         BOOST_CHECK_EQUAL( v.get<2>().value(), 0u );
80         BOOST_CHECK_EQUAL( p.data().size(), 15u );
81         p.data()[4] = 0x01u;
82         BOOST_CHECK_EQUAL( v.get<2>().value(), 1u );
83     };
84 }
85
86 // We can't use the unnamed namespace here since there's a bug in gcc-4.2.3 which is
87 // the default version of gcc on ubuntu hardy :-(
88 // See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34213
89 namespace VariantParser_test_cc_anon_namespace {
90
91     struct SubParser : public senf::PacketParserBase
92     {
93 #       include SENF_FIXED_PARSER()
94
95         SENF_PARSER_FIELD( foo, senf::UInt32Parser );
96         SENF_PARSER_FIELD( bar, senf::Int16Parser );
97
98         SENF_PARSER_FINALIZE(SubParser);
99     };
100
101     struct TestParser : public senf::PacketParserBase
102     {
103 #       include SENF_PARSER()
104
105         SENF_PARSER_BITFIELD_RO( len,  4, unsigned );
106         SENF_PARSER_BITFIELD_RO( type, 4, unsigned );
107         // just here so the second variant is 'var'
108         SENF_PARSER_VARIANT( value, len,
109                                  (senf::VoidPacketParser)
110                                  (senf::UInt8Parser)
111                                  (senf::UInt16Parser)
112                                  (senf::UInt32Parser)
113             );
114         SENF_PARSER_VARIANT( content_, type,
115                                  ( novalue( nocontent, key(10, senf::VoidPacketParser) ) )
116                                  (      id( content,           SubParser               ) )
117             );
118
119         SENF_PARSER_FINALIZE(TestParser);
120     };
121
122 }
123 using namespace VariantParser_test_cc_anon_namespace;
124
125 SENF_AUTO_UNIT_TEST(VariantParserMacro)
126 {
127     senf::DataPacket p (senf::DataPacket::create(senf::init_bytes<TestParser>::value));
128
129     {
130         TestParser v (p.data().begin(), & p.data());
131         v.init();
132         BOOST_CHECK( ! v.has_content() );
133         BOOST_CHECK_EQUAL( senf::bytes(v), 1u );
134         BOOST_CHECK_EQUAL( v.type(), 10u );
135         v.init_content();
136         // Parser invalidated
137     }
138     {
139         TestParser v (p.data().begin(), & p.data());
140         BOOST_CHECK( v.has_content() );
141         BOOST_CHECK_EQUAL( senf::bytes(v), 7u );
142         BOOST_CHECK_EQUAL( v.content().foo(), 0u );
143         BOOST_CHECK_EQUAL( v.type(), 1u );
144         v.nocontent();
145         // Parser invalidated
146     }
147 }
148
149 //-/////////////////////////////////////////////////////////////////////////////////////////////////
150 #undef prefix_
151
152 \f
153 // Local Variables:
154 // mode: c++
155 // fill-column: 100
156 // comment-column: 40
157 // c-file-style: "senf"
158 // indent-tabs-mode: nil
159 // ispell-local-dictionary: "american"
160 // compile-command: "scons -u test"
161 // End: