Merged revisions 262,264-265,267-282,284-298,300-311 via svnmerge from
[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 "PacketImpl.hh"
29 #include "PacketInterpreter.hh"
30 #include "PacketType.hh"
31 #include "main.test.hh"
32
33 #include <boost/test/auto_unit_test.hpp>
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     BOOST_CHECK_EQUAL(
54         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 1u);
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         BOOST_CHECK_EQUAL(
71             senf::pool_alloc_mixin< senf::PacketInterpreter<VoidPacket> >::allocCounter(), 1u);
72         senf::PacketInterpreterBase::ptr pi2 (pi);
73         BOOST_CHECK_EQUAL(p->refcount(), 3);
74     }
75     BOOST_CHECK_EQUAL(p->refcount(),1);
76
77     {
78         senf::PacketInterpreterBase::ptr pi (p->first());
79         BOOST_CHECK_EQUAL(p->refcount(),2);
80         p->truncateInterpreters(pi.get());
81         BOOST_CHECK_EQUAL(p->refcount(),1);
82     }
83     BOOST_CHECK_EQUAL(
84         senf::pool_alloc_mixin<senf::PacketInterpreterBase>::allocCounter(), 0u);
85     BOOST_CHECK_EQUAL(p->refcount(),1);
86
87
88     // The refcount must be one here (from incrementing the refcount above)
89     // Therefore we can safely delete the object.
90     BOOST_CHECK_EQUAL(p->refcount(), 1);
91     p->release();
92     BOOST_CHECK_EQUAL(
93         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
94 }
95
96 BOOST_AUTO_UNIT_TEST(packetImpl_data)
97 {
98     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create());
99     senf::detail::PacketImpl * p (senf::detail::packet::test::TestDriver::impl(pi));
100
101     senf::detail::PacketImpl::byte data[] = 
102         { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
103
104     p->insert(&pi->data(),p->begin(),data, data+sizeof(data));
105     BOOST_CHECK_EQUAL(p->size(), 8u);
106     BOOST_CHECK_EQUAL(p->begin()[0], 0x00u);
107     BOOST_CHECK_EQUAL(p->begin()[7], 0x07u);
108     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
109     
110     p->insert(&pi->data(),p->begin()+2,0xf0u);
111     BOOST_CHECK_EQUAL(p->size(),9u);
112     BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
113     BOOST_CHECK_EQUAL(p->begin()[2], 0xf0u);
114     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
115
116     p->insert(&pi->data(),p->begin()+9,8,0xffu);
117     BOOST_CHECK_EQUAL(p->size(),17u);
118     BOOST_CHECK_EQUAL(p->begin()[16], 0xffu);
119     BOOST_CHECK_EQUAL(p->begin()[8], 0x07u);
120     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
121
122     p->erase(&pi->data(),p->begin());
123     BOOST_CHECK_EQUAL(p->size(),16u);
124     BOOST_CHECK_EQUAL(p->begin()[0], 0x01u);
125     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
126     
127     p->erase(&pi->data(),p->begin()+2, p->begin()+7);
128     BOOST_CHECK_EQUAL(p->size(),11u);
129     BOOST_CHECK_EQUAL(p->begin()[2], 0x07u);
130     BOOST_CHECK_EQUAL(p->begin()[3], 0xffu);
131     BOOST_CHECK_EQUAL(pi->data().size(), p->size());
132
133     BOOST_REQUIRE_EQUAL(pi->data().size(), p->size());
134     BOOST_REQUIRE(pi->data().begin() == p->begin());
135
136     p->clear(&pi->data());
137     BOOST_CHECK_EQUAL(p->size(), 0u);
138     BOOST_CHECK_EQUAL(pi->data().size(), 0u);
139     BOOST_CHECK(pi->data().begin() == p->begin());
140 }
141
142 BOOST_AUTO_UNIT_TEST(packetImpl_interpreters)
143 {
144     senf::detail::PacketImpl * p (new senf::detail::PacketImpl());
145     p->add_ref();
146
147     {
148         senf::PacketInterpreterBase::ptr pi2 (
149             senf::detail::packet::test::TestDriver::create<VoidPacket>(
150                 p,p->begin(),p->end(),senf::PacketInterpreterBase::Append));
151         senf::PacketInterpreterBase::ptr pi3 (
152             senf::detail::packet::test::TestDriver::create<VoidPacket>(
153                 p,p->end(),p->end(),senf::PacketInterpreterBase::Append));
154         senf::PacketInterpreterBase::ptr pi1 (
155             senf::detail::packet::test::TestDriver::create<VoidPacket>(
156                 p,p->begin(),p->end(),senf::PacketInterpreterBase::Prepend));
157
158         BOOST_CHECK_EQUAL(p->first(), pi1.get());
159         BOOST_CHECK_EQUAL(p->next(p->first()), pi2.get());
160         BOOST_CHECK_EQUAL(p->next(p->next(p->first())), pi3.get());
161         BOOST_CHECK( !p->next(p->next(p->next(p->first()))) );
162
163         BOOST_CHECK_EQUAL(p->last(), pi3.get());
164         BOOST_CHECK_EQUAL(p->prev(p->last()), pi2.get());
165         BOOST_CHECK_EQUAL(p->prev(p->prev(p->last())), pi1.get());
166         BOOST_CHECK( !p->prev(p->prev(p->prev(p->last()))) );
167
168         p->insert(&pi2->data(),p->begin(),10,0x00u);
169         BOOST_CHECK_EQUAL(pi1->data().size(), 10u);
170         BOOST_CHECK_EQUAL(pi2->data().size(), 10u);
171         BOOST_CHECK_EQUAL(pi3->data().size(), 0u);
172         BOOST_CHECK( pi1->data().begin() == p->begin() );
173         BOOST_CHECK( pi2->data().begin() == p->begin() );
174         BOOST_CHECK( pi3->data().begin() == p->end() );
175         
176         p->insert(&pi3->data(),p->end(), 0x00u);
177         BOOST_CHECK_EQUAL(pi1->data().size(), 11u);
178         BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
179         BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
180         
181         p->insert(&pi1->data(),p->end(), 2, 0x00u);
182         BOOST_CHECK_EQUAL(pi1->data().size(), 13u);
183         BOOST_CHECK_EQUAL(pi2->data().size(), 11u);
184         BOOST_CHECK_EQUAL(pi3->data().size(), 1u);
185         BOOST_CHECK( pi1->data().end() == p->begin()+13u );
186         BOOST_CHECK( pi2->data().end() == p->begin()+11u );
187         BOOST_CHECK( pi3->data().end() == p->begin()+11u );
188
189         p->clear(&pi2->data());
190         BOOST_CHECK_EQUAL(pi1->data().size(), 2u);
191         BOOST_CHECK( ! p->next(p->next(p->first())) );
192     }
193
194     BOOST_CHECK_EQUAL(p->refcount(), 1);
195     p->release();
196     BOOST_CHECK_EQUAL(
197         senf::pool_alloc_mixin<senf::detail::PacketImpl>::allocCounter(), 0u);
198 }
199
200 ///////////////////////////////cc.e////////////////////////////////////////
201 #undef prefix_
202
203 \f
204 // Local Variables:
205 // mode: c++
206 // fill-column: 100
207 // c-file-style: "senf"
208 // indent-tabs-mode: nil
209 // ispell-local-dictionary: "american"
210 // End: