Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[senf.git] / Packets / ParseArray.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 // Unit tests
24
25 //#include "ParseArray.test.hh"
26 //#include "ParseArray.test.ih"
27
28 // Custom includes
29 #include "PacketInterpreter.hh"
30 #include "ParseArray.hh"
31 #include "ParseInt.hh"
32 #include "PacketType.hh"
33
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41     struct VoidPacket : public senf::PacketTypeBase
42     {};
43
44     struct SomePacketParser : public senf::PacketParserBase
45     {
46         SENF_PACKET_PARSER_INIT(SomePacketParser);
47
48         typedef senf::Parse_Array<2,senf::Parse_UInt24> Parse_Array2;
49         
50         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
51             ((Field)( array, Parse_Array2 ))
52             ((Field)( index, senf::Parse_UInt16 )) );
53     };
54
55     struct SomeOtherParser : public senf::PacketParserBase
56     {
57         SENF_PACKET_PARSER_INIT(SomeOtherParser);
58         
59         typedef senf::Parse_Array<1,SomePacketParser> Parse_Array1;
60
61         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
62             ((Field)( fields, Parse_Array1 )) );
63     };
64 }
65
66 BOOST_AUTO_UNIT_TEST(parseArray_test)
67 {
68     senf::PacketParserBase::byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
69     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));   
70
71     {
72         typedef senf::Parse_Array<6,senf::Parse_UInt8> Parse_UInt8Array6;
73         Parse_UInt8Array6 v (p->data().begin(),&p->data());
74
75         BOOST_CHECK_EQUAL( v[0], 0x00 );
76         BOOST_CHECK_EQUAL( v[5], 0x05 );
77         BOOST_CHECK_EQUAL( *v.begin(), 0x00 );
78         BOOST_CHECK_EQUAL( std::distance(v.begin(),v.end()), 
79                            Parse_UInt8Array6::difference_type(v.size()) );
80         BOOST_CHECK_EQUAL( v.size(), 6u );
81         Parse_UInt8Array6::iterator i1 (v.begin());
82         Parse_UInt8Array6::iterator i2 (v.begin());
83         ++i1;
84         BOOST_CHECK_EQUAL( *i1, 0x01 );
85         BOOST_CHECK_EQUAL( i1[-1], 0x00 );
86         BOOST_CHECK_EQUAL( i1-i2, 1 );
87         BOOST_CHECK_EQUAL( i2-i1, -1 );
88         --i1;
89         BOOST_CHECK( i1==i2 );
90     }
91     
92     {
93         SomeOtherParser v (p->data().begin(),&p->data());
94
95         BOOST_CHECK_THROW( v.fields(), senf::TruncatedPacketException );
96     }
97 }
98
99 ///////////////////////////////cc.e////////////////////////////////////////
100 #undef prefix_
101
102 \f
103 // Local Variables:
104 // mode: c++
105 // fill-column: 100
106 // c-file-style: "senf"
107 // indent-tabs-mode: nil
108 // ispell-local-dictionary: "american"
109 // compile-command: "scons -u test"
110 // End: