Id keyword, again
[senf.git] / Packets / ParseListB.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief ParseListB.test unit tests */
23
24 //#include "ParseListB.test.hh"
25 //#include "ParseListB.test.ih"
26
27 // Custom includes
28 #include "Packets.hh"
29
30 #include "../Utils/auto_unit_test.hh"
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct VoidPacket : public senf::PacketTypeBase
38     {};
39
40     struct ParseVec : public senf::PacketParserBase
41     {
42 #       include SENF_PARSER()
43
44         SENF_PARSER_PRIVATE_FIELD( size, senf::Parse_UInt8 );
45         SENF_PARSER_VEC_N( vec, size, senf::Parse_UInt16 );
46
47         SENF_PARSER_FINALIZE(ParseVec);
48     };
49
50     typedef senf::Parse_ListB<ParseVec,senf::Parse_UInt16>::parser ParseList;
51 }
52
53 BOOST_AUTO_UNIT_TEST(parseListB)
54 {
55     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
56                                              ParseList::init_bytes));
57     
58     ParseList p (pi->data().begin(),&pi->data());
59     p.init();
60     BOOST_CHECK_EQUAL( p.size(), 0u );
61     BOOST_CHECK_EQUAL( p.bytes(), 2u );
62     BOOST_CHECK( p.empty() );
63     BOOST_CHECK( p.begin() == p.end() );
64
65     // the mutators are really tested together with the container wrappers since they are based
66     // on the container wrapper. Here we only need one call to make the list larger ...
67
68     p.push_back_space();
69     p = ParseList(pi->data().begin(),&pi->data());
70     BOOST_CHECK_EQUAL( p.bytes(), 3u );
71     BOOST_CHECK_EQUAL( p.size(), 1u );
72     BOOST_CHECK( ! p.empty() );
73     BOOST_CHECK( p.begin() != p.end() );
74 }
75
76 BOOST_AUTO_UNIT_TEST(parseListB_container)
77 {
78     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
79                                              ParseList::init_bytes));
80     
81     {
82         ParseList::container c (ParseList(pi->data().begin(),&pi->data()));
83      
84         BOOST_CHECK_EQUAL( c.size(), 0u );
85         BOOST_CHECK_EQUAL( c.bytes(), 2u );
86         BOOST_CHECK( c.begin() == c.end() );
87         
88         c.shift(c.begin());
89         BOOST_CHECK_EQUAL( c.size(), 1u );
90         BOOST_CHECK_EQUAL( c.bytes(), 3u );
91
92         BOOST_CHECK_EQUAL( c.front().vec().size(), 0u );
93         c.front().vec().push_back(0x1234u);
94         BOOST_CHECK_EQUAL( c.bytes(), 5u );
95
96         {
97             senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
98                                                       ParseList::init_bytes));
99             ParseList::container c2 (ParseList(pi2->data().begin(),&pi2->data()));
100             c2.push_back_space();
101             {
102                 ParseVec::vec_t::container c2v (c2.front().vec());
103                 c2v.push_back(0x2345u);
104                 c2v.push_back(0x3456u);
105             }
106
107             BOOST_CHECK_EQUAL(c2.size(), 1u);
108             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
109             
110             c.insert(c.end(),c2.back());
111             BOOST_CHECK_EQUAL( c.size(), 2u );
112             BOOST_CHECK_EQUAL( c.bytes(), 10u );
113             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
114             BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
115
116             c2.back().vec()[0] << 0x1357u;
117             c.insert(boost::next(c.begin()), 2u, c2.back());
118             BOOST_CHECK_EQUAL( c.size(), 4u );
119             BOOST_CHECK_EQUAL( c.bytes(), 20u );
120             BOOST_CHECK_EQUAL( (*boost::next(c.begin())).vec()[0], 0x1357u ); 
121             BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2)).vec()[0], 0x1357u );
122
123             c2.back().vec()[0] << 0x2468u;
124             c.insert(c.begin(),c2.begin(),c2.end());
125             BOOST_CHECK_EQUAL( c.size(), 5u );
126             BOOST_CHECK_EQUAL( c.bytes(), 25u );
127             BOOST_CHECK_EQUAL( c.front().vec()[0], 0x2468u );
128
129             c.erase(c.begin(),2);
130             BOOST_CHECK_EQUAL( c.size(), 3u );
131             BOOST_CHECK_EQUAL( c.bytes(), 17u );
132             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
133             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x2345u );
134             
135             c.erase((boost::next(c.begin(),2)),c.end());
136             BOOST_CHECK_EQUAL( c.size(), 2u );
137             BOOST_CHECK_EQUAL( c.bytes(), 12u );
138             BOOST_CHECK_EQUAL( c.front().vec()[0],0x1357u );
139             BOOST_CHECK_EQUAL( c.back().vec()[0], 0x1357u );
140
141             c.clear();
142             BOOST_CHECK_EQUAL( c.size(), 0u );
143             BOOST_CHECK_EQUAL( c.bytes(), 2u );
144        }
145     }
146 }
147
148 ///////////////////////////////cc.e////////////////////////////////////////
149 #undef prefix_
150
151 \f
152 // Local Variables:
153 // mode: c++
154 // fill-column: 100
155 // comment-column: 40
156 // c-file-style: "senf"
157 // indent-tabs-mode: nil
158 // ispell-local-dictionary: "american"
159 // compile-command: "scons -u test"
160 // End: