Fix boot auto unit tests for Boost V1.34 compatibility
[senf.git] / Packets / PacketParser.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief PacketParser.test unit tests */
23
24 //#include "PacketParser.test.hh"
25 //#include "PacketParser.test.ih"
26
27 // Custom includes
28 #include "Packets.hh"
29
30 #include "../Utils/auto_unit_test.hh"
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct VoidPacket : public senf::PacketTypeBase {};
38
39     struct SimpleParser : public senf::PacketParserBase
40     {
41 #       include SENF_FIXED_PARSER()
42         SENF_PARSER_FINALIZE(SimpleParser);
43         
44         using senf::PacketParserBase::check;
45         using senf::PacketParserBase::validate;
46     };
47
48     struct FooParser : public senf::PacketParserBase
49     {
50 #       include SENF_FIXED_PARSER()
51
52         SENF_PARSER_FIELD( name, senf::Parse_UInt16 );
53         SENF_PARSER_FIELD( id,   senf::Parse_Int32  );
54
55         SENF_PARSER_FINALIZE(FooParser);
56     };
57
58     struct BarParser : public senf::PacketParserBase
59     {
60 #       include SENF_PARSER()
61
62         SENF_PARSER_FIELD( name, senf::Parse_UInt16 );
63         SENF_PARSER_FIELD( id,   senf::Parse_Int32  );
64
65         SENF_PARSER_FINALIZE(BarParser);
66     };
67 }
68
69 BOOST_AUTO_UNIT_TEST(packetParserBase)
70 {
71     senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
72     SimpleParser p (pi->data().begin(),&pi->data());
73
74     BOOST_CHECK( pi->data().begin() == p.i() );
75     BOOST_CHECK( p.check(6u) );
76     BOOST_CHECK( ! p.check(7u) );
77     BOOST_CHECK_NO_THROW( p.validate(6u) );
78     BOOST_CHECK_THROW( p.validate(7u), senf::TruncatedPacketException );
79
80     // ?? Why the heck do I need the +0? I get an 'undefined symbol FooParser::fixed_bytes'
81     // otherwise ...
82     BOOST_CHECK_EQUAL( FooParser::fixed_bytes+0, 6u );
83     BOOST_CHECK_EQUAL( BarParser(pi->data().begin(),&pi->data()).bytes(), 6u );
84     BOOST_CHECK_EQUAL( senf::bytes(senf::Parse_UInt16(pi->data().begin(),&pi->data())), 2u );
85     BOOST_CHECK_EQUAL( senf::bytes(FooParser(pi->data().begin(),&pi->data())), 6u );
86     BOOST_CHECK_EQUAL( senf::bytes(BarParser(pi->data().begin(),&pi->data())), 6u );
87
88     BOOST_CHECK_EQUAL( senf::init_bytes<FooParser>::value, 6u );
89     BOOST_CHECK_EQUAL( senf::init_bytes<BarParser>::value, 6u );
90 }
91
92 BOOST_AUTO_UNIT_TEST(safePacketParser)
93 {
94     senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
95     senf::SafePacketParser<senf::Parse_UInt16> p;
96     
97     BOOST_CHECK( !p );
98
99     p =  senf::Parse_UInt16(pi->data().begin(),&pi->data());
100
101     BOOST_CHECK( p );
102     (*p) = 0x1234u;
103     
104     BOOST_CHECK_EQUAL( (*p), 0x1234u );
105     BOOST_CHECK_EQUAL( p->data()[0], 0x12u );
106
107     p->data().resize(1024u);
108     BOOST_CHECK_EQUAL( (*p), 0x1234u );
109     (*p) = 0x2345u;
110     BOOST_CHECK_EQUAL( p->data()[0], 0x23u );
111 }
112
113 ///////////////////////////////cc.e////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40
125 // End: