Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[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 "ParseListB.hh"
29 #include "ParseVec.hh"
30 #include "PacketType.hh"
31 #include "ParseInt.hh"
32
33 #include <boost/test/auto_unit_test.hpp>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40     struct VoidPacket : public senf::PacketTypeBase
41     {};
42
43     typedef senf::Parse_VectorN<senf::Parse_UInt16,senf::Parse_UInt8>::parser ParseVec;
44     typedef senf::Parse_ListB<ParseVec,senf::Parse_UInt16>::parser ParseList;
45 }
46
47 BOOST_AUTO_UNIT_TEST(parseListB)
48 {
49     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
50                                              ParseList::init_bytes));
51     
52     ParseList p (pi->data().begin(),&pi->data());
53     p.init();
54     BOOST_CHECK_EQUAL( p.size(), 0u );
55     BOOST_CHECK_EQUAL( p.bytes(), 2u );
56     BOOST_CHECK( p.empty() );
57     BOOST_CHECK( p.begin() == p.end() );
58
59     // the mutators are really tested together with the container wrappers since they are based
60     // on the container wrapper. Here we only need one call to make the list larger ...
61
62     p.push_back_space();
63     p = ParseList(pi->data().begin(),&pi->data());
64     BOOST_CHECK_EQUAL( p.bytes(), 3u );
65     BOOST_CHECK_EQUAL( p.size(), 1u );
66     BOOST_CHECK( ! p.empty() );
67     BOOST_CHECK( p.begin() != p.end() );
68 }
69
70 BOOST_AUTO_UNIT_TEST(parseListB_container)
71 {
72     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
73                                              ParseList::init_bytes));
74     
75     {
76         ParseList::container c (ParseList(pi->data().begin(),&pi->data()));
77      
78         BOOST_CHECK_EQUAL( c.size(), 0u );
79         BOOST_CHECK_EQUAL( c.bytes(), 2u );
80         BOOST_CHECK( c.begin() == c.end() );
81         
82         c.shift(c.begin());
83         BOOST_CHECK_EQUAL( c.size(), 1u );
84         BOOST_CHECK_EQUAL( c.bytes(), 3u );
85
86         BOOST_CHECK_EQUAL( c.front().size(), 0u );
87         c.front().push_back(0x1234u);
88         BOOST_CHECK_EQUAL( c.bytes(), 5u );
89
90         {
91             senf::PacketInterpreterBase::ptr pi2 (senf::PacketInterpreter<VoidPacket>::create(
92                                                       ParseList::init_bytes));
93             ParseList::container c2 (ParseList(pi2->data().begin(),&pi2->data()));
94             c2.push_back_space();
95             {
96                 ParseVec::container c2v (c2.front());
97                 c2v.push_back(0x2345u);
98                 c2v.push_back(0x3456u);
99             }
100
101             BOOST_CHECK_EQUAL(c2.size(), 1u);
102             BOOST_CHECK_EQUAL(c2.bytes(), 7u);
103             
104             c.insert(c.end(),c2.back());
105             BOOST_CHECK_EQUAL( c.size(), 2u );
106             BOOST_CHECK_EQUAL( c.bytes(), 10u );
107             BOOST_CHECK_EQUAL( c.back()[0], 0x2345u );
108             BOOST_CHECK_EQUAL( c.back().bytes(), c2.back().bytes() );
109
110             c2.back()[0] << 0x1357u;
111             c.insert(boost::next(c.begin()), 2u, c2.back());
112             BOOST_CHECK_EQUAL( c.size(), 4u );
113             BOOST_CHECK_EQUAL( c.bytes(), 20u );
114             BOOST_CHECK_EQUAL( (*boost::next(c.begin()))[0], 0x1357u ); 
115             BOOST_CHECK_EQUAL( (*boost::next(c.begin(),2))[0], 0x1357u );
116
117             c2.back()[0] << 0x2468u;
118             c.insert(c.begin(),c2.begin(),c2.end());
119             BOOST_CHECK_EQUAL( c.size(), 5u );
120             BOOST_CHECK_EQUAL( c.bytes(), 25u );
121             BOOST_CHECK_EQUAL( c.front()[0], 0x2468u );
122
123             c.erase(c.begin(),2);
124             BOOST_CHECK_EQUAL( c.size(), 3u );
125             BOOST_CHECK_EQUAL( c.bytes(), 17u );
126             BOOST_CHECK_EQUAL( c.front()[0],0x1357u );
127             BOOST_CHECK_EQUAL( c.back()[0], 0x2345u );
128             
129             c.erase((boost::next(c.begin(),2)),c.end());
130             BOOST_CHECK_EQUAL( c.size(), 2u );
131             BOOST_CHECK_EQUAL( c.bytes(), 12u );
132             BOOST_CHECK_EQUAL( c.front()[0],0x1357u );
133             BOOST_CHECK_EQUAL( c.back()[0], 0x1357u );
134
135             c.clear();
136             BOOST_CHECK_EQUAL( c.size(), 0u );
137             BOOST_CHECK_EQUAL( c.bytes(), 2u );
138        }
139     }
140 }
141
142 ///////////////////////////////cc.e////////////////////////////////////////
143 #undef prefix_
144
145 \f
146 // Local Variables:
147 // mode: c++
148 // fill-column: 100
149 // comment-column: 40
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // End: