Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Packets / PacketImpl.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 PacketImpl unit tests */
25
26 //#include "PacketImpl.test.hh"
27 //#include "PacketImpl.test.ih"
28
29 // Custom includes
30 #include "Packets.hh"
31 #include "main.test.hh"
32
33 #include <sstream>
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace {
41     struct VoidPacket : public senf::PacketTypeBase {};
42 }
43
44 // PacketImpl cannot be tested without relying on PacketInterpreterBase.  However these unit-tests
45 // only validate PacketInterpreterBase as far as to ensure that a failure of one test is not due to
46 // an error in PacketInterpreterbase
47
48 SENF_AUTO_UNIT_TEST(packetImpl_mem)
49 {
50     senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
51     BOOST_CHECK_EQUAL(p->refcount(), 0);
52     p->add_ref();
53     BOOST_CHECK_EQUAL(p->refcount(), 1);
54 #ifdef SENF_DEBUG
55     BOOST_CHECK_EQUAL(
56         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
57 #endif
58     // From now on, the object should stay alive since I manually incremented the
59     // refcount ..
60
61
62     p->add_ref();
63     BOOST_CHECK_EQUAL(p->refcount(), 2);
64     p->release();
65     BOOST_CHECK_EQUAL(p->refcount(), 1);
66
67     {
68         senf::PacketInterpreterBase::ptr pi (
69             senf::detail::packet::test::TestDriver::create<VoidPacket>(
70                 p,p->begin(),p->end(), senf::PacketInterpreterBase::Append));
71         // Hmm ... this check works as long as sizeof(PacketInterpreterBase> !=
72         // sizeof(PacketImpl) ... !!
73 #ifdef SENF_DEBUG
74         BOOST_CHECK_EQUAL(
75             senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
76 #endif
77         senf::PacketInterpreterBase::ptr pi2 (pi);
78         BOOST_CHECK_EQUAL(p->refcount(), 2);
79     }
80     BOOST_CHECK_EQUAL(p->refcount(),1);
81
82     {
83         senf::PacketInterpreterBase::ptr pi (p->first());
84         BOOST_CHECK_EQUAL(p->refcount(),2);
85         p->truncateInterpreters(pi.get());
86         BOOST_CHECK_EQUAL(p->refcount(),1);
87     }
88 #ifdef SENF_DEBUG
89     BOOST_CHECK_EQUAL(
90         senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
91 #endif
92     BOOST_CHECK_EQUAL(p->refcount(),1);
93
94
95     // The refcount must be one here (from incrementing the refcount above)
96     // Therefore we can safely delete the object.
97     BOOST_CHECK_EQUAL(p->refcount(), 1);
98     p->release();
99 #ifdef SENF_DEBUG
100     BOOST_CHECK_EQUAL(
101         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
102 #endif
103 }
104
105 SENF_AUTO_UNIT_TEST(packetImpl_data)
106 {
107     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
108     senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
109
110     senf::detail::PacketImpl::byte data[] =
111         { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
112
113     p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
114     BOOST_CHECK_EQUAL(p->size(), 8u);
115     BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
116     BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
117     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
118
119     p->insert(&pi->data(),p->begin()+2,0xf0u);
120     BOOST_CHECK_EQUAL(p->size(),9u);
121     BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
122     BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
123     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
124
125     p->insert(&pi->data(),p->begin()+9,8,0xffu);
126     BOOST_CHECK_EQUAL(p->size(),17u);
127     BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
128     BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
129     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
130
131     p->erase(&pi->data(),p->begin());
132     BOOST_CHECK_EQUAL(p->size(),16u);
133     BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
134     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
135
136     p->erase(&pi->data(),p->begin()+2, p->begin()+7);
137     BOOST_CHECK_EQUAL(p->size(),11u);
138     BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
139     BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
140     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
141
142     BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
143     BOOST_REQUIRE(pi->data().begin() == p->begin());
144
145     p->clear(&pi->data());
146     BOOST_CHECK_EQUAL(p->size(), 0u);
147     BOOST_CHECK_EQUAL(pi->data().size(), 0u);
148     BOOST_CHECK(pi->data().begin() == p->begin());
149 }
150
151 SENF_AUTO_UNIT_TEST(packetImpl_interpreters)
152 {
153     senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
154     p->add_ref();
155
156     {
157         senf::PacketInterpreterBase::ptr pi3 (
158             senf::detail::packet::test::TestDriver::create<VoidPacket>(
159                 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
160         senf::PacketInterpreterBase::ptr pi4 (
161             senf::detail::packet::test::TestDriver::create<VoidPacket>(
162                 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
163         senf::PacketInterpreterBase::ptr pi1 (
164             senf::detail::packet::test::TestDriver::create<VoidPacket>(
165                 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
166         senf::PacketInterpreterBase::ptr pi2 (
167             senf::detail::packet::test::TestDriver::create<VoidPacket>(
168                 p,p->begin(),p->end(),pi3));
169
170         BOOST_CHECK_EQUAL(p->first(), pi1.get());
171         BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
172         BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
173         BOOST_CHECK_EQUAL(p->next(p->next(p->next(p->first()))), pi4.get());
174         BOOST_CHECK( !p->next(p->next(p->next(p->next(p->first())))) );
175
176         BOOST_CHECK_EQUAL(p->last(), pi4.get());
177         BOOST_CHECK_EQUAL(p->prev(p->last()), pi3.get());
178         BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi2.get());
179         BOOST_CHECK_EQUAL(p->prev(p->prev(p->prev(p->last()))), pi1.get());
180         BOOST_CHECK( !p->prev(p->prev(p->prev(p->prev(p->last())))) );
181
182         p->insert(&pi2->data(),p->begin(),10,0x00u);
183         BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
184         BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
185         BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
186         BOOST_CHECK( pi1->data().begin() == p->begin() );
187         BOOST_CHECK( pi2->data().begin() == p->begin() );
188         BOOST_CHECK( pi3->data().begin() == p->end() );
189
190         p->insert(&pi3->data(),p->end(), 0x00u);
191         BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
192         BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
193         BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
194
195         p->insert(&pi1->data(),p->end(), 2, 0x00u);
196         BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
197         BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
198         BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
199         BOOST_CHECK( pi1->data().end() == p->begin()+13u );
200         BOOST_CHECK( pi2->data().end() == p->begin()+11u );
201         BOOST_CHECK( pi3->data().end() == p->begin()+11u );
202
203         p->clear(&pi2->data());
204         BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
205         BOOST_CHECK( ! p->next(p->next(p->first())) );
206     }
207
208     BOOST_CHECK_EQUAL(p->refcount(), 1);
209     p->release();
210 #ifdef SENF_DEBUG
211     BOOST_CHECK_EQUAL(
212         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
213 #endif
214 }
215
216 //-/////////////////////////////////////////////////////////////////////////////////////////////////
217 #undef prefix_
218
219 \f
220 // Local Variables:
221 // mode: c++
222 // fill-column: 100
223 // c-file-style: "senf"
224 // indent-tabs-mode: nil
225 // ispell-local-dictionary: "american"
226 // compile-command: "scons -u test"
227 // comment-column: 40
228 // End: