4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief VariantParser unit tests */
31 //#include "VariantParser.test.hh"
32 //#include "VariantParser.test.ih"
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 SENF_AUTO_UNIT_TEST(VariantParser)
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;
49 unsigned char data[] = { 0x01, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
51 senf::DataPacket p (senf::DataPacket::create(data));
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(),
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 );
68 Variant v ( boost::next(p.data().begin()), & p.data() );
70 BOOST_CHECK_EQUAL( p.data()[0], 2u );
71 BOOST_REQUIRE_EQUAL( v.variant(), 2u );
72 BOOST_CHECK_EQUAL( senf::bytes(v), 4u );
74 unsigned char data2[] = { 0x02, 0x00, 0x00, 0x00, 0x00,
75 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
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 );
82 BOOST_CHECK_EQUAL( v.get<2>().value(), 1u );
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 {
91 struct SubParser : public senf::PacketParserBase
93 # include SENF_FIXED_PARSER()
95 SENF_PARSER_FIELD( foo, senf::UInt32Parser );
96 SENF_PARSER_FIELD( bar, senf::Int16Parser );
98 SENF_PARSER_FINALIZE(SubParser);
101 struct TestParser : public senf::PacketParserBase
103 # include SENF_PARSER()
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)
114 SENF_PARSER_VARIANT( content_, type,
115 ( novalue( nocontent, key(10, senf::VoidPacketParser) ) )
116 ( id( content, SubParser ) )
119 SENF_PARSER_FINALIZE(TestParser);
123 using namespace VariantParser_test_cc_anon_namespace;
125 SENF_AUTO_UNIT_TEST(VariantParserMacro)
127 senf::DataPacket p (senf::DataPacket::create(senf::init_bytes<TestParser>::value));
130 TestParser v (p.data().begin(), & p.data());
132 BOOST_CHECK( ! v.has_content() );
133 BOOST_CHECK_EQUAL( senf::bytes(v), 1u );
134 BOOST_CHECK_EQUAL( v.type(), 10u );
136 // Parser invalidated
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 );
145 // Parser invalidated
149 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"