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