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