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