switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / ArrayParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ArrayParser unit tests */
30
31 //#include "ArrayParser.test.hh"
32 //#include "ArrayParser.test.ih"
33
34 // Custom includes
35 #include "Packets.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace {
44     struct VoidPacket : public senf::PacketTypeBase
45     {};
46
47     struct SomePacketParser : public senf::PacketParserBase
48     {
49 #       include SENF_FIXED_PARSER()
50
51         typedef senf::ArrayParser<2,senf::UInt24Parser> ArrayParser2;
52
53         SENF_PARSER_FIELD( array, ArrayParser2 );
54         SENF_PARSER_FIELD( index, senf::UInt16Parser );
55
56         SENF_PARSER_FINALIZE(SomePacketParser);
57     };
58
59     struct SomeOtherParser : public senf::PacketParserBase
60     {
61 #       include SENF_FIXED_PARSER()
62
63         typedef senf::ArrayParser<1,SomePacketParser> ArrayParser1;
64
65         SENF_PARSER_FIELD( fields, ArrayParser1 );
66
67         SENF_PARSER_FINALIZE(SomeOtherParser);
68     };
69 }
70
71 SENF_AUTO_UNIT_TEST(ArrayParser_test)
72 {
73     senf::PacketParserBase::byte data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
74     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
75
76     {
77         typedef senf::ArrayParser<6,senf::UInt8Parser> UInt8ParserArray6;
78         UInt8ParserArray6 v (p->data().begin(),&p->data());
79
80         BOOST_CHECK_EQUAL( v[0], 0x00 );
81         BOOST_CHECK_EQUAL( v[5], 0x05 );
82         BOOST_CHECK_EQUAL( *v.begin(), 0x00 );
83         BOOST_CHECK_EQUAL( std::distance(v.begin(),v.end()),
84                            UInt8ParserArray6::difference_type(v.size()) );
85         BOOST_CHECK_EQUAL( v.size(), 6u );
86         UInt8ParserArray6::iterator i1 (v.begin());
87         UInt8ParserArray6::iterator i2 (v.begin());
88         ++i1;
89         BOOST_CHECK_EQUAL( *i1, 0x01 );
90         BOOST_CHECK_EQUAL( i1[-1], 0x00 );
91         BOOST_CHECK_EQUAL( i1-i2, 1 );
92         BOOST_CHECK_EQUAL( i2-i1, -1 );
93         --i1;
94         BOOST_CHECK( i1==i2 );
95     }
96
97     {
98         SomeOtherParser v (p->data().begin(),&p->data());
99
100         BOOST_CHECK_THROW( v.fields(), senf::TruncatedPacketException );
101     }
102 }
103
104 //-/////////////////////////////////////////////////////////////////////////////////////////////////
105 #undef prefix_
106
107 \f
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40
116 // End: