a22455b2cf7a28f9a6c2768b21be14a333f20ff7
[senf.git] / Packets / ArrayParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.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 "ArrayParser.test.hh"
26 //#include "ArrayParser.test.ih"
27
28 // Custom includes
29 #include "Packets.hh"
30
31 #include "../Utils/auto_unit_test.hh"
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 #       include SENF_FIXED_PARSER()
44
45         typedef senf::ArrayParser<2,senf::UInt24Parser> ArrayParser2;
46         
47         SENF_PARSER_FIELD( array, ArrayParser2 );
48         SENF_PARSER_FIELD( index, senf::UInt16Parser );
49
50         SENF_PARSER_FINALIZE(SomePacketParser);
51     };
52
53     struct SomeOtherParser : public senf::PacketParserBase
54     {
55 #       include SENF_FIXED_PARSER()
56         
57         typedef senf::ArrayParser<1,SomePacketParser> ArrayParser1;
58
59         SENF_PARSER_FIELD( fields, ArrayParser1 );
60
61         SENF_PARSER_FINALIZE(SomeOtherParser);
62     };
63 }
64
65 BOOST_AUTO_UNIT_TEST(ArrayParser_test)
66 {
67     senf::PacketParserBase::byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
68     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));   
69
70     {
71         typedef senf::ArrayParser<6,senf::UInt8Parser> UInt8ParserArray6;
72         UInt8ParserArray6 v (p->data().begin(),&p->data());
73
74         BOOST_CHECK_EQUAL( v[0], 0x00 );
75         BOOST_CHECK_EQUAL( v[5], 0x05 );
76         BOOST_CHECK_EQUAL( *v.begin(), 0x00 );
77         BOOST_CHECK_EQUAL( std::distance(v.begin(),v.end()), 
78                            UInt8ParserArray6::difference_type(v.size()) );
79         BOOST_CHECK_EQUAL( v.size(), 6u );
80         UInt8ParserArray6::iterator i1 (v.begin());
81         UInt8ParserArray6::iterator i2 (v.begin());
82         ++i1;
83         BOOST_CHECK_EQUAL( *i1, 0x01 );
84         BOOST_CHECK_EQUAL( i1[-1], 0x00 );
85         BOOST_CHECK_EQUAL( i1-i2, 1 );
86         BOOST_CHECK_EQUAL( i2-i1, -1 );
87         --i1;
88         BOOST_CHECK( i1==i2 );
89     }
90     
91     {
92         SomeOtherParser v (p->data().begin(),&p->data());
93
94         BOOST_CHECK_THROW( v.fields(), senf::TruncatedPacketException );
95     }
96 }
97
98 ///////////////////////////////cc.e////////////////////////////////////////
99 #undef prefix_
100
101 \f
102 // Local Variables:
103 // mode: c++
104 // fill-column: 100
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // comment-column: 40
110 // End: