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