bd78e2fd00b7d9d7ec34a3e0ee8042387341d502
[senf.git] / Packets / ParseListN.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 ParseListN.test unit tests */
23
24 //#include "ParseListN.test.hh"
25 //#include "ParseListN.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_Type : public senf::PacketTypeBase
38     {};
39     typedef senf::ConcretePacket<VoidPacket_Type> VoidPacket;
40
41     struct MyVec : public senf::PacketParserBase
42     {
43 #       include SENF_PARSER()
44
45         SENF_PARSER_PRIVATE_FIELD( size, senf::Parse_UInt8 );
46         SENF_PARSER_VEC_N( vec, size, senf::Parse_UInt16 );
47         
48         SENF_PARSER_FINALIZE(MyVec);
49     };
50 }
51
52 BOOST_AUTO_UNIT_TEST(parseListN)
53 {
54     typedef senf::Parse_ListN<MyVec,senf::Parse_UInt16>::parser ParseList;
55     
56     VoidPacket vp (VoidPacket::create(ParseList::init_bytes));
57
58     {
59         ParseList p (vp.data().begin(),&vp.data());
60         p.init();
61         BOOST_CHECK_EQUAL( p.size(), 0u );
62         BOOST_CHECK_EQUAL( p.bytes(), 2u );
63         BOOST_CHECK( p.empty() );
64         BOOST_CHECK( p.begin() == p.end() );
65     }
66
67     {
68 #       define p ParseList(vp.data().begin(),&vp.data())
69
70         p.push_back_space();
71         BOOST_CHECK_EQUAL( p.bytes(), 3u );
72         BOOST_CHECK_EQUAL( p.size(), 1u );
73         BOOST_CHECK_EQUAL( p.front().bytes(), 1u );
74         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u );
75         BOOST_CHECK_EQUAL( vp.data()[1], 0x01u );
76
77         p.front().vec().push_back(0x1234u);
78         BOOST_CHECK_EQUAL( p.front().vec().size(), 1u );
79         BOOST_CHECK_EQUAL( p.front().bytes(), 3u );
80         BOOST_CHECK_EQUAL( p.front().vec()[0], 0x1234u );
81         BOOST_CHECK_EQUAL( p.size(), 1u );
82         BOOST_CHECK_EQUAL( p.bytes(), 5u );
83
84         p.front().vec().push_back(0x2345u);
85         BOOST_CHECK_EQUAL( p.front().vec().back(), 0x2345u );
86         BOOST_CHECK_EQUAL( p.bytes(), 7u );
87
88         p.push_back_space();
89         BOOST_CHECK_EQUAL( p.size(), 2u );
90         BOOST_CHECK_EQUAL( p.bytes(), 8u );
91         
92         p.back().vec().push_front(0x0123u);
93         BOOST_CHECK_EQUAL( p.front().vec().size(), 2u );
94         BOOST_CHECK_EQUAL( p.back().vec().size(), 1u );
95
96         p.push_front_space(2u);
97         BOOST_CHECK_EQUAL( p.size(), 4u );
98         BOOST_CHECK_EQUAL( p.front().vec().size(), 0u);
99         
100         p.resize(3u);
101         BOOST_CHECK_EQUAL( p.size(), 3u );
102         BOOST_CHECK_EQUAL( p.back().vec()[0], 0x1234u );
103         BOOST_CHECK_EQUAL( p.bytes(), 9u );
104
105 #       undef p
106     }
107 }
108
109 BOOST_AUTO_UNIT_TEST(parseListN_container)
110 {
111     typedef senf::Parse_ListN<MyVec,senf::Parse_UInt16>::parser ParseList;
112     
113     VoidPacket vp (VoidPacket::create(ParseList::init_bytes));
114
115     ParseList(vp.data().begin(),&vp.data()).init();
116
117     
118 }
119
120
121 ///////////////////////////////cc.e////////////////////////////////////////
122 #undef prefix_
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: