4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief VariantParser.test unit tests */
26 //#include "VariantParser.test.hh"
27 //#include "VariantParser.test.ih"
32 #include "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
38 BOOST_AUTO_UNIT_TEST(VariantParser)
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;
44 unsigned char data[] = { 0x01, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
46 senf::DataPacket p (senf::DataPacket::create(data));
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(),
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 );
63 Variant v ( boost::next(p.data().begin()), & p.data() );
65 BOOST_CHECK_EQUAL( p.data()[0], 2u );
66 BOOST_REQUIRE_EQUAL( v.variant(), 2u );
67 BOOST_CHECK_EQUAL( senf::bytes(v), 4u );
69 unsigned char data2[] = { 0x02, 0x00, 0x00, 0x00, 0x00,
70 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
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 );
77 BOOST_CHECK_EQUAL( v.get<2>().value(), 1u );
83 struct SubParser : public senf::PacketParserBase
85 # include SENF_FIXED_PARSER()
87 SENF_PARSER_FIELD( foo, senf::UInt32Parser );
88 SENF_PARSER_FIELD( bar, senf::Int16Parser );
90 SENF_PARSER_FINALIZE(SubParser);
93 struct TestParser : public senf::PacketParserBase
95 # include SENF_PARSER()
97 struct TestTransform {
98 typedef unsigned value_type;
99 static unsigned get(unsigned v) { return v/2; }
100 static unsigned set(unsigned v) { return 2*v; }
103 SENF_PARSER_SKIP_BITS( 4 );
104 SENF_PARSER_BITFIELD_RO( type, 4, unsigned );
105 SENF_PARSER_PRIVATE_VARIANT( content_, transform(TestTransform, type),
106 (senf::VoidPacketParser)(SubParser) );
108 bool hasContent() const { return content_().variant() == 1; }
109 void hasContent(bool v) const { if (v) content_().init<1>(); else content_().init<0>(); }
110 SubParser content() const { return content_().get<1>(); }
112 SENF_PARSER_FINALIZE(TestParser);
117 BOOST_AUTO_UNIT_TEST(VariantParserMacro)
119 senf::DataPacket p (senf::DataPacket::create(senf::init_bytes<TestParser>::value));
122 TestParser v (p.data().begin(), & p.data());
123 BOOST_CHECK( ! v.hasContent() );
124 BOOST_CHECK_EQUAL( senf::bytes(v), 1u );
125 BOOST_CHECK_EQUAL( v.type(), 0u );
127 // Parser invalidated
130 TestParser v (p.data().begin(), & p.data());
131 BOOST_CHECK( v.hasContent() );
132 BOOST_CHECK_EQUAL( senf::bytes(v), 7u );
133 BOOST_CHECK_EQUAL( v.content().foo(), 0u );
134 BOOST_CHECK_EQUAL( v.type(), 2u );
138 ///////////////////////////////cc.e////////////////////////////////////////
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"