switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / PacketData.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketData unit tests */
30
31 //#include "PacketData.test.hh"
32 //#include "PacketData.test.ih"
33
34 // Custom includes
35 #include "Packets.hh"
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 namespace {
44     struct VoidPacket : public senf::PacketTypeBase {};
45 }
46
47 SENF_AUTO_UNIT_TEST(packetData)
48 {
49     // We cannot simply allocate a packetData instance .. we must go through PacketInterpreterBase
50     // and PacketImpl.
51
52     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
53
54     senf::PacketData & d (pi->data());\
55
56     BOOST_CHECK( d.begin() == d.end() );
57     BOOST_CHECK_EQUAL( d.size(), 0u );
58     BOOST_CHECK( d.empty() );
59
60     d.insert(d.begin(), 0xabu);
61     BOOST_CHECK_EQUAL( d.size(), 1u );
62     BOOST_CHECK_EQUAL( d[0], 0xabu );
63     BOOST_CHECK( !d.empty() );
64
65     d.insert(d.begin(), 10, 0xcdu );
66     BOOST_CHECK_EQUAL( d.size(), 11u );
67     BOOST_CHECK_EQUAL( d[0], 0xcdu );
68     BOOST_CHECK_EQUAL( d[9], 0xcdu );
69     BOOST_CHECK_EQUAL( d[10], 0xabu );
70
71     senf::PacketData::byte data[] =
72         { 0xf0u, 0xf1u, 0xf2u, 0xf3u, 0xf4u, 0xf5u, 0xf6u, 0xf7u };
73     d.insert(d.begin()+5, data, data+sizeof(data)/sizeof(data[0]));
74     BOOST_CHECK_EQUAL( d.size(), 19u );
75     BOOST_CHECK_EQUAL( d[4], 0xcdu );
76     BOOST_CHECK_EQUAL( d[5], 0xf0u );
77     BOOST_CHECK_EQUAL( d[12], 0xf7u );
78
79     d.erase(d.begin());
80     BOOST_CHECK_EQUAL( d.size(), 18u );
81     BOOST_CHECK_EQUAL( d[4], 0xf0u );
82
83     d.erase(d.begin(), d.begin()+11);
84     BOOST_CHECK_EQUAL( d.size(), 7u );
85     BOOST_CHECK_EQUAL( d[0], 0xf7u );
86
87     d.resize(16u, 0xefu);
88     BOOST_CHECK_EQUAL( d.size(), 16u );
89     BOOST_CHECK_EQUAL( d[15], 0xefu );
90     BOOST_CHECK_EQUAL( d[6], 0xabu );
91
92     d.resize(8u, 0xbcu);
93     BOOST_CHECK_EQUAL( d.size(), 8u );
94     BOOST_CHECK_EQUAL( d[7], 0xefu );
95
96     d.clear();
97     BOOST_CHECK_EQUAL( d.size(), 0u );
98     BOOST_CHECK( d.empty() );
99 }
100
101 SENF_AUTO_UNIT_TEST(safePacketIterator)
102 {
103     // We cannot simply allocate a packetData instance .. we must go through PacketInterpreterBase
104     // and PacketImpl.
105
106     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
107
108     senf::PacketData & d (pi->data());
109
110     senf::safe_data_iterator i;
111
112     BOOST_CHECK( ! i );
113     i = senf::safe_data_iterator(d);
114     BOOST_CHECK( i );
115     i = d.begin();
116     BOOST_CHECK( i == senf::safe_data_iterator(d,d.begin()) );
117     BOOST_CHECK( senf::PacketData::iterator(i) == d.begin() );
118
119     senf::PacketData::byte data[] =
120         { 0xf0u, 0xf1u, 0xf2u, 0xf3u, 0xf4u, 0xf5u, 0xf6u, 0xf7u };
121     d.resize(sizeof(data)/sizeof(data[0]));
122     BOOST_CHECK( senf::PacketData::iterator(i) == d.begin() );
123     std::copy(data,data+sizeof(data)/sizeof(data[0]),i);
124
125     BOOST_CHECK_EQUAL( d.size(), sizeof(data)/sizeof(data[0]) );
126     BOOST_CHECK_EQUAL( *(i+sizeof(data)/sizeof(data[0])-1), 0xf7u );
127     BOOST_CHECK_EQUAL( std::distance(i,senf::safe_data_iterator(d,d.end())),
128                        senf::PacketData::difference_type(d.size()) );
129     *(++i) = 0x01u;
130     BOOST_CHECK_EQUAL( d[1], 0x01u );
131     *(--i) = 0x02u;
132     BOOST_CHECK_EQUAL( d[0], 0x02u );
133     BOOST_CHECK( &d == &i.data() );
134 }
135
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 #undef prefix_
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // comment-column: 40
148 // End: