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