44b406d6bf25468ad1b3f1a0a6a1159434f7214c
[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 "Packets.hh"
30
31 #include <boost/test/auto_unit_test.hpp>
32 #include <boost/test/test_tools.hpp>
33
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 namespace {
38     struct VoidPacket : public senf::PacketTypeBase
39     {};
40
41     struct SomePacketParser : public senf::PacketParserBase
42     {
43         SENF_PACKET_PARSER_INIT(SomePacketParser);
44
45         typedef senf::Parse_Array<2,senf::Parse_UInt24> Parse_Array2;
46         
47         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
48             ((Field)( array, Parse_Array2 ))
49             ((Field)( index, senf::Parse_UInt16 )) );
50     };
51
52     struct SomeOtherParser : public senf::PacketParserBase
53     {
54         SENF_PACKET_PARSER_INIT(SomeOtherParser);
55         
56         typedef senf::Parse_Array<1,SomePacketParser> Parse_Array1;
57
58         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
59             ((Field)( fields, Parse_Array1 )) );
60     };
61 }
62
63 BOOST_AUTO_UNIT_TEST(parseArray_test)
64 {
65     senf::PacketParserBase::byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
66     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));   
67
68     {
69         typedef senf::Parse_Array<6,senf::Parse_UInt8> Parse_UInt8Array6;
70         Parse_UInt8Array6 v (p->data().begin(),&p->data());
71
72         BOOST_CHECK_EQUAL( v[0], 0x00 );
73         BOOST_CHECK_EQUAL( v[5], 0x05 );
74         BOOST_CHECK_EQUAL( *v.begin(), 0x00 );
75         BOOST_CHECK_EQUAL( std::distance(v.begin(),v.end()), 
76                            Parse_UInt8Array6::difference_type(v.size()) );
77         BOOST_CHECK_EQUAL( v.size(), 6u );
78         Parse_UInt8Array6::iterator i1 (v.begin());
79         Parse_UInt8Array6::iterator i2 (v.begin());
80         ++i1;
81         BOOST_CHECK_EQUAL( *i1, 0x01 );
82         BOOST_CHECK_EQUAL( i1[-1], 0x00 );
83         BOOST_CHECK_EQUAL( i1-i2, 1 );
84         BOOST_CHECK_EQUAL( i2-i1, -1 );
85         --i1;
86         BOOST_CHECK( i1==i2 );
87     }
88     
89     {
90         SomeOtherParser v (p->data().begin(),&p->data());
91
92         BOOST_CHECK_THROW( v.fields(), senf::TruncatedPacketException );
93     }
94 }
95
96 ///////////////////////////////cc.e////////////////////////////////////////
97 #undef prefix_
98
99 \f
100 // Local Variables:
101 // mode: c++
102 // fill-column: 100
103 // c-file-style: "senf"
104 // indent-tabs-mode: nil
105 // ispell-local-dictionary: "american"
106 // compile-command: "scons -u test"
107 // comment-column: 40
108 // End: