X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FPackets%2FPacket.test.cc;h=c714924ebc938501466d004ce92094e4ec04daad;hb=ddb2132be4265f8a0d7d4c954c7c9401e59d027c;hp=6aecfbea691148c55825d8e8464df082575f0f27;hpb=601d1f509f5bb24df167a4dd5a20da67a0af9af8;p=senf.git diff --git a/senf/Packets/Packet.test.cc b/senf/Packets/Packet.test.cc index 6aecfbe..c714924 100644 --- a/senf/Packets/Packet.test.cc +++ b/senf/Packets/Packet.test.cc @@ -2,23 +2,28 @@ // // Copyright (C) 2007 // Fraunhofer Institute for Open Communication Systems (FOKUS) -// Competence Center NETwork research (NET), St. Augustin, GERMANY -// Stefan Bund // -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. +// The contents of this file are subject to the Fraunhofer FOKUS Public License +// Version 1.0 (the "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// http://senf.berlios.de/license.html // -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. +// The Fraunhofer FOKUS Public License Version 1.0 is based on, +// but modifies the Mozilla Public License Version 1.1. +// See the full license text for the amendments. // -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the -// Free Software Foundation, Inc., -// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// Software distributed under the License is distributed on an "AS IS" basis, +// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +// for the specific language governing rights and limitations under the License. +// +// The Original Code is Fraunhofer FOKUS code. +// +// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. +// (registered association), Hansastraße 27 c, 80686 Munich, Germany. +// All Rights Reserved. +// +// Contributor(s): +// Stefan Bund /** \file \brief Packet unit tests */ @@ -29,13 +34,14 @@ // Custom includes #include #include +#include #include "Packets.hh" -#include "../Utils/auto_unit_test.hh" +#include #include #define prefix_ -///////////////////////////////cc.p//////////////////////////////////////// +//-///////////////////////////////////////////////////////////////////////////////////////////////// namespace { @@ -43,7 +49,7 @@ namespace { typedef unsigned key_t; }; - struct FooPacketType + struct FooPacketType : public senf::PacketTypeBase, public senf::PacketTypeMixin { @@ -57,7 +63,7 @@ namespace { // mode. Otherwise, mixin::nextPacketRange() would query the parser for it's size to find // the header size. Since the parser is VoidPacketParser, the header size would therefore be // 0 - static size_type initHeadSize() + static size_type initHeadSize() { return initSize(); } }; typedef senf::ConcretePacket FooPacket; @@ -65,7 +71,7 @@ namespace { struct BarPacketParser : public senf::PacketParserBase { # include SENF_FIXED_PARSER() - + SENF_PARSER_FIELD( type, senf::UInt16Parser ); SENF_PARSER_FIELD( length, senf::Int32Parser ); SENF_PARSER_FIELD( reserved, senf::UInt16Parser ); @@ -77,7 +83,7 @@ namespace { SENF_PARSER_FINALIZE(BarPacketParser); }; - struct BarPacketType + struct BarPacketType : public senf::PacketTypeBase, public senf::PacketTypeMixin { @@ -90,8 +96,8 @@ namespace { using mixin::init; static void dump(packet p, std::ostream & os) { os << "BarPacket:\n" - << " type: " << p->type() << "\n" - << " length: " << p->length() << "\n"; + << senf::fieldName("type") << p->type() << "\n" + << senf::fieldName("length") << p->length() << "\n"; } static void finalize(packet p) { if (p.next(senf::nothrow)) @@ -106,17 +112,17 @@ namespace { typedef BarPacketType::packet BarPacket; namespace reg { - senf::PacketRegistry::RegistrationProxy registerFoo(1u); - senf::PacketRegistry::RegistrationProxy registerBar(2u); + senf::PacketRegistry::ScopedRegistrationProxy registerFoo(1u); + senf::PacketRegistry::ScopedRegistrationProxy registerBar(2u); } struct IntAnnotation { - unsigned value; + boost::uint32_t value; }; std::ostream & operator<<(std::ostream & os, IntAnnotation const & v) { os << v.value; return os; } - + struct LargeAnnotation { char value[32]; }; @@ -126,9 +132,12 @@ namespace { struct ComplexAnnotation : senf::ComplexAnnotation { - ComplexAnnotation() : s(), i() {} + ComplexAnnotation() : s("empty"), i(-1) {} std::string s; - int i; + boost::int32_t i; + // padding so the size does not depend on the platform ... + struct _ {std::string s;boost::int32_t i;}; + char __ [32-sizeof(_)]; }; std::ostream & operator<<(std::ostream & os, ComplexAnnotation const & v) @@ -150,17 +159,22 @@ namespace { } -BOOST_AUTO_UNIT_TEST(packet) +SENF_AUTO_UNIT_TEST(packet) { - senf::Packet packet (FooPacket::create()); + BOOST_CHECK(! senf::Packet().is() ); + senf::Packet packet (FooPacket::create()); BarPacket::createAfter(packet); + BOOST_CHECK_THROW( senf::Packet().as(), senf::WrapException ); + BOOST_CHECK_THROW( packet.as(), senf::WrapException ); + BOOST_REQUIRE( packet ); BOOST_CHECK( packet.next() ); BOOST_CHECK( ! packet.next().next(senf::nothrow) ); + BOOST_CHECK( ! packet.next().next(senf::nothrow).is() ); BOOST_CHECK( ! packet.prev(senf::nothrow) ); BOOST_CHECK( packet.next().prev() == packet ); - BOOST_CHECK( packet.next() != packet ); + SENF_CHECK_NOT_EQUAL( packet.next(), packet ); BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.next().data().begin()), 4 ); BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.data().end()), 12 ); BOOST_CHECK_EQUAL( std::distance(packet.next().data().begin(), packet.next().data().end()), 8 ); @@ -171,17 +185,28 @@ BOOST_AUTO_UNIT_TEST(packet) BOOST_CHECK( packet.next().is() ); BOOST_CHECK( packet.first() == packet ); BOOST_CHECK( packet.last() == packet.next() ); - + + BOOST_CHECK( ! packet.is_shared() ); + { + senf::Packet p2 (packet); + BOOST_CHECK( packet.is_shared() ); + BOOST_CHECK( p2.is_shared() ); + } + BOOST_CHECK( ! packet.is_shared() ); + senf::Packet p2 (packet.next()); BOOST_CHECK( p2 ); + BOOST_CHECK( packet.is_shared() ); + BOOST_CHECK( p2.is_shared() ); packet.parseNextAs(); BOOST_CHECK_EQUAL( packet.size(), 12u ); BOOST_CHECK_EQUAL( packet.next().size(), 8u ); BOOST_CHECK( packet.next().is() ); BOOST_CHECK( ! p2 ); BOOST_CHECK( packet.next().as() ); - + p2 = packet.next().clone(); + BOOST_CHECK( ! packet.is_shared() ); BOOST_REQUIRE( p2 ); packet.next().append( p2 ); BOOST_REQUIRE( packet.next().next() ); @@ -193,19 +218,18 @@ BOOST_AUTO_UNIT_TEST(packet) BOOST_CHECK_EQUAL( senf::PacketRegistry::key(packet), 1u ); packet.next().parseNextAs( senf::PacketRegistry::lookup(2u).factory() ); BOOST_CHECK( packet.next().next().is() ); - + std::stringstream s; packet.dump(s); - BOOST_CHECK_EQUAL( s.str(), + BOOST_CHECK_EQUAL( s.str(), "Annotations:\n" - " (anonymous namespace)::ComplexAnnotation: no value\n" - " (anonymous namespace)::IntAnnotation: 0\n" + " (anonymous namespace)::IntAnnotation : 0\n" "BarPacket:\n" - " type: 0\n" - " length: 0\n" ); - + " type : 0\n" + " length : 0\n" ); + packet.finalizeAll(); - BOOST_CHECK_EQUAL( packet.last().as()->type(), + BOOST_CHECK_EQUAL( packet.last().as()->type(), BarPacket::Parser::type_t::value_type(-1) ); packet.last().append(FooPacket::create()); packet.finalizeThis(); @@ -236,13 +260,15 @@ BOOST_AUTO_UNIT_TEST(packet) BOOST_CHECK( packet.last().rfind() == packet.last() ); BOOST_CHECK( packet.next() == packet.next() ); BOOST_CHECK( packet.last().prev().prev() == packet ); - + senf::DataPacket::createAfter(packet); BOOST_CHECK_THROW( packet.next().next().next().parseNextAs(), senf::InvalidPacketChainException ); + + SENF_CHECK_NO_THROW( BarPacket::create(senf::noinit).dump(s)); } -BOOST_AUTO_UNIT_TEST(concretePacket) +SENF_AUTO_UNIT_TEST(concretePacket) { senf::PacketData::byte data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; @@ -266,40 +292,61 @@ BOOST_AUTO_UNIT_TEST(concretePacket) // No 'u' suffix here to check, that the disable_if works ... BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10).size(), 10u ); BOOST_CHECK_EQUAL( packet.size(), 14u ); - + BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,senf::noinit).size(), 2u ); BOOST_CHECK_EQUAL( packet.size(), 6u ); - + BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,data).size(), 6u ); BOOST_CHECK_EQUAL( packet.size(), 10u ); - + BOOST_CHECK_EQUAL( FooPacket::createBefore(packet).size(), 14u ); BOOST_CHECK_EQUAL( packet.size(), 10u ); BOOST_CHECK_EQUAL( FooPacket::createBefore(packet,senf::noinit).size(), 10u ); BOOST_CHECK_EQUAL( packet.size(), 10u ); - BOOST_CHECK( packet.clone() != packet ); + BOOST_CHECK_EQUAL( FooPacket::createInsertBefore(packet).size(), 14u ); + BOOST_CHECK_EQUAL( packet.size(), 10u ); + BOOST_REQUIRE( packet.prev() ); + BOOST_CHECK_EQUAL( packet.prev().size(), 14u ); + BOOST_REQUIRE( packet.prev().prev() ); + BOOST_CHECK_EQUAL( packet.prev().prev().size(), 14u ); + + BOOST_CHECK_EQUAL( FooPacket::createInsertBefore(packet,senf::noinit).size(), 10u ); + BOOST_CHECK_EQUAL( packet.size(), 10u ); + BOOST_REQUIRE_NO_THROW( packet.prev().prev().prev() ); + BOOST_CHECK_THROW( packet.prev().prev().prev().prev(), senf::InvalidPacketChainException ); + BOOST_CHECK_EQUAL( packet.prev().size(), 10u ); + BOOST_CHECK_EQUAL( packet.prev().prev().size(), 14u ); + BOOST_CHECK_EQUAL( packet.prev().prev().prev().size(), 14u ); + + SENF_CHECK_NOT_EQUAL( packet.clone(), packet ); BOOST_CHECK_EQUAL( BarPacket::create()->reserved(), 0xA0A0u ); } -BOOST_AUTO_UNIT_TEST(packetAssign) +SENF_AUTO_UNIT_TEST(packetAssign) { BarPacket bar1 (BarPacket::create()); BarPacket bar2 (BarPacket::create()); bar2->type() << 0x2A2Bu; bar1.parser() << bar2; - + BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu ); } -BOOST_AUTO_UNIT_TEST(packetAnnotation) +SENF_AUTO_UNIT_TEST(packetAnnotation) { + typedef senf::detail::AnnotationRegistry Reg; + senf::Packet packet (FooPacket::create()); BarPacket::createAfter(packet); ComplexAnnotation & ca (packet.annotation()); + + BOOST_CHECK_EQUAL( ca.s, "empty" ); + BOOST_CHECK_EQUAL( ca.i, -1 ); + ca.s = "dead beef"; ca.i = 0x12345678; SENF_CHECK_NO_THROW( packet.annotation().value = 0xDEADBEEF ); @@ -310,10 +357,33 @@ BOOST_AUTO_UNIT_TEST(packetAnnotation) BOOST_CHECK_EQUAL( p2.annotation().s, "dead beef" ); BOOST_CHECK_EQUAL( p2.annotation().i, 0x12345678 ); - BOOST_CHECK( senf::detail::AnnotationIndexer::Small ); - BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); - BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); - BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); + senf::Packet pClone (packet.clone()); + + p2.clearAnnotations(); + BOOST_CHECK_EQUAL( p2.annotation().s, "empty" ); + BOOST_CHECK_EQUAL( p2.annotation().i, -1 ); + BOOST_CHECK_EQUAL( p2.annotation().value, 0 ); + + BOOST_CHECK_EQUAL( pClone.annotation().value, 0xDEADBEEFu ); + BOOST_CHECK_EQUAL( pClone.annotation().s, "dead beef" ); + BOOST_CHECK_EQUAL( pClone.annotation().i, 0x12345678 ); + + BOOST_CHECK( Reg::lookup() >= 0 ); + BOOST_CHECK( Reg::lookup() < 0 ); + BOOST_CHECK( Reg::lookup() < 0 ); + BOOST_CHECK( Reg::lookup() < 0 ); + + std::stringstream ss; + senf::dumpPacketAnnotationRegistry(ss); + BOOST_CHECK_EQUAL( + ss.str(), + "SENF_PACKET_ANNOTATION_SLOTS = 8\n" + "SENF_PACKET_ANNOTATION_SLOTSIZE = 16\n" + "TYPE FAST COMPLEX SIZE\n" + "(anonymous namespace)::ComplexAnnotation no yes 32\n" + "(anonymous namespace)::ComplexEmptyAnnotation no yes 1\n" + "(anonymous namespace)::IntAnnotation yes no 4\n" + "(anonymous namespace)::LargeAnnotation no no 32\n" ); } #ifdef COMPILE_CHECK @@ -324,7 +394,7 @@ COMPILE_FAIL(invalidAnnotation) # ifdef BOOST_HAS_TYPE_TRAITS_INTRINSICS senf::Packet packet (FooPacket::create()); - (void) packet.annotation(); + senf::IGNORE( packet.annotation() ); # else # endif @@ -336,7 +406,7 @@ COMPILE_FAIL(invalidAnnotation) #endif -///////////////////////////////cc.e//////////////////////////////////////// +//-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_