90b0b7e0fc6c2ed79d14d794dd801690c6c23659
[senf.git] / senf / Packets / AuxParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 AuxParser unit tests */
25
26 //#include "AuxParser.test.hh"
27 //#include "AuxParser.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 // The other aux policies are tested with the corresponding container classes
39
40 SENF_AUTO_UNIT_TEST(vectorPacketSizeAuxPolicy)
41 {
42     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
43                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
44     senf::Packet p (senf::DataPacket::create(data));
45
46     typedef senf::VectorParser<
47         senf::UInt8Parser,
48         senf::detail::PacketSizeAuxParserPolicy
49         > UInt8VectorParser;
50
51     {
52         UInt8VectorParser v (p.data().begin(), &p.data());
53
54         BOOST_CHECK_EQUAL( v.size(), p.data().size() );
55         BOOST_CHECK_EQUAL_COLLECTIONS( v.begin(), v.end(), data, data+sizeof(data) );
56     }
57
58     {
59         UInt8VectorParser v (p.data().begin(), &p.data());
60         UInt8VectorParser::container w (v);
61
62         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(), data, data+sizeof(data) );
63
64         w.shift(w.begin()+1);
65         BOOST_CHECK_EQUAL( w.size(), p.data().size() );
66
67         w.erase(w.begin()+3, w.begin()+5);
68         BOOST_CHECK_EQUAL( w.size(), p.data().size() );
69
70         unsigned char data2[] = { 0x10, 0x00, 0x11, 0x14, 0x15,
71                                   0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
72
73         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(), data2, data2+sizeof(data2) );
74     }
75 }
76
77 SENF_AUTO_UNIT_TEST(vectorPacketSizeAuxPolicy_transformed)
78 {
79     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
80                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
81     senf::Packet p (senf::DataPacket::create(data));
82
83     typedef senf::VectorParser<
84         senf::UInt16Parser,
85         senf::detail::TransformAuxParserPolicy<
86             senf::detail::PacketSizeAuxParserPolicy,
87             senf::detail::VectorParserBytesTransform<senf::UInt16Parser::fixed_bytes> >
88         > UInt16VectorParser;
89
90     {
91         UInt16VectorParser v (p.data().begin(), &p.data());
92
93         BOOST_REQUIRE_EQUAL( v.size(), p.data().size()/2 );
94         BOOST_CHECK_EQUAL( v[0], 0x1011u );
95         BOOST_CHECK_EQUAL( v[1], 0x1213u );
96         BOOST_CHECK_EQUAL( v[5], 0x2425u );
97     }
98
99     {
100         UInt16VectorParser v (p.data().begin(), &p.data());
101         UInt16VectorParser::container w (v);
102
103         BOOST_REQUIRE_EQUAL( w.size(), p.data().size()/2 );
104         BOOST_CHECK_EQUAL( w[0], 0x1011u );
105         BOOST_CHECK_EQUAL( w[1], 0x1213u );
106         BOOST_CHECK_EQUAL( w[5], 0x2425u );
107
108         w.shift(w.begin()+1);
109         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
110
111         w.erase(w.begin()+3, w.begin()+5);
112         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
113
114         senf::UInt16Parser::value_type  data2[] =
115             { 0x1011u, 0x0000u, 0x1213u, 0x2223u, 0x2425u };
116
117         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(),
118                                        data2, data2+sizeof(data2)/sizeof(data2[0]) );
119     }
120 }
121
122 SENF_AUTO_UNIT_TEST(listPacketSizeAuxPolicy)
123 {
124     unsigned char data[] = { 0x10, 0x11,  0x12, 0x13,  0x14, 0x15,
125                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
126     senf::Packet p (senf::DataPacket::create(data));
127
128     typedef senf::ListParser<
129         senf::detail::ListBParser_Policy<
130             senf::UInt16Parser,
131             senf::detail::PacketSizeAuxParserPolicy>
132         > ListParser;
133
134     {
135         ListParser l (p.data().begin(), &p.data());
136
137         BOOST_REQUIRE_EQUAL( l.size(), p.data().size()/2 );
138         BOOST_CHECK_EQUAL( l.front(), 0x1011u );
139         BOOST_CHECK_EQUAL( l.back(), 0x2425u );
140     }
141
142     {
143         ListParser l (p.data().begin(), &p.data());
144         ListParser::container w (l);
145
146         BOOST_REQUIRE_EQUAL( w.size(), p.data().size()/2 );
147         BOOST_CHECK_EQUAL( *w.begin(), 0x1011u );
148         BOOST_CHECK_EQUAL( *boost::next(w.begin()), 0x1213u );
149         BOOST_CHECK_EQUAL( *boost::next(w.begin(),5), 0x2425u );
150
151         w.shift(boost::next(w.begin()));
152         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
153
154         w.erase(boost::next(w.begin(),3), boost::next(w.begin(),5));
155         BOOST_CHECK_EQUAL( w.size(), p.data().size()/2 );
156
157         senf::UInt16Parser::value_type  data2[] =
158             { 0x1011u, 0x0000u, 0x1213u, 0x2223u, 0x2425u };
159
160         BOOST_CHECK_EQUAL_COLLECTIONS( w.begin(), w.end(),
161                                        data2, data2+sizeof(data2)/sizeof(data2[0]) );
162     }
163 }
164
165 //-/////////////////////////////////////////////////////////////////////////////////////////////////
166 #undef prefix_
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: