2087a5b012ff105ea804772df4db333026d6dd4f
[senf.git] / senf / Packets / PacketData.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 PacketData unit tests */
25
26 //#include "PacketData.test.hh"
27 //#include "PacketData.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 namespace {
39     struct VoidPacket : public senf::PacketTypeBase {};
40 }
41
42 SENF_AUTO_UNIT_TEST(packetData)
43 {
44     // We cannot simply allocate a packetData instance .. we must go through PacketInterpreterBase
45     // and PacketImpl.
46
47     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
48
49     senf::PacketData & d (pi->data());\
50
51     BOOST_CHECK( d.begin() == d.end() );
52     BOOST_CHECK_EQUAL( d.size(), 0u );
53     BOOST_CHECK( d.empty() );
54
55     d.insert(d.begin(), 0xabu);
56     BOOST_CHECK_EQUAL( d.size(), 1u );
57     BOOST_CHECK_EQUAL( d[0], 0xabu );
58     BOOST_CHECK( !d.empty() );
59
60     d.insert(d.begin(), 10, 0xcdu );
61     BOOST_CHECK_EQUAL( d.size(), 11u );
62     BOOST_CHECK_EQUAL( d[0], 0xcdu );
63     BOOST_CHECK_EQUAL( d[9], 0xcdu );
64     BOOST_CHECK_EQUAL( d[10], 0xabu );
65
66     senf::PacketData::byte data[] =
67         { 0xf0u, 0xf1u, 0xf2u, 0xf3u, 0xf4u, 0xf5u, 0xf6u, 0xf7u };
68     d.insert(d.begin()+5, data, data+sizeof(data)/sizeof(data[0]));
69     BOOST_CHECK_EQUAL( d.size(), 19u );
70     BOOST_CHECK_EQUAL( d[4], 0xcdu );
71     BOOST_CHECK_EQUAL( d[5], 0xf0u );
72     BOOST_CHECK_EQUAL( d[12], 0xf7u );
73
74     d.erase(d.begin());
75     BOOST_CHECK_EQUAL( d.size(), 18u );
76     BOOST_CHECK_EQUAL( d[4], 0xf0u );
77
78     d.erase(d.begin(), d.begin()+11);
79     BOOST_CHECK_EQUAL( d.size(), 7u );
80     BOOST_CHECK_EQUAL( d[0], 0xf7u );
81
82     d.resize(16u, 0xefu);
83     BOOST_CHECK_EQUAL( d.size(), 16u );
84     BOOST_CHECK_EQUAL( d[15], 0xefu );
85     BOOST_CHECK_EQUAL( d[6], 0xabu );
86
87     d.resize(8u, 0xbcu);
88     BOOST_CHECK_EQUAL( d.size(), 8u );
89     BOOST_CHECK_EQUAL( d[7], 0xefu );
90
91     d.clear();
92     BOOST_CHECK_EQUAL( d.size(), 0u );
93     BOOST_CHECK( d.empty() );
94 }
95
96 SENF_AUTO_UNIT_TEST(safePacketIterator)
97 {
98     // We cannot simply allocate a packetData instance .. we must go through PacketInterpreterBase
99     // and PacketImpl.
100
101     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
102
103     senf::PacketData & d (pi->data());
104
105     senf::safe_data_iterator i;
106
107     BOOST_CHECK( ! i );
108     i = senf::safe_data_iterator(d);
109     BOOST_CHECK( i );
110     i = d.begin();
111     BOOST_CHECK( i == senf::safe_data_iterator(d,d.begin()) );
112     BOOST_CHECK( senf::PacketData::iterator(i) == d.begin() );
113
114     senf::PacketData::byte data[] =
115         { 0xf0u, 0xf1u, 0xf2u, 0xf3u, 0xf4u, 0xf5u, 0xf6u, 0xf7u };
116     d.resize(sizeof(data)/sizeof(data[0]));
117     BOOST_CHECK( senf::PacketData::iterator(i) == d.begin() );
118     std::copy(data,data+sizeof(data)/sizeof(data[0]),i);
119
120     BOOST_CHECK_EQUAL( d.size(), sizeof(data)/sizeof(data[0]) );
121     BOOST_CHECK_EQUAL( *(i+sizeof(data)/sizeof(data[0])-1), 0xf7u );
122     BOOST_CHECK_EQUAL( std::distance(i,senf::safe_data_iterator(d,d.end())),
123                        senf::PacketData::difference_type(d.size()) );
124     *(++i) = 0x01u;
125     BOOST_CHECK_EQUAL( d[1], 0x01u );
126     *(--i) = 0x02u;
127     BOOST_CHECK_EQUAL( d[0], 0x02u );
128     BOOST_CHECK( &d == &i.data() );
129 }
130
131 //-/////////////////////////////////////////////////////////////////////////////////////////////////
132 #undef prefix_
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // fill-column: 100
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // comment-column: 40
143 // End: