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