c7e6829ad85204b44fac278ab0557504138686cd
[senf.git] / senf / 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 /** \file
24     \brief ArrayParser unit tests */
25
26 //#include "ArrayParser.test.hh"
27 //#include "ArrayParser.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39     struct VoidPacket : public senf::PacketTypeBase
40     {};
41
42     struct SomePacketParser : public senf::PacketParserBase
43     {
44 #       include SENF_FIXED_PARSER()
45
46         typedef senf::ArrayParser<2,senf::UInt24Parser> ArrayParser2;
47
48         SENF_PARSER_FIELD( array, ArrayParser2 );
49         SENF_PARSER_FIELD( index, senf::UInt16Parser );
50
51         SENF_PARSER_FINALIZE(SomePacketParser);
52     };
53
54     struct SomeOtherParser : public senf::PacketParserBase
55     {
56 #       include SENF_FIXED_PARSER()
57
58         typedef senf::ArrayParser<1,SomePacketParser> ArrayParser1;
59
60         SENF_PARSER_FIELD( fields, ArrayParser1 );
61
62         SENF_PARSER_FINALIZE(SomeOtherParser);
63     };
64 }
65
66 SENF_AUTO_UNIT_TEST(ArrayParser_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::ArrayParser<6,senf::UInt8Parser> UInt8ParserArray6;
73         UInt8ParserArray6 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                            UInt8ParserArray6::difference_type(v.size()) );
80         BOOST_CHECK_EQUAL( v.size(), 6u );
81         UInt8ParserArray6::iterator i1 (v.begin());
82         UInt8ParserArray6::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 // comment-column: 40
111 // End: